Deep into PHP polymorphism to realize _php skills

Source: Internet
Author: User
Tags class manager
Polymorphism refers to the same operations or functions, processes that can be used on multiple types of objects and obtain different results. Different objects, receiving 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.

Polymorphism is one of the most important parts in object-oriented software development. Object-oriented programming is not simply a combination of relevant methods and data, but rather uses various elements of object-oriented programming to clearly describe the various situations in real life. This section will give a detailed explanation of polymorphism in object-oriented programming.

1. What is polymorphism

Polymorphism (polymorphism) is literally understood to mean "multiple shapes." Can be understood as a variety of manifestations, that is, "an external interface, a number of internal implementation methods." In object-oriented theory, polymorphism is generally defined as: the same operation acts on an instance of a different class, which produces different execution results. Also, when objects of different classes receive the same message, different results are obtained.

In the actual application development, the use of object-oriented polymorphism is mainly to be able to treat different subclass objects as a parent class, and can mask the differences between the different subclass objects, write common code, make general programming, to adapt to the changing needs.

2. Multi-state application design

In the actual application development, in order to enable the project to be able to expand and upgrade easily in the later time, it is necessary to implement the reusable module for easy upgrade through inheritance. When designing a reusable module, you need to minimize the use of Process control statements. In this case, the design can be implemented by polymorphism.

The sample example lifts a Process Control statement to implement different classes of processing. The code looks like the following.
Copy Code code as follows:

<?php
Class painter{//Definition painter class
Public Function Paintbrush () {//define painter action
echo Painter is painting! /n ";
}
}
Class typist{//Definition of typists
Public function typed () {//Definition of typist work
echo "The typist is typing! /n ";
}
}
function printworking ($obj) {//Define processing class
if ($obj instanceof painter) {//If the object is a painter class, show painter action
$obj->paintbrush ();
}elseif ($obj instanceof Typist) {//If the object is a typist, show the typist action
$obj->typed ();
}else{//Unless the above class is displayed, an error message appears
echo "Error: Object Errors!" ";
}
}
Printworking (new painter ()); Show Employee work
Printworking (New typist ()); Show Employee work
?>

Analysis:In the above procedure, first define two employee class: Painter class and Typist class. Then define a handler function in which to determine whether an employee is a defined employee, and print out the employee's working status. The results are shown below.
The painter is painting the paint
The typist is typing.
As you can easily see from the above program, if you want to show the working 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 the ElseIf statement to the printworking () function to check which employee class The object is an instance of. This is very undesirable in practical applications. If you use polymorphism at this point, you can easily resolve this problem.

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

The example examples illustrate the use of polymorphism to rewrite the example。 The code looks like the following.
Copy Code code as follows:

<?php
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{//define painter class
Public Function Working () {//Implementation inheritance method of work
echo Painter is painting! /n ";
}
}
Class Typist extends employee{//definition typist
Public Function working () {
echo "The typist is typing! /n ";
}
}
Class Manager extends employee{//definition Manager class
Public Function working () {
Echo Manager is in the meeting! ";
}
}
function printworking ($obj) {//Definition processing method
if ($obj instanceof employee) {//If Employee object, display its working status
$obj->working ();
}else{//Otherwise display error message
echo "Error: Object Errors!" ";
}
}
Printworking (new painter ())//show painter's work
Printworking (New typist ());//show the work of the typist
Printworking (new manager ());//Show Manager's work
?>

Analysis: In the above program, first define an employee base class, and define a method of employee working status. Then define the three employee classes that will inherit from the employee base class: Painters, typists, and managers. Then define a method that shows the employee's working status. and create a "yes" relationship 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 having a meeting!
From the example above, it is found that no matter how many employee classes you add, you only need to implement the employee class and methods that are inherited from the employee parent class. Without modifying the method that displays the employee's working status printworking ().

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.