The question about polymorphism in PHP This post was last edited by chenlong451 in Wikipedia from 2013-03-2823: 51: 16. there is a definition about polymorphism: in object-oriented language, many different implementation methods of the interface are Polymorphism. please refer to this article: Questions about polymorphism in http://www.cnblogs.com/tecs27/archive/2012/03 PHP
At the end of this post, the definition of polymorphism in Wikipedia was edited by chenlong451 at 23:51:16. in object-oriented languages, the implementation of interfaces is polymorphism.
Read this article:
Http://www.cnblogs.com/tecs27/archive/2012/03/13/2394028.html
Two sample codes are provided in this article. the code segment that uses flow control is as follows:
Class painter {// defines the painter class
Public function paintbrush () {// defines the painter action
Echo "the painter is painting! \ N ";
}
}
Class typist {// defines the typist class
Public function typed () {// defines the work of a typist
Echo "Typist Typing! \ N ";
}
}
Function printworking ($ obj) {// defines the processing class
If ($ obj instanceof painter) {// if the object is a painter class, the painter action is displayed.
$ Obj-> paintbrush ();
} Elseif ($ obj instanceof typist) {// if the object is a typist class, the typist action is displayed.
$ Obj-> typed ();
} Else {// if the preceding class is not used, the error message is displayed.
Echo "Error: object Error! ";
}
}
Printworking (new painter (); // Display employees' work
Printworking (new typist (); // Display employees' work
To demonstrate polymorphism, the following code segment uses polymorphism:
Class employee {// defines the employee parent class
Protected function working () {// defines employee work, which must be implemented in the subclass
Echo "this method needs to be reloaded in the subclass! ";
}
}
Class painter extends employee {// defines the painter class
Public function working () {// how to implement inheritance
Echo "the painter is painting! \ N ";
}
}
Class typist extends employee {// defines the typist class
Public function working (){
Echo "Typist Typing! \ N ";