In-depth implementation of php polymorphism _ PHP Tutorial

Source: Internet
Author: User
Tags class manager
Go deep into the implementation of php polymorphism. Polymorphism means that the same operation, function, and process can act on multiple types of objects and obtain different results. Different objects receive the same message and produce different results. This polymorphism means that the same operation, function, and process can act on multiple types of objects and obtain different results. Different objects receive the same message and produce different results. this phenomenon is called polymorphism.

Polymorphism allows each object to respond to common messages in a suitable way. Polymorphism enhances software flexibility and reusability.

Polymorphism is one of the most important parts in object-oriented software development. Object-oriented programming does not simply combine related methods with data, but uses various elements of object-oriented programming to clearly describe various situations in real life. This section describes the polymorphism in object-oriented programming in detail.

1. what is polymorphism?

Polymorphism literally means "multiple shapes ". It can be understood as a variety of forms, that is, "one external interface, multiple internal implementation methods ". In the object-oriented theory, polymorphism is generally defined as: the same operation acts on instances of different classes and will produce different execution results. That is, different classes of objects receive different results when they receive the same message.

In actual application development, the polymorphism in object orientation is mainly used to treat different subclass objects as a parent class, and the differences between different subclass objects can be shielded, write common code and make general programming to adapt to the changing needs.

2. application design with polymorphism

In actual application development, in order to enable the project to easily implement expansion and upgrade in the future, you need to implement reusable modules through inheritance for easy upgrade. When designing reusable modules, we need to minimize the use of flow control statements. This design can be implemented using polymorphism.

[Example] describes how to use flow control statements to process different classes.The code is as follows.

The code is as follows:


Class painter {// defines the painter class
Public function paintbrush () {// defines the painter action
Echo "the painter is painting! /N ";
}
}
Class typist {// defines the typist class
Public function typed () {// defines the work of a typist
Echo "Typist Typing! /N ";
}
}
Function printworking ($ obj) {// defines the 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 the preceding class is not used, the error message is displayed.
Echo "Error: object Error! ";
}
}
Printworking (new painter (); // Display employees' work
Printworking (new typist (); // Display employees' work
?>


Analysis:In the preceding procedure, two employee classes are defined: Painter and typist. Define a processing function to determine whether an employee is a defined employee and print the employee's working status. The result is as follows.
The painter is painting
Typist Typing
It can be easily seen from the above program that to display the working status of several of its employees, you need to first define the employee class, define the work of the employee in the employee class, and then in printworking () add the elseif statement to the function to check the instance of the employee class of the object. This is very undesirable in practical applications. If polymorphism is used at this time, the problem can be easily solved.

You can create an employee parent class. all employee classes inherit from the employee parent class and inherit all methods and attributes of the parent class. Create a "yes" relationship in the employee class to determine whether the employee is a legal employee.

[Example] illustrates how to rewrite the previous example by using polymorphism.. The code is as follows.

The code is as follows:


Class employee {// defines the employee parent class
Protected function working () {// defines employee work, which must be implemented in the subclass
Echo "this method needs to be reloaded in the subclass! ";
}
}
Class painter extends employee {// defines the painter class
Public function working () {// how to implement inheritance
Echo "the painter is painting! /N ";
}
}
Class typist extends employee {// defines the typist class
Public function working (){
Echo "Typist Typing! /N ";
}
}
Class manager extends employee {// defines the manager class
Public function working (){
Echo "the manager is in a meeting! ";
}
}
Function printworking ($ obj) {// defines the processing method
If ($ obj instanceof employee) {// if it is an employee object, its working status is displayed.
$ Obj-> working ();
} Else {// otherwise, an error message is displayed.
Echo "Error: object Error! ";
}
}
Printworking (new painter (); // display the painter's work
Printworking (new typist (); // display the work of a typist
Printworking (new manager (); // display the manager's work
?>


Analysis: In the above procedure, first define an employee base class and define a method of employee working status. Then define three employee classes that will inherit from the employee base class: Painter class, typist class, and manager class. Then define a method to display the employee's working status. Create a "yes" relationship in the method to determine whether the employee is valid. The result is as follows.
The painter is painting!
The typist is typing!
The manager is in a meeting!
As shown in the preceding example, no matter how many employee classes are added, you only need to implement the employee class and method inherited from the employee parent class. You do not need to modify the printworking () method to display the employee's working status ().

Bytes. Different objects can produce different results when they receive the same message...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.