Multi-state applications
Polymorphism is one of the three most object-oriented features in addition to encapsulation and inheritance, and I personally think that PHP can be
To achieve polymorphism, but polymorphism is not as significant as the C + + and Java object-oriented languages, because PHP
itself is a weakly typed language, there is no parent object converted to a subclass object or a subclass object converted to a parent class
The problem of image, so the application of polymorphism is not so obvious; the so-called polymorphism refers to a program that can handle many types of
The ability of an object, such as working in a company, paying a salary every month, and the same method of paying wages, not
Employees of the same or different positions are issued through this method, but the wages are not the same.
So there are many forms of the same wage-issuing method. For object-oriented programs, polymorphism is the target class
Like assigning to a parent class reference, and then calling the method of the parent class to execute the method that the subclass overrides the parent class, but in PHP it is
Weakly typed, the object reference is the same, not the parent class reference, or the subclass reference.
Let's take a look at an example, first of all, to have the relationship between the parent class object and the subclass object by using polymorphism. Make a
The shape of the interface or abstract class as the parent class, which has two abstract methods, one way to find the circumference, the other is to
The method of area; The subclass of this interface is a number of different shapes, each with a perimeter and an area, and because the parent class is
An interface, so the subclass must implement an abstract method of the two perimeter and area of the parent class, so that the purpose
Is that each of the different shapes of the subclass are subject to the specification of the parent interface, there must be a way to seek Zhou Changhe area.
Code fragment
Copy Code code as follows:
?
Defines a shape's interface, which has two abstract methods for subclasses to implement
Interface shape{
function area ();
function perimeter ();
}
Defines a rectangular subclass that implements the perimeter and area of a shape interface
Class Rect implements shape{
Private $width;
Private $height;
function __construct ($width, $height) {
$this->width= $width;
$this->height= $height;
}
function Area () {
Return "The area of the rectangle is:". ($this->width* $this->height);
}
function perimeter () {
Return "The circumference of the rectangle is:". (2* ($this->width+ $this->height));
}
}
Defines a circular subclass that implements the perimeter and area of the shape interface
Class Circular implements shape{
Private $radius;
function __construct ($radius) {
$this->radius= $radius;
}
function Area () {
Return "The area of the circle is:". (3.14* $this->radius* $this->radius);
}
function perimeter () {
Return "The circumference of the circle is:". (2*3.14* $this->radius);
}
}
A reference to a shape that is assigned to a rectangular object
$shape =new Rect (5, 10);
echo $shape->area (). <br> ";
echo $shape->perimeter (). <br> ";
Butt Class A circular object is assigned to a reference to a shape
$shape =new Circular (10);
echo $shape->area (). <br> ";
echo $shape->perimeter (). <br> ";
?>
The previous example performs the result:
Execution results
The size of the rectangle is: 50
The circumference of the rectangle is: 30
The area of the circle is: 314
Circumference of the circle is: 62.8
In the example above, we see that the rectangular object and the circular object are assigned to the variable $shape, and the $shape reference is called
The area and perimeter of the method, there are different results, this is a polymorphic application, in fact, in our PHP this weak
In the object-oriented language of the class shape, polymorphism is not particularly obvious, but it is the variable of object type.
Application.