Understanding of PHP Polymorphism

Source: Internet
Author: User
Tags class manager
This article mainly introduces the understanding of PHP polymorphism, has a certain reference value, now share to everyone, the need for friends can refer to

PHP polymorphic

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

Example example uses a Process control statement to implement different classes of processing. Its code is shown below.

Class painter{//define painter class public       function Paintbrush () {                 //define painter action           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, the error message           echo "Error: Object Error! ";       }   } 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 echo in subclass       "This method needs to be overloaded in subclasses!";   }} class Painter extends employee{//defines the painter class public   function working () {//How to implement inheritance working methods       echo painter is painting! \ n ";   }} Class Typist extends employee{//defines the Typist class public   function working () {       echo "typist is typing! \ n ";   }} Class Manager extends employee{//defines the manager class public   function working () {       echo Manager is in a meeting! ";   }} function printworking ($obj) {//define the processing method   if ($obj instanceof employee) {///If the Employee object, displays its working status       $obj->working ();   } else{//otherwise displays the error message       echo "Error: Object Error! ";   }} Printworking (new painter ());//display painter's work printworking (New typist ());//Show Typist work 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.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related recommendations:

Introduction to PHP File programming

Introduction to PHP Basic syntax

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.