What is polymorphism?
polymorphism, which comes from the definition of dictionary.com, "appears in different forms, stages, or types in an independent organization, or in a homogeneous organization, without a fundamental distinction." "By this definition, we can argue that polymorphism is a programming way to describe the same object through a variety of states or stages." In fact, the real meaning of it is: in actual development, we only need to focus on an interface or base class programming, without worrying about the specific classes (class) that an object belongs to.
If you are familiar with design patterns, you will understand this concept even if you have a preliminary understanding. In fact, PHP5 polymorphism may be the greatest tool in programming based on pattern design. It allows us to organize similar objects in a logical way so that we do not have to worry about the specific types of objects when we encode them, and we only need to program to one of the desired interfaces or base classes. The more abstract an application is, the more flexible it becomes--and polymorphism is one of the best ways to abstract behavior.
For example, let's consider a class called person. We can subclass a person with classes called David,charles and Alejandro. The person has an abstract method Acceptfeedback (), and all subclasses implement this method. This means that any code that uses a subclass of the base class person can Invoke Method Acceptfeedback (). You don't have to check if the object is a David or a Alejandro, just knowing that it is a person is enough. As a result, your code only needs to focus on the "lowest common denominator"-person class.
The person class in this example can also be created as an interface. Of course, there are some differences compared to the above, mainly because an interface does not give any behavior, but only the set of rules. A person interface requires that you must support the Addfeedback () method, while a person class can provide the default code for some Addfeedback () method-Your understanding can be "if you do not choose to support Addfeedback (), Then you should provide a default implementation. "As to how to select an interface or base class is not the subject of this article, but generally, you need to implement a default method through the base class." You can also use an interface if you can simply sketch out a set of desired features that your class is going to implement.
Popular understanding
the most direct definition of polymorphism is to allow different classes of objects with inherited relationships to be invoked on member functions of the same name, producing different results.
Polymorphic Code
<?php/** * Declaration Interface Demo * @author wzy * */interface Demo {Const NAME = "Wangzhengy
I ";
Const AGE = 25; function fun1 ();
The Declaration method defaults to the public abstract function fun2 (); Class One implements Demo {public Function fun1 () {echo demo::name.
"Studying at the Communication University of Communication"; The Public Function fun2 () {echo demo::name. "The Age is".
Demo::age; } class two implements Demo {public Function fun1 () {echo demo::name.
"In Beijing Ling Chong Public and Technology Co., Ltd. Internship"; The Public Function fun2 () {echo demo::name.
"Last year's age was 24"; }///the same interface to implement the same method, different objects, different reaction results.
This is the manifestation and application of polymorphism $one = new One (); $one->fun1 (); Wangzhengyi studied at the Communication University $one->fun2 ();
The age of Wangzhengyi is the echo "<br>";
$two = new Two (); $two->fun1 (); Wangzhengyi in Beijing Ling Chong Public and Technology Co., Ltd. internship $two->fun2 (); Wangzhengyi's age was 24 last year.