This article mainly introduces the PHP interface and abstract class use examples, and did a detailed explanation, the need for friends can refer to the following
Abstract class 1. An abstract class is a class that has an abstract keyword in front of class and that has an abstraction method (the abstract keyword is added before the class method function keyword). 2. Abstract classes cannot be directly instantiated. The method that is required to define (or partially implement) subclasses in an abstract class. Subclasses can specify abstract classes by inheriting abstract classes and by implementing all abstract methods in an abstract class. 3. If the subclass needs to be instantiated, it implements all the abstract methods in the abstract class. If the subclass does not fully implement all abstract methods in the abstract class, the subclass is also an abstract class that must precede the class with the abstract keyword and cannot be instantiated. Code as follows: abstract class A { /** abstract class can define variables */ protected $value 1 = 0; Private $value 2 = 1; Public $value 3 = 2; /** can also define Non-abstract methods */ Public Function my_print () { echo "hello,world/n"; } /** * In most cases, an abstract class contains at least one abstract method. Abstract methods are declared with an abstract keyword, which cannot have concrete content. * can declare an abstract method as you would a normal class method, but end with a semicolon rather than a method body. That is, abstract methods cannot be implemented in abstract classes, that is, there is no function body "{Some codes}". */ abstract protected function abstract_func1 (); Abstract protected function abstract_func2 (); } Abstract class B extends A { public Function abstract_func1 () &N Bsp { echo "Implement the Abstract_func1 in class a/n"; } /** Zend Studio 8 will error */ //abstract protected function abstract_func2 (); } Class C extends B { public Function abstract_func2 () {&N Bsp echo "Implement the Abstract_func2 in class a/n"; } } 4. If a subclass B inherits from A is created as follows, but the abstract method is not implemented Abstract_func (): Code is as follows: Class B extends a{}; Then the program will appear with the following error Error: Code as follows: fatal error:class B contains 1 abstract method and must therefore is declared abstract or IM Plement the remaining methods (A::abstract_func) 5. Such asFruit B implements the abstract method Abstract_func (), then the access control of the Abstract_func () method in B is not stricter than the access control of Abstract_func () in a (), that is to say: (1) If Abstract_func () in A is declared public, then the declaration of Abstract_func () in B can only be public, not protected or private (2) If Abstract_func () in A is declared protected, then the declaration of Abstract_func () in B can be public or protected, but not private ( 3) if A Abstract_func () declared private, hehe, can not be defined as private Oh! (Fatal error:abstract function A::abstract_func () cannot be declared private) II, interface interface 1. Abstract classes provide a standard for implementation, whereas interfaces are pure templates. Interfaces define functionality only and do not contain the content of the implementation. The interface is declared with the keyword interface. 2. Interface is completely abstract, can only declare methods, and can only declare public methods, cannot declare private and protected methods, cannot define the method body or declare instance variables. However, interface can declare constant variables. However, the objective of the existence of the constant variable in interface is violated, and the different value of interface and class is also confused. If you do, you can place it in the corresponding abstract class or class. Code as follows: interface IA { const avar=3; public Function iAfunc1 () ; PUBlic function IAfunc2 (); } Echo IA:: avar; 3. Any class that implements an interface implements all the methods code defined in the interface: Class E implements IA { public Function iAfunc1 () {echo "I n iAfunc1 ";} Public Function IAfunc2 () {echo ' in IAfunc2 ';} } Otherwise the class must be declared abstract. Copy code code as follows: Abstract class E implements ia{} 4. A class can implement an interface in a declaration using the Implements keyword. After doing so, the exact process of implementing the interface is the same as inheriting an abstract class that contains only abstract methods. A class can inherit a parent class and implement any number of interfaces at the same time. The extends clause should precede the implements clause. PHP only supports inheriting from a parent class, so the extends keyword can only be followed by a class name. Code as follows: interface IB { public function iBfunc1 (); Public funct Ion IBfunc2 (); } Class D extends A implements Ia,ib { public Function abstract_func1 () & nbsp { echo "Implement the Abstract_func1 in class a/n"; } Public Function absTRACT_FUNC2 () { echo "Implement the Abstract_func2 in class a/n"; nbsp } public Function iAfunc1 () {echo ' in IAfunc1 ';} public Function IA Func2 () {echo ' in IAfunc2 '} public Function iBfunc1 () {echo ' in IBfunc1 ';} public function IBfunc2 () {echo "in IBfunc2";} } Class D extends B implements Ia,ib { & nbsp Public Function abstract_func1 () {  PARENT::ABSTRACT_FUNC1 (); echo "Override the Abstract_func1 in class a/n"; } public Function abstract_func2 () { & nbsp echo "Implement the Abstract_func2 in class a/n"; } public Function iAfunc1 () {echo ' in IAfunc1 ';} , public function IAfunc2 () {echo ' in IAfunc2 ';} public Function iBfunc1 () {echo ' in IBfunc1 ';} & nbsp Public Function IBfunc2 () {echo ' in IBfunc2 ';} } 5. Interfaces may not implement another interface, but can inherit multiple code as follows: interface IC extends ia,ib{} class F implements IC { ublic function iAfunc1 () {echo ' in IAfunc1 ';} public Function IAfunc2 () {echo ' in IAfunc2 ';} &NB Sp Public Function iBfunc1 () {echo ' in IBfunc1 ';} public Function IBfunc2 () {echo ' in IBfunc2 ';} &n Bsp } III, abstract class and interface similarities and differences 1. Same point: (1) Both are abstract classes and cannot be instantiated. The subclasses of the (2) interface implementation class and abstract class must implement the abstract method that has been declared. 2. Different points: (1) interface need to implement, to use implements, and abstract class need to inherit, to use extends. (2) A class can implement multiple interface, but a class can inherit only one abstract class. (3) interface emphasis on specific featuresImplementation, while the abstract class emphasizes the owning relationship. (4) Although subclasses of the interface implementation class and abstract class must implement the corresponding abstract method, the implementation is in a different form. Each of the methods in interface is an abstract method, which is only declared (declaration, no method body), and the implementation class must be implemented. And the subclasses of the abstract class can be implemented selectively. This choice has two points: a) Not all methods in abstract class are abstracted, only those whose crowns have abstractions are abstract, and subclasses must be implemented. Methods that do not have an abstract, the method body must be defined in abstract class; b The subclass of the abstract class inherits it, can either inherit directly from or overwrite the Non-abstract method, and the abstract method can optionally be implemented or left to its subclasses. However, this class must also be declared as an abstract class. is both an abstract class and, of course, cannot be instantiated. (5) abstract class is an intermediary between interface and class. Abstract class plays a connecting role in interface and class. On one hand, abstract class is abstract, you can declare an abstraction method to standardize the functionality that a subclass must implement, and on the other, it can define the default method body for use or overwrite by subclasses. In addition, it can define its own instance variables to be used by the subclass through inheritance. The abstract methods in the (6) interfaces are not, and cannot be added to, an abstract keyword, the default implicit is an abstraction, and the final keyword cannot be added to prevent the inheritance of abstract methods. Abstract methods in an abstract class must be preceded by an abstract representation of the display declaration as an abstraction. The abstract methods in (7) interfaces are public by default and can only be public, and cannot be decorated with private, protected modifiers. Abstract methods in an abstract class can be decorated with public, protected, but not private. 3. Interface applications (1) classes and classesRequires a specific interface to be coordinated, regardless of how it is implemented. (2) as the identity that can implement a particular function, or what interface method does not have a pure identity. (3) requires that a group of classes be treated as a single class, and that the caller only contacts this group of classes through an interface. (4) needs to implement a number of specific functions, and these features may be completely without any connection. 4. The application of abstract class A word, you can use it when you need a unified interface, and you need an instance variable or a default method. The most common are: (1) defines a set of interfaces, but does not want to force each implementation class to implement all interfaces. You can define a set of method bodies in abstract class, or even an empty method body, and then the subclass chooses the method that it is interested in to overwrite. (2) In some cases, only a pure interface can not meet the coordination between classes and classes, but also required classes to represent the state variables to distinguish between different relationships. The mediating effect of abstract can satisfy this very well. (3) regulates a set of coordinated methods, some of which are common, state-independent, shareable, without subclasses, and others that require individual subclasses to implement specific functions according to their specific state.