1.定义:接口,使用interface关键字定义,与类类似,专门用来规范一些共性类必须实现的方法。
interface people{}
2.接口实现:接口是用来规范类必须完成的事情,所以接口只能被类实现:implements。(不允许实例化)
class man implements people{}
3 .接口成员:接口中只能定义公有抽象方法和接口常量
interface animal{ const name = '人';//只允许有接口常量 public function eat();//接口方法必须为公有抽象方法}
4.接口的实现类必须实现所有的抽象方法,或者实现类为抽象类,接口常量可以直接在实现类中访问
interface animal{ const name = '人'; public function eat(); }//实现接口class man implements animal{ //必须实现接口所有抽象方法 public function eat(){ echo self::name; //可以访问接口常量 }}//抽象类实现接口abstract class ladyboy implements animal{} //正常实现
5.实现接口的类成员,不允许重写接口中的常量,不允许增加接口方法的控制权限
interface animal{ const name = '人'; public function eat(); }class woman implements animal{ //重写接口常量 const name = '女人'; //错误:不允许重写接口常量 //强化接口方法控制 private function eat(){} //错误:接口方法不允许使用其他访问修饰限定符,必须使用public}
6.接口可以继承接口:extends,而且接口可以多继承接口
interface plant{ public function lightning();}interface animal{ public function eat();}//单继承interface man extends animal{}//多继承interface apple extends plant,animal{}
推荐:php视频教程
以上就是特立独行的世外高人-php中的interface的详细内容。