What is polymorphism?
Polymorphism is the third feature of object-oriented language following the abstraction and inheritance of database. Polymorphism is a variety of forms, with the ability to express a variety of forms of characteristics. Representation in an object-oriented manner is handled differently depending on the type of object. Polymorphism allows each object to respond to a common message in a way that suits itself. Polymorphism enhances the flexibility and reusability of software.
If we create a doing () method, if the student prints the class, the company clerk prints to work.
General Practice
Use if to determine
/** * PHP polymorphism *//define Student class student{ public Function cla () { echo "student worker is in class!<br/>"; }}//define staff class of fice{Public function Wor () { echo "clerk is working!<br/>"; }}//Judging object type method function doing ($obj) { if ($obj instanceof student) { $obj->cla (); } ElseIf ($obj instanceof Office) { $obj->wor (); } else{ echo "No this object!" "; }} Doing (new student ()); Students are in class doing (new Office ()); The clerk is at work
Above results output:
The students are having a class
The clerk is at work
This common method has a disadvantage, that is, if a lot of objects, then if. else.. Very long and inflexible.
Polymorphism practices
Defines a public abstract method that inherits it from all subclasses.
/** * PHP polymorphism *//define a common class pub{ protected function working () { echo "This method needs to be overloaded in subclasses!; }}//Define student class, Inherit public class PUBC Lass Student extends pub{ public Function working () { echo "student worker is in class!<br/>"; }}//Define employee class, inherit common class Pubclass Office extends pub{public function working () { echo "clerk is working!<br/>";} }//Judging object type method function doing ($ obj) { if ($obj instanceof Pub) { $obj->working (); } else{ echo "No this object!" "; }} Doing (new student ()); Students are in class doing (new Office ()); The clerk is at work
This is a polymorphic feature that is flexible to reuse.
Other practices
From the way of realization of polymorphism, we can standardize the method of each class to overload the parent class, so as to achieve uniform effect. And when we define classes, it is possible to add a uniform approach to our own. So the above example can be implemented as well:
/** * PHP polymorphism *///Definition of class student{//define a uniform method Pub public function pub () { echo "Student work is in class!<br/>"; }}//define staff Class office{ //define Uniform method Pub public function pub () { echo "clerk is working!<br/>"; }}// Determine the object type method function doing ($obj) { if ($obj) { //Call class Uniform method $obj->pub (); } else{ Echo ' does not have this object '; }} doing (new student ());//students are attending classes doing (new Office ()); The clerk is at work
Polymorphism can also be understood as a programming approach, and the ultimate goal of programming is: flexible, polymorphic, reusable, efficient.