tags: abstract class interface abstract class and interface php
## Introduction: This is a question that is often asked in interviews and is also a classic question. We try our best to quote official authoritative instructions or conduct experiments to prove the accuracy of the content stated in this article.Abstract classPlease check the document for the official description. The following is a simplified version of the official description:
cannotdefine its specific function implementation. For example,
abstract class AbstractClass { // 强制要求子类定义这些方法,且不能定义具体功能 注意没有大括号{} abstract protected function getValue (); abstract protected function prefixValue ( $prefix ); // 普通方法(非抽象方法) public function printOut () { print $this -> getValue () . "\n" ; } }
is similar to function eat(
b=1) The $b is the 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 declarations. This also applies to constructors since PHP 5.4. Constructor declarations before PHP 5.4 could be different.abstract class Sutdent extends Human{ abstract private function study();}
Fatal error: Abstract function Sutdent::study() cannot be declared private in D:\11\index.php on line 10
and cannotoverride abstract methods of abstract parent classes. Such usage can be understood as an extension of abstract classes. For example,
abstract class Human{ abstract function eat();}abstract class Sutdent extends Human{ abstract function study(); //abstract function eat(); 若重写抽象父类的抽象方法eat()会报错}
Interface
) .
interface Play { const LEVEL=10; public function PlayLOL(); public function PlayFootball(); }
Replenish:
You can inherit abstract classes and implement interfaces at the same time. Extends must be written in front.
Abstract classes implement interfaces, and there is no need to redo the methods.
Inheritance of interfaceAn interface can inherit another or multiple interfaces, use the extends keyword, and separate multiple ones with ',', but You cannot implement another interface, and of course you cannot inherit an abstract class (error when inheriting an abstract class:
- #When implementing multiple interfaces, methods in the interfaces cannot have the same name.
- Interfaces can also be inherited, by using the extends operator.
- To implement an interface, a class must use methods that are exactly the same as those defined in the interface. Otherwise a fatal error will result.
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();}Copy after login
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 Play1 { public function PlayFootball(); }interface PlayGame extends play,Play1{ public function PlayLOL(); }
Let’s briefly summarize: abstract classes are generally used to define a type of entity What is it? It includes attributes, abstract methods and non-abstract methods. An interface is used to define what a class of entities can do. It is generally believed that it only has abstract methods, and constants are rarely used. Related recommendations:
The difference between php abstract classes and interfacesThe above is the detailed content of The difference between abstract classes and interfaces in php. For more information, please follow other related articles on the PHP Chinese website!