Dive into the implementation details of php polymorphism

  • 2020-06-12 08:35:19
  • OfStack

Polymorphism means that the same operation or function, procedure can act on multiple types of objects and obtain different results. Different objects receiving the same 1 message can produce different results, a phenomenon known as polymorphism.

Polymorphism allows each object to respond to a common message in a way that suits it. Polymorphism enhances software flexibility and reuse.

Polymorphism is one of the most important parts of object-oriented software development. Object-oriented programming is not just a simple combination of relevant methods and data, but the use of object-oriented programming in various elements of a clear description of various situations in real life. Polymorphism in object-oriented programming is explained in detail in this section.

1. What is polymorphism

Polymorphism (Polymorphism) literally means "multiple shapes". It can be understood as multiple manifestations, namely "1 external interface, multiple internal implementation methods". In object-oriented theory, the general definition of polymorphism is that the same operation acting on different instances of a class will produce different execution results. That is, when objects of different classes receive the same message, they will get different results.

In practical application development, adopting polymorphism in object-oriented mainly lies in that different subclass objects can be treated as a parent class, and the differences between different subclass objects can be screened, common code can be written, and common programming can be made to adapt to the changing needs.

2. Application design of polymorphism

In practical application development, in order to make the project easy to expand and upgrade in the future, it is necessary to easily upgrade reusable modules through inheritance. When designing reusable modules, it is necessary to minimize the use of process control statements. This design can then be implemented using polymorphism.

[example] illustrates that process control statements are commonly used to implement different types of processing. The code is shown below.

<?php
    class painter{                                    // Defines the painter class 
        public function paintbrush(){                 // Define the painter action 
            echo " The painter is painting! /n";
        }
    }
    class typist{                                    // Defines the typist class 
        public function typed(){                     // Define a typist's job 
            echo " The typist is typing! /n";
        }
    }
    function printworking($obj){                    // Define 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 not, an error message is displayed 
            echo "Error:  Object error! ";
        }
    }
    printworking(new painter());                    // Display employee work 
    printworking(new typist());                     // Display employee work 
?> 

Analysis: In the above procedure, two employee classes are defined first: the painter class and the typist class. It then defines a handler function in which it determines whether the employee is a defined employee and prints out the employee's working status. The results are shown below.
The painter is painting
The typist is typing
As you can easily see from the above program, if you want to display the work status of several of its employees, you need to first define the employee class, define the employee's work in the employee class, and then add elseif statements to the printworking() function to check which instance of the employee class the object is. This is very undesirable in practical applications. If polymorphism is used, this problem can be easily solved.

You can start by creating an employee parent class from which all employee classes will inherit, and all methods and properties of the parent class will be inherited. A "yes 1" relationship is then created in the employee class to determine if it is a legitimate employee.

The above example is rewritten in a polymorphic way. The code is shown below.

<?php
class employee{// Define the employee parent class 
protected function working(){// Defining employee work requires implementation in subclasses 
echo " This method needs to be overridden in a subclass !";
}
}
class painter extends employee{// Defines the painter class 
public function working(){// Implement the working method of inheritance 
echo " The painter is painting! /n";
}
}
class typist extends employee{// Defines the typist class 
public function working(){
echo " The typist is typing! /n";
}
}
class manager extends employee{// Defining the manager class 
public function working(){
echo " The manager is in a meeting! ";
}
}
function printworking($obj){// Define processing methods 
if($obj instanceof employee){// In the case of an employee object, its work status is displayed 
$obj->working();
}else{// Otherwise an error message is displayed 
echo "Error:  Object error! ";
}
}
printworking(new painter());// Show the painter's work 
printworking(new typist());// Shows what a typist does 
printworking(new manager());// Show the manager's work 
?> 

Analysis: In the above procedure, first define 1 employee base class, and define 1 employee working status method. Then define the three employee classes that will inherit from the employee base class: the painter class, the typist class, and the manager class. Then define the method to display the employee's working status. A "yes 1" relationship is created in the method to determine whether it is a legitimate employee. The results are shown below.
The painter is painting!
The typist is typing!
The manager is in a meeting!
As you can see from the above example, no matter how many employee classes are added, you only need to implement the employee class and methods inherited from the employee's parent class. Without changing the method printworking() that shows the employee's working status.

Related articles: