PHP 5 also supports the interface concept and introduces the interface and implements keywords for it. Like Java, PHP 5 uses interfaces to achieve the same effect as "Multi-inheritance.
The PHP5 interface syntax is as follows:
When declaring a class, the keyword we use is "class", while the interface is a special class and the keyword used is "interface ";
Class Definition: class name {... }, Interface declaration: interface name {...}
The code for the specific PHP5 abstract class is as follows:
| The Code is as follows: |
Copy code |
Abstract class AbstractClass { Abstract public function test (); } Class ImplementedClass extends AbstractClass { Public function test (){ Echo "ImplementedClass: test () called ."; } } $ O = new ImplementedClass; $ O-> test (); |
Instance 2
| The Code is as follows: |
Copy code |
Interface displayable { Function display (); } Interface printable { Function doprint (); } Class foo implements displayable, printable { Function display (){ // Code } Function doprint (){ // Code } } |
The introduction of abstract classes and PHP5 interfaces makes PHP a fully object-oriented language.
// Define an interface using the interface keyword. "One" is the interface name.
| The Code is as follows: |
Copy code |
Interface One { // Define a constant Const constant = 'constant value '; // Defines an abstract method "fun1" Public function fun1 (); // Defines the abstract method "fun2" Public function fun2 (); } |
In the above example, an interface "one" is defined, which declares two abstract methods "fun1" and "fun2", because all methods in the interface are abstract methods, therefore, when declaring an abstract method, you do not need to use the "abstract" keyword as the abstract class. This keyword is added by default, in addition, the "public" access permission in the interface can also be removed, because it is public by default, because all the members in the interface are public, for the Members in the interface, we cannot use the "private" and "protected" permissions, both of which must be public or default. In addition, we also declare a constant "constant" in the interface. Because variable members cannot be used in the interface, we need to use the const keyword for declaration.
Because an interface is a special abstract class, all the methods in it are abstract methods, so the interface cannot generate instance objects; it is also a standard, and all abstract methods need to be implemented by subclasses.
We can use the "extends" keyword to let an interface inherit from another interface;
| The Code is as follows: |
Copy code |
// Use extends to inherit another interface Interface Two extends One { Function fun3 (); Function fun4 (); } |
We define a subclass of an interface to implement the keyword "implements" used by all abstract methods in the interface, rather than the "extends" mentioned above ";
// Use the keyword "implements" to implement the abstract method in the interface
| The Code is as follows: |
Copy code |
Class Three implements One { Function fun1 () { ... ... } Function fun2 () { ... ... } } |
// All methods are implemented. We can use subclasses to instantiate objects.
| The Code is as follows: |
Copy code |
| $ Three = new Three (); |
We can also use abstract classes to implement partial abstract methods in Interfaces. However, to instantiate an object, this abstract class must have a subclass to implement all its abstract methods;
As we have mentioned earlier, PHP is a single inheritance. A class can only have one parent class, but a class can implement multiple interfaces, which is equivalent to a class complying with multiple specifications, just as we must not only abide by national laws, but also comply with school rules if we are in school;
| The Code is as follows: |
Copy code |
// Use implements to implement multiple interfaces Class Four implemtns interface 1, interface 2 ,...... { // The object can be instantiated only when all methods in the interface are implemented. } |
In PHP, not only can a class implement multiple interfaces, but also can inherit a class and implement multiple interfaces at the same time. You must inherit the class before implementing the interface;
| The Code is as follows: |
Copy code |
// Use extends to inherit a class and implements to implement multiple interfaces Class Four extends class name 1 implemtns interface 1, interface 2 ,...... { // Objects can be instantiated only when methods in all interfaces are implemented ......... } |
After talking so much, let's finally look at an instance.
VideoCard. php interface file (function interface definition of the video card)
| The Code is as follows: |
Copy code |
<? Php Interface VideoCardInter { Function Display (); Function getName (); } ?> |
Dmeng. php implementation interface (how can the emon manufacturer implement these interfaces? The motherboard manufacturer does not need to care about them)
| The Code is as follows: |
Copy code |
<? Php Include_once ("VideoCard. php "); Class Dmeng implements VideoCardInter { Function Display (){ Echo "Display "; } Function getName (){ Return "Dmeng VideoCard "; } } ?> |
Mainboard. php application interface (plug the video card into the motherboard, the motherboard only needs to use these interfaces, you can also do not need)
| The Code is as follows: |
Copy code |
<? Php Include_once ("VideoCard. php "); Include_once ("Dmeng. php "); Class Mainboard { Var $ vc; Function run (VideoCardInter $ vc) {// defines the VideoCardInter interface type parameter. At this time, you do not know who will implement it. $ This-> vc = $ vc; $ This-> vc-> Display (); Echo "main board running! "; } } $ Conputer = new Mainboard (); $ Conputer-> run (new Dmeng ); |
// When used, write the name of the implemented interface class. (the video card of EMR can also be changed to another home, as long as they all implement the interface)
?> Above is