In-depth implementation of PHP polymorphism

Source: Internet
Author: User

Original link: http://www.poluoluo.com/jzxy/201306/215056.html

Polymorphism refers to the same operation or function, the process can be used on multiple types of objects and obtain different results. Different objects that receive the same message can produce different results, a phenomenon called polymorphism.

Polymorphism allows each object to respond to a common message in a way that suits itself. Polymorphism enhances the flexibility and reusability of software.

In object-oriented software development, polymorphism is one of the most important parts. Object-oriented programming is not just a simple combination of related methods and data, but rather the use of various elements in object-oriented programming to clearly describe the various situations in real life. This section explains the polymorphism of object-oriented programming in detail.

1. What is polymorphic

Polymorphism (polymorphism) is literally understood as "multiple shapes." Can be understood as a variety of manifestations, also known as "an external interface, a number of internal implementation methods." In object-oriented theory, the general definition of polymorphism is that the same operation acts on different instances of the class, resulting in different execution results. That is, when objects of different classes receive the same message, they will get different results.

In the actual application development, the use of multi-state in object-oriented is mainly that the different sub-class objects can be treated as a parent class, and can block the differences between different sub-class objects, write out common code, and make general programming to adapt to the changing requirements.

2. Multi-state application design

In the actual application development, in order to make the project can be easily extended and upgraded in the later time, it is necessary to upgrade easily by inheriting and implementing reusable modules. In the design of reusable modules, it is necessary to reduce the use of process control statements as much as possible. In this case, you can implement this type of design with polymorphism.

The example examples illustrate the process of implementing different classes with flow control statements. its code is shown below.

<?PHPclasspainter{//defining the Painter class Public functionPaintbrush () {//define painter ActionsEcho"The Painter is painting!" /n ";}}classtypist{//defining the Typist class Public functionTyped () {//Define typist WorkEcho"The typist is typing!" /n ";}}functionPrintworking ($obj){//Defining processing Classesif($objinstanceof painter) {//if the object is a painter class, the painter action is displayed$obj-paintbrush ();}ElseIf($objInstanceof Typist) {//if the object is a typist class, the typist action is displayed$obj-typed ();}Else{//if it is not the above class, an error message is displayedEcho"Error: Object is wrong! ";}} Printworking (Newpainter ());//Show Employee WorkPrintworking (NewTypist ());//Show Employee Work?>

Analysis: in the above program, first define two employee classes: Painter class and Typist class. Then define a handler function that determines whether the employee is a defined employee and prints out the employee's working status. The results are as follows.
The painter is brushing the paint
The typist is typing.
It is easy to see from the above program that if you want to show the working status of several of its employees, you need to define the employee class first, define the employee's work in the employee class, and then add the ElseIf statement in the printworking () function to check which instance of the employee class the object is. This is very undesirable in practical applications. If you use polymorphism at this time, you can easily resolve this problem.

You can first create an employee parent class, and all employee classes inherit from the employee parent class and inherit all the methods and properties of the parent class. Then create a "is a" relationship in the employee class to determine whether it is a legitimate employee.

The sample example uses a polymorphic approach to rewrite the previous example . Its code is shown below.

<?PHPclassemployee{//Define employee Parent classprotected functionWorking () {//Define employee work that needs to be implemented in subclassesEcho"This method needs to be overloaded in subclasses!";}}classPainterextendsemployee{//defining the Painter class Public functionWorking () {//working methods to implement inheritanceEcho"The Painter is painting!" /n ";}}classTypistextendsemployee{//defining the Typist class Public functionworking () {Echo"The typist is typing!" /n ";}}classManagerextendsemployee{//define the manager class Public functionworking () {Echo"The manager is in a meeting!" ";}}functionPrintworking ($obj){//Defining processing Methodsif($objInstanceof Employee) {//If the Employee object, displays its working status$obj-working ();}Else{//Otherwise, an error message is displayedEcho"Error: Object is wrong! ";}} Printworking (Newpainter ());//show Painter's workPrintworking (NewTypist ());//Show the work of a typistPrintworking (NewManager ());//Show Manager's work?>

Analysis: In the above program, first define an employee base class, and define an employee's working state 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 that displays the employee's working status. and create a "is a" relationship in the method that determines whether the employee is legitimate. The results are as follows.
The painter is brushing the paint!
The typist is typing!
The manager is in a meeting!
From the above example, you can see that no matter how many employee classes you add, you only need to implement that employee class and method inherited from the employee parent class. Without modifying the method printworking () that shows the employee's working status.

In-depth implementation of PHP polymorphism

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.