Home > Article > Backend Development > What is the difference between php abstract classes and interfaces?
This article will introduce to you the difference between PHP abstract classes and interfaces. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Abstract class
Please check the document for the official description, the official description is below A simplified version of the description:
(Abstract classes can have no abstract methods, but abstract classes still cannot be instantiated) A method defined as abstract only declares its calling method (parameters), cannot be defined Its specific function is realized.
For example:
abstract class AbstractClass { // 强制要求子类定义这些方法,且不能定义具体功能 注意没有大括号{} abstract protected function getValue (); abstract protected function prefixValue ( $prefix ); // 普通方法(非抽象方法) public function printOut () { print $this -> getValue () . "\n" ; } }
In addition, the calling method of the method must match, that is, the type and the number of required parameters must be consistent. For example, if the subclass defines an optional parameter ($b in function eat($a,$b=1) is an optional parameter), but it is not included in the declaration of the abstract method of the parent class, then There is no conflict between the two statements. This also applies to constructors since PHP 5.4. Constructor declarations before PHP 5.4 could be different.
Supplement:
1. Abstract classes can have member attributes.
2. Someone asked: Can an abstract method be defined as private? The answer is no, because the purpose of the abstract interface is to abstract the class model for inheritance. It is defined as private and cannot be accessed externally. If the design purpose is not met, an error will be reported.
3. An abstract class can implement an interface, and does not need to implement its methods
abstract class Sutdent extends Human { abstract private function study(); } Fatal error: Abstract function Sutdent::study() cannot be declared private in ...
4. An abstract class can inherit an abstract class, and cannotoverwrite the abstract parent class abstract method. Such usage can be understood as an extension of abstract classes. Such as
abstract class Human { abstract function eat(); } abstract class Sutdent extends Human { abstract function study(); //abstract function eat(); 若重写抽象父类的抽象方法eat()会报错 Fatal error: Can't inherit abstract function Human::eat() (previously declared abstract in Sutdent) in ... }
Interface
1. Definition of interface
interface Play { const LEVEL=10; public function PlayLOL(); public function PlayFootball(); }
2. Implementation of the interface
To implement an interface, use the implements operator. Non-abstract classes must implement all methods defined in the interface, otherwise a fatal error will be reported. A class can implement multiple interfaces. Use commas to separate the names of multiple interfaces.
Additional:
interface Play { const LEVEL=10; public function PlayLOL(); public function PlayFootball(); } interface Read { public function ReadNovel(); } abstract class Human { abstract function eat(); } //抽象类可以实现接口后不实现其方法,可以继承一个抽象类的同时实现多个接口注意必须要把extends语句写在implements前面,否则会报错 abstract class Sutdent extends Human implements Play,Read { abstract function study(); }
#3. Interface inheritance
The interface can inherit another or multiple interfaces, use the extends keyword, multiple Separated by ',', but you cannot implement another interface, and of course you cannot inherit an abstract class (error when inheriting an abstract class: Fatal error: PlayGame cannot implement Human - it is not an interface in D:\11 \index.php on line 10)
interface Play { public function PlayFootball(); } interface PlayNew { public function PlayFootballNew(); } interface PlayGame extends play,PlayNew { public function PlayLOL(); }
Difference:
1. Use implements for interface inheritance, Abstract classes use extends.
2. Variables cannot be declared in interfaces, but class constants can be declared. Various variables can be declared in abstract classes
3. Interfaces have no constructors, but abstract classes can Yes
4. The methods in the interface are public by default, and the methods in the abstract class can be modified with public, protected, private
5. A class can inherit multiple interfaces, but it can only inherit An abstract class
Related recommendations: PHP tutorial
The above is the detailed content of What is the difference between php abstract classes and interfaces?. For more information, please follow other related articles on the PHP Chinese website!