interface
接口类: 成员方法必须声明为public访问权限,有定义但没有具体实现,可以声明常量但无法声明属性,无法实例化,只能被其他类implements(实现)的特殊类.只能extends(继承)接口,但可以继承多个interface(接口) .
1.接口中定义的所有方法都必须没有实现,只有定义
<?php //错误示例: 方法进行了实现 interface human { public function walk(){ // todo } public function run(){ // todo } }
( ! ) Fatal error: Interface function human::walk() cannot contain body in F:\wamp\www\blog.54skyer.cn\index.php on line 81
2.接口中所有方法的访问修饰符都必须是public
<?php //错误示例: 方法访问权限修饰符不是public interface human { protected function walk(); private function run(); }
( ! ) Fatal error: Access type for interface method human::walk() must be omitted in F:\wamp\www\blog.54skyer.cn\index.php on line 79
<?php //正确示例: 方法访问权限修饰符必须都是public,且方法只有定义没有实现. interface human { public function walk(); public function run(); }
3.可以定义常量,但不能定义属性
<?php //错误示例: 定义属性 interface mp4 { $name = 'mp4'; public function vedio(); public function music(); }
( ! ) Parse error: syntax error, unexpected '$name' (T_VARIABLE), expecting function (T_FUNCTION) in F:\wamp\www\blog.54skyer.cn\index.php on line 80
<?php //正确示例: 定义常量 interface mp4 { const name = 'mp4'; public function vedio(); public function music(); }
4.不能被实例化,只能被其他类实现
<?php //错误示例: 实例化 interface human { public function walk(); public function run(); } $human = new human();
( ! ) Fatal error: Cannot instantiate interface human in F:\wamp\www\blog.54skyer.cn\index.php on line 83
<?php //错误示例: 不是所有的方法都被实现.teacher::run()没有方法体 interface human { public function walk(); public function run(); } //$human = new human(); class teacher implements human { public function walk(){ echo "teacher walk"; } public function run(); }
( ! ) Fatal error: Non-abstract method teacher::run() must contain body in F:\wamp\www\blog.54skyer.cn\index.php on line 90
<?php //错误示例: 不是所有的方法都被实现.human::walk没有被实现 interface human { public function walk(); public function run(); } //$human = new human(); class teacher implements human { public function run(){ echo "teacher run"; }; }
( ! ) Fatal error: Class teacher contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (human::walk) in F:\wamp\www\blog.54skyer.cn\index.php on line 92
<?php //正确示例: 所有的方法都被实现. interface human { public function walk(); public function run(); } //$human = new human(); class teacher implements human { public function walk(){ echo "teacher walk"; } public function run(){ echo "teacher run"; }; // 还有有自己的特殊行为 public function teach(){ echo "teacher teach"; } } $teacher = new teacher(); $teacher->walk(); $teacher->run(); $teacher->teach();
5.可以extends(继承)其他接口,最后的类implements(实现)接口时,需要将继承链上的所有接口的抽象方法都实现
<?php //错误示例: 没有将继承链上的接口的方法都实现 interface human { public function walk(); public function run(); } interface male extends human { public function showSelf(); } class man implements male { } $man = man();
Fatal error: Class man contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (male::showSelf, human::walk, human::run) in F:\wamp\www\blog.54skyer.cn\index.php on line 90
<?php //正确示例: 继承链上的接口的方法都实现 interface human { public function walk(); public function run(); } interface male extends human { public function showSelf(); } class man implements male { } class man implements male { public function walk(){ echo "man wallk"; } public function run(){ echo "man run"; } public function showSelf(){ echo "man show himself"; } } $man = new man(); $man->walk(); $man->run(); $man->showSelf();
6.可以继承多个接口,多个被继承的接口用逗号隔开,最后的类implements(实现)接口时,需要将继承链上的所有接口的抽象方法都实现
<?php //错误示例: 没有将继承链上的接口的方法都实现 interface mp3 { public function music(); } interface phone { public function call(); } interface mobilephone extends mp3, phone { public function note(); } class iphone implements mobilephone { } $iphone = new iphone();
( ! ) Fatal error: Class iphone contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (mobilephone::note, mp3::music, phone::call) in F:\wamp\www\blog.54skyer.cn\index.php on line 92
<?php //正确示例: 将继承链上的接口的方法都实现 interface mp3 { public function music(); } interface phone { public function call(); } interface mobilephone extends mp3, phone { public function note(); } class iphone implements mobilephone { public function music(){ echo 'play a music'; } public function call(){ echo 'make a call'; } public function note(){ echo 'write a note'; } } $iphone = new iphone(); $iphone->music(); $iphone->call(); $iphone->note();
总结: 接口更多是定义一个标准,用来规范属于这个抽象范围内的子类或者实现类.如果希望代码工程是标准规范可控,应该业务模型阶段把接口都定义好,写业务逻辑时只需要关注实现就可以了,方法属性命名都可以在定义接口类时定义好.
注:
implements(实现)接口的第一个普通类[即可以是普通的class也可以是abstract class],所有方法访问权限必须为public,方法的实现必须保持参数数量一致,参数名称可以不同.
有关abstract(抽象类)的相关只是见另一篇博文 => 类相关的关键字 - abstrct
欢迎评论区交流讨论!