PHP polymorphic code instance, PHP polymorphic instance
Polymorphic definition: Only one interface or base class is concerned, not the specific class of an object. (same type, different result)
Here are two examples:
First, we find that the base class defines the standard, and the subclass implements the self-rule. This is a requirement for polymorphism. At the same time, this is a satisfying rewrite; in fact, it is different from the different classes, not strictly satisfies an interface, or the base class programming. Because you call not Stu->showgrade () but their own methods;
Class stu{public function Showgrade () { echo "base class"; }} Class Xiaomin extends stu{public function Showgrade () { echo ' is son show '; }} Class Xiaoli extends stu{public function Showgrade () { echo ' is son show '; }} function doit ($obj) { if (Get_class ($obj)! = "Stu") { $obj->showgrade () }} Doit (new Xiaoli ());d Oit (New Xiaomin ());
The second example: the Dovoice parameter specifies that $obj is a animal, and consciousness is to accept the implementation class object with the interface. The upward transformation. This conforms to the same type, with different results, which is polymorphic;
In fact, in Java it would be animal a = new dog (); Because PHP is a type language. There is no object transformation mechanism.
Interface animal{public function voice (); Class Cat implements animal{public function voice () { echo] miao~~~
"; }} Class Dog implements animal{public function voice () { echo ' Wang ~ ~ ~
"; }} function Dovoice (animal $obj) { $obj->voice ();} Dovoice (New Dog ());d Ovoice (New Cat ());
http://www.bkjia.com/PHPjc/1022787.html www.bkjia.com true http://www.bkjia.com/PHPjc/1022787.html techarticle PHP polymorphic Code instance, php polymorphic instance polymorphism definition: only one interface or base class is concerned, not the specific class of an object. (same type, different results) Here are two examples of ...