Abstract classes, abstract methods, and interfaces of PHP, and abstract interfaces of php
Abstract classes and abstract methods in PHP are not used much by themselves, but they are often used by others in projects. Similarly, today, when reading other people's code, I found that abstract classes are used, summary:
- Abstract class:
1. If a method in a class is an abstract method, the class is an abstract class;
2. abstract keywords must be added to the abstract class;
- Abstract method:
Definition: if a method does not have a method body, the method is an abstract method.
1. If a method does not have a method body, use a semicolon to end the method;
2. for abstract methods, you must use abstract keywords to describe them.
Note: 1. As long as a class with abstract modification is an abstract class
2. Apart from abstract classes that can have abstract methods, they are exactly the same as normal classes.
3,Abstract classes cannot instantiate objects
4. If an abstract class exists, you must have a subclass of this class, and write a method in the subclass to overwrite the abstract method in the abstract class (with the method Body added)
5. The subclass must all override the abstract method in the parent class before it can be instantiated. If it is only the implementation part, The subclass is still an abstract class.
Abstract classes: strictly require the subclass structure, so abstract classes are actually a standard.
The role of an abstract method is to specify that the subclass must have this method and assign the function to the subclass for processing. Only the structure of the method is written, but not implemented (no method body ), specific function implementation is implemented by specific sub-classes according to their own needs
1/** 2 * abstract class 3*4 */5 abstract class Person 6 {7 public $ name; 8 public $ age; 9 10 // abstract method 11 abstract function say (); 12 13 abstract function eat (); 14 15 function run () 16 {17 echo "hello world "; 18} 19 20 function sleep () 21 {22 echo "test"; 23} 24}
1/** 2 * subclass of the abstract class 3*4 */5 class StudentCn extends Person 6 {7 function say () 8 {9 echo "I speak Chinese <br> "; 10} 11 12 function eat () 13 {14 echo "I eat with chopsticks"; 15} 16}
Interface:An interface is a special abstract class.
1. abstract classes and interfaces all have abstract methods.
2. abstract classes and interfaces cannot be instantiated.
3. abstract classes and interfaces have the same meaning (I .e., function)
Comparison: a. All methods in the interface must be abstract methods. Therefore, abstract keywords are not required for abstract methods in the interface. simply end with a semicolon.
B. The member attribute in the interface must be a constant (no variable is available)
C. All methods must be public.
D. The declared interface does not apply to class, but uses interface
Tips: 1. You can use extends to allow one interface to inherit from another interface, that is, common inheritance (new extension abstract method) without overwriting.
2. You can use a class to implement all the methods in the interface, or an abstract class to implement some methods in the interface.
3. Do not use the keyword extends. Use implements to implement implements, which is equivalent to extends.
4. A class can use implements to implement an interface while inheriting another class, or implement multiple interfaces (it must be inherited first before implementing the interface)
1/** 2 * interface 3 */4 interface Demo 5 {6 const Name = ""; 7 const Age = 10; 8 9 function test (); 10 11 function test2 (); 12} 13 echo Demo: Name; // access constant 14 15/** 16 * interface inherited interface, used to extend the interface 17 */18 interface Test extends Demo19 {20 function test4 (); 21} 22 23/** 24 * Common class 25 */26 class world27 {28 function test5 () 29 {30} 31} 32 33 34/** 35 * inheritance + Interface 36 */37 class Hello extends World implements Test38 {39 40 function test () 41 {42} 43 44 function test2 () 45 {46} 47 48 function test3 () 49 {50} 51 52 function test4 () 53 {54} 55 56 function test5 () 57 {58} 59 60}