In PHP, only inheritance from one class is supported, and it is not supported to inherit from two or more classes at the same time. The ability to inherit from two or more two classes is called multiple inheritance. PHP is designed to prohibit this functionality. The reason for this is to avoid the complexities of multiple classes. When you find that you need to inherit from two or more classes, you can take the method of the parent class apart and use the interface for the same purpose.
Interfaces are similar to classes, however, the interface can contain only method prototypes, and cannot contain any complete definition methods. This prevents method conflicts that can occur when using abstract classes, allowing multiple interfaces to be used on a given implementation class. However, since an interface cannot define a method with a full implementation, we must provide a non-abstract base class separately if we want to provide the default functionality for the inheritors.
Description: The base class, is the father, grandparents class, etc., derived classes, such as subclasses, grandchildren and so on.
1. Interface definition
Use Keyword: interface
Define an interface Ispeedinfointerface ispeedinfo{public function Getmaximumspeed ();}
2. Use of the interface
Use keyword: implements
Define an extension class Fastcar, and call the interface Ispeedinfoclass Fastcar extends Car implements ispeedinfo{ function Getmaximumspeed () { return;} }
Description: Extend class Fastcar, Know Ispeedinfo interface, and car base class is not aware of Ispeedinfo interface.
Note: When we want to call two or more than two interfaces, we use commas to separate the two
Class Fastcar extends Car implements ispeedinfoa,ispeedinfob,ispeedinfoc{}
3. Calling methods in the interface
Call $a=new Fastcar (); Echo $a->getmaximumspeed ();
======= Full Code ======
1<?PHP2 //define an interface Ispeedinfo3 Interfaceispeedinfo{4 Public functiongetmaximumspeed ();5 }6 7 //define an abstract class car8 classcar{9 //any non-abstract methodTen } One A //define an extension class Fastcar and call the interface Ispeedinfo - ClassFastcarextendsCarImplementsispeedinfo{ - functiongetmaximumspeed () { the return150; - } - } - + //called - $a=NewFastcar (); + Echo $a-getmaximumspeed (); A?>
View Code
Interface interface, interface inheritance implements