What is polymorphic? PHP Object-oriented multi-state example tutorial

Source: Internet
Author: User
What is polymorphism?

Polymorphism is the third feature of object-oriented language following the abstraction and inheritance of database. Polymorphism is a variety of forms, with the ability to express a variety of forms of characteristics. Representation in an object-oriented manner is handled differently depending on the type of object. Polymorphism allows each object to respond to a common message in a way that suits itself. Polymorphism enhances the flexibility and reusability of software.

If we create a doing () method, if the student prints the class, the company clerk prints to work.

General Practice

Use if to determine

/** * PHP polymorphism *//define Student class student{    public Function cla () {            echo "student worker is in class!<br/>";    }}//define staff class of fice{Public                                      function Wor () {            echo "clerk is working!<br/>";        }}//Judging object type method function doing ($obj) {         if ($obj instanceof student) {             $obj->cla ();        } ElseIf ($obj instanceof Office) {            $obj->wor ();        } else{            echo "No this object!" ";        }} Doing (new student ()); Students are in class doing (new Office ());  The clerk is at work

Above results output:

The students are having a class
The clerk is at work

This common method has a disadvantage, that is, if a lot of objects, then if. else.. Very long and inflexible.

Polymorphism practices

Defines a public abstract method that inherits it from all subclasses.

/** * PHP polymorphism *//define a common class pub{    protected function working () {        echo "This method needs to be overloaded in subclasses!;    }}//Define student class, Inherit public class PUBC Lass Student extends pub{    public Function working () {        echo "student worker is in class!<br/>";    }}//Define employee class, inherit common class Pubclass Office extends pub{public        function working () {        echo "clerk is working!<br/>";}        }//Judging object type method function doing ($ obj) {         if ($obj instanceof Pub) {             $obj->working ();        } else{            echo "No this object!" ";        }} Doing (new student ()); Students are in class doing (new Office ());  The clerk is at work

This is a polymorphic feature that is flexible to reuse.

Other practices

From the way of realization of polymorphism, we can standardize the method of each class to overload the parent class, so as to achieve uniform effect. And when we define classes, it is possible to add a uniform approach to our own. So the above example can be implemented as well:

/** * PHP polymorphism *///Definition of class student{//define    a uniform method Pub public    function pub () {        echo "Student work is in class!<br/>";    }}//define staff Class office{    //define Uniform method Pub public        function pub () {        echo "clerk is working!<br/>";        }}// Determine the object type method function doing ($obj) {     if ($obj) {        //Call class Uniform method            $obj->pub ();    } else{        Echo ' does not have this object ';      }} doing (new student ());//students are attending classes doing (new Office ());  The clerk is at work

Polymorphism can also be understood as a programming approach, and the ultimate goal of programming is: flexible, polymorphic, reusable, efficient.

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.