The similarities between interfaces and abstract classes:
1. Both abstract classes and interfaces have abstract methods
2. Abstract classes and interfaces cannot create instance objects
3. Abstract classes and interfaces have the same meaning (define a specification)
The difference between interfaces and abstract classes:
1. All methods in the interface must be abstract methods (non-abstract methods cannot be used), so do not use abstract
in all methods of the interface, and directly end with a semicolon
2. Member attributes in the interface must be constants (cannot have variables)
3. All permissions of the interface must be public (public
)
4 , do not use class
to declare the interface, use interface
interface Person{ public $name; //不能声明变量 报错 const NAME='tom'// 可以声明常量 function test(); //因为接口中全部是抽象方法 所以 省去 abstract 。 function test1(); Protect function test3() 报错 只可以是 public } $re=new Person; //报错 不可以 创建实例化 对象 。 echo Person:: NAME; 输出常量。
Recommended tutorial: PHP video tutorial
The above is the detailed content of What are the similarities and differences between interfaces and abstract classes in PHP. For more information, please follow other related articles on the PHP Chinese website!