Php object-oriented-polymorphism. Php object-oriented -- polymorphism first explains polymorphism: php object-oriented polymorphism refers to the method implemented by defining subclasses through an abstract class or interface, php object-oriented in a function or class-polymorphism
First, let's explain polymorphism: php object-oriented polymorphism refers to the method implemented by defining subclasses through an abstract class or interface, then, input the names of objects after each subclass instantiation defined previously in a function or class method, and call the same method in the new class (because the parameters are different) different execution logics of different objects are implemented. (Your understanding is not official ).
The following is an example:
// Define the parent class interface
Interface Person {
Function getName ();
Function getAge ();
}
// The first subclass
Class Phper implements Person {
Public function getName ()
{
Echo 'this is php \ 'getname ';
}
Public function getAge ()
{
Echo 'this is php \ 'getage ';
}
}
// The second subclass
Class Javaer implements Person {
Public function getName ()
{
Echo 'this is jave \ 'getname ';
}
Public function getAge ()
{
Echo 'this is java \ 'getage ';
}
}
// A brand new class
Class my {
Function isMy ($ obj) // The parameter here is an object name
{
$ Obj-> getName ();
Echo"
";
$ Obj-> getAge ();
}
}
// Several classes before instantiation
$ Php = new Phper;
$ Java = new Javaer;
$ My = new My;
$ My-> isMy ($ java); // execute different business logic by passing in different object names
?>
If you want to ask me how to use this item, I can only say that it may be helpful in terms of code specifications and understanding. I am not sure about the others. this is because I have never encountered application polymorphism in my actual work.
I hope Boyou can give explanations or links in comments for new insights or better cases.
Explain the polymorphism first: php object-oriented polymorphism refers to defining the method implemented by the subclass through an abstract class or interface, and then defining the method in a function or class...