Examples of PHP object-oriented Polymorphism

Source: Internet
Author: User
This article mainly introduces the example of PHP object-oriented polymorphism. This article uses examples to explain how PHP polymorphism and the benefits of polymorphism can help you fully understand polymorphism, for more information, see

This article mainly introduces the example of PHP object-oriented polymorphism. This article uses examples to explain how PHP polymorphism and the benefits of polymorphism can help you fully understand polymorphism, for more information, see

What is polymorphism?

Polymorphism is the third feature of object-oriented language after database abstraction and inheritance. Polymorphism is a variety of forms, with the ability to express a variety of forms. Objects are processed in different ways based on the object type. Polymorphism allows each object to respond to common messages in a suitable way. Polymorphism enhances software flexibility and reusability.

For example, if we create a doing () method, if it is a student, the class is printed, and if it is a company employee, the work is printed.

Common Practice

Use if to judge

The Code is as follows:


/**
* PHP Polymorphism
* Qiongtai blog
*/

// Define the Student Category
Class student {
Public function linoleic (){
Echo "students are attending classes!
";
}
}

// Define employee class
Class office {
Public function Wor (){
Echo "staff at work!
";
}
}

// Method for Determining Object Types
Function doing ($ obj ){
If ($ obj instanceof student ){
$ Obj-> linoleic ();
} Elseif ($ obj instanceof office ){
$ Obj-> wor ();
} Else {
Echo "No such object! ";
}
}

Doing (new student (); // students are attending classes
Doing (new office (); // The employee is at work

The above result is output:

Students are attending classes
Staff at work

This common method has a disadvantage, that is, if there are many objects, then if... else... is very long and not flexible.

Polymorphism

Defines a public abstract method, and all subclasses inherit it.

The Code is as follows:


/**
* PHP Polymorphism
* Qiongtai blog
*/

// Define a public class
Class pub {
Protected function working (){
Echo "This method needs to be reloaded in the subclass! ";
}
}

// Define the student class and inherit from the public class pub
Class student extends pub {
Public function working (){
Echo "students are attending classes!
";
}
}

// Defines the staff class and inherits the public class pub
Class office extends pub {
Public function working (){
Echo "staff at work!
";
}
}

// Method for Determining Object Types
Function doing ($ obj ){
If ($ obj instanceof pub ){
$ Obj-> working ();
} Else {
Echo "No such object! ";
}
}

Doing (new student (); // students are attending classes
Doing (new office (); // The employee is at work


This is the characteristic of polymorphism and flexible reuse.

Other practices

From the implementation of polymorphism, it is nothing more than standardizing a method of the parent class for every class to achieve a unified effect. It is also feasible to add a unified method when defining classes. The preceding example can also be implemented as follows:

The Code is as follows:


/**
* PHP Polymorphism
* Qiongtai blog
*/

// Define the Student Category
Class student {
// Define a uniform method pub
Public function pub (){
Echo "students are attending classes!
";
}
}

// Define employee class
Class office {
// Define a uniform method pub
Public function pub (){
Echo "staff at work!
";
}
}

// Method for Determining Object Types
Function doing ($ obj ){
If ($ obj ){
// Call the unified method of the class
$ Obj-> pub ();
} Else {
Echo 'no such object ';
}
}

Doing (new student (); // students are attending classes
Doing (new office (); // The employee is at work


Of course, the above examples do not show that polymorphism can be done in this way. After all, polymorphism can effectively achieve flexible reuse in complex programming.

Polymorphism can also be understood as a programming method, and the ultimate goal of programming is flexibility, polymorphism, reuse, and efficiency.

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.