<?PHP//Polymorphism is performed by different people to produce different results://A person performs the same action in different States, producing different results can also be called polymorphism//polymorphic principle, that is, the class is written well, do not modify it, as long as the call parameters outside the class Change//Excuse specification of the subclass of the action
Interfacecomputer{ Public functionVersion ();//This method indicates what method to use Public functionWork ();//how it works. } //implement an excuse, a notebook Class A desktop class classNotecomputerImplementscomputer{ Public functionversion () {EchoLaptop; } Public functionWork () {Echo' Run Win7 '; } } classDesktopcomputerImplementscomputer{ Public functionversion () {EchoDesktop; } Public functionWork () {Echo' Run XP '; } } //Create a user to use your computer classperson{//Create a method to accept the computer Public function_run ($type){ Echo' The person's '; $type-version (); $type-Work (); } } $note=NewNotecomputer (); $desktop=NewDesktopcomputer (); $person=NewPerson (); $person->_run ($note);//object references are passed, holding each other's references Echo' <br/> '; $person->_run ($desktop);?>
PHP polymorphic Simple Example