PHP Multi-State understanding

Source: Internet
Author: User
Tags class manager

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.

Class painter{//Definition painter class
Public Function Paintbrush () {//define painter actions
echo "Painter is painting!" \ n ";
}
}

Class typist{//define Typist Class
Public function typed () {//define typist work
echo "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 the above class is not present, an error message is displayed
echo "Error: Object wrong! ";
}
}

Printworking (new painter ()); Show Employee work
Printworking (New typist ()); 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.

Class employee{//define Employee parent class
protected function Working () {//define employee work, need to implement in subclass
echo "This method needs to be overloaded in subclasses!";
}

}

class Painter extends employee{//definition painter class
Public Function Working () {//Implement inherited working methods
echo "Painter is painting!" \ n ";
}
}

Class Typist extends employee{//defining typist classes
Public Function working () {
echo "Typist is typing! \ n ";
}
}

Class Manager extends employee{//defining manager classes
Public Function working () {
The Echo Manager is in a meeting! ";
}
}

function printworking ($obj) {//define processing method
if ($obj instanceof employee) {//If the Employee object, displays its working status
$obj->working ();
}else{//otherwise displays an error message
echo "Error: Object wrong! ";
}
}

Printworking (new painter ());//show Painter's work
Printworking (New typist ());//Display the work of a typist
Printworking (new manager);//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.

PHP Multi-State understanding

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.