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, the implementation of different interfaces is Polymorphism. please refer to the following article: www.cnblogs.comtecs27archive201203132394028.html, which provides two questions about polymorphism in PHP:
This post was last edited by chenlong451 at 23:51:16
In Wikipedia, there is a definition of polymorphism: in object-oriented languages, interfaces are implemented in a variety of different ways, that 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 ";