php
classes do not allow multiple inheritance
, but interface
can multiple inheritance
, and secondly, using interfaces canEncapsulation
The specific implementation does not expose the specific implementation details to the outside. It only exposes the interface
, and users can only access it through the interface
, which also has a certain degree of security. .
1. Definition : Interface, defined using the interface
keyword, is similar to a class and is specifically used to standardize the methods that some common classes must implement.
interface People{}
2. Interface implementation: The interface is used to standardize what a class must accomplish, so the interface can only be implemented by the class: implements
. (Instantiation is not allowed)
class Man implements People{}
3 .Interface members: Only public abstract methods
and interface constants
interface Animal{ const NAME = '人';//只允许有接口常量 public function eat();//接口方法必须为公有抽象方法 }
4. The implementation class of the interface must implement all abstract methods
,or the implementation class is abstract class
,Interface constants
can be accessed directly in the implementation class
interface Animal{ const NAME = '人'; public function eat(); } //实现接口 class Man implements Animal{ //必须实现接口所有抽象方法 public function eat(){ echo self::NAME; //可以访问接口常量 } } //抽象类实现接口 abstract class Ladyboy implements Animal{} //正常实现
5. The class members that implement the interface
,are not It is allowed to rewrite the constants in the interface, and it is not allowed to increase the control permissions of the interface methods
interface Animal{ const NAME = '人'; public function eat(); } class Woman implements Animal{ //重写接口常量 const NAME = '女人'; //错误:不允许重写接口常量 //强化接口方法控制 private function eat(){} //错误:接口方法不允许使用其他访问修饰限定符,必须使用public }
6. The interface can inherit the interface:extends,
And the interface can inherit from multiple interfaces
interface Plant{ public function lightning(); } interface Animal{ public function eat(); } //单继承 interface Man extends Animal{} //多继承 interface Apple extends Plant,Animal{}
The above is the detailed content of Maverick outsider - interface in php. For more information, please follow other related articles on the PHP Chinese website!