1<?PHP2 //interface keyword for defining interfaces3 Interfaceicaneat{4 //the method inside the interface does not need to implement the method5 Public functionEat$food) ;6 }7 //The implemaents keyword is used to indicate that a class implements an interface8 classHumanImplementsicaneat{9 //after implementing an interface, you must provide a concrete implementation of the methods defined in the interfaceTen Public functionEat$food) { One Echo"Human eating".$food." <br/> "; A } - } - classAnimalImplementsicaneat{ the //after implementing an interface, you must provide a concrete implementation of the methods defined in the interface - Public functionEat$food) { - Echo"Animal eating".$food." <br/> "; - } + } - $obj=NewHuman (); + $obj->eat (' Apple '); A at $monkey=NewAnimal (); - $monkey->eat (' Banana '); - - //cannot instantiate interface - //$EATOBJ = new Icaneat (); - in //You can use the INSTANCEOF keyword to determine whether an object implements an interface - Var_dump($objinstanceof icaneat); to + functionCheckeat ($obj) { - if($objinstanceof Icaneat) { the $obj->eat (' food '); *}Else { $ Echo"The obj can ' t eat." <br/> ";Panax Notoginseng } - } the +Checkeat ($obj)." <br/> "; ACheckeat ($monkey); the + //you can use extends to let interfaces inherit interfaces - InterfaceIcanpeeextendsicaneat{ $ Public functionpee (); $ } - //When a class implements a sub-interface, the method defined by the parent interface also needs to be implemented in this class specifically - classHuman1Implementsicanpee{ the Public functionpee () {} - Public functionEat$food){}Wuyi}
Summarize:
A class implements (implements) Thedifference between an interface and an inheritance (extends) of a class- similar to implementing interfaces and inheriting classes, but interfaces cannot directly create their own objects ·· If you create a "will eat this object", then how to eat at all do not know --The inherited parent must have a concrete implementation of the method, the subclass can override the parent class method, or do not rewrite the interface inside the method is not required to implement, As long as you define the name and parameters of the method ,-- the specific implementation must be defined in the implementation class --A short summary: The method of the class must be implemented, the method of the interface must be empty
Polymorphic:
Polymorphism: Because the interface method implementation can have many kinds of methods, so the definition of the interface inside the method of specific internship is a variety of, this characteristic we call polymorphism such as interface A has two implementations B and C,b and C on the definition of a method of the implementation can be different, this phenomenon is polymorphic
For an example of the above interface
1 // The same line of code, for passing in the implementation of different interfaces of the object, the performance is different, this is polymorphic 2 Checkeat ($obj); 3 Checkeat ($monkey);
Interface interface and polymorphism meaning