PHP polymorphic Simple Example

Source: Internet
Author: User

<?PHP//Polymorphism is performed by different people to produce different results://A person performs the same action in different States, producing different results can also be called polymorphism//polymorphic principle, that is, the class is written well, do not modify it, as long as the call parameters outside the class Change//Excuse specification of the subclass of the action 
        Interfacecomputer{ Public functionVersion ();//This method indicates what method to use         Public functionWork ();//how it works.    }    //implement an excuse, a notebook Class A desktop class    classNotecomputerImplementscomputer{ Public functionversion () {EchoLaptop; }         Public functionWork () {Echo' Run Win7 '; }    }    classDesktopcomputerImplementscomputer{ Public functionversion () {EchoDesktop; }         Public functionWork () {Echo' Run XP '; }    }    //Create a user to use your computer    classperson{//Create a method to accept the computer         Public function_run ($type){            Echo' The person's '; $type-version (); $type-Work (); }    }    $note=NewNotecomputer (); $desktop=NewDesktopcomputer (); $person=NewPerson (); $person->_run ($note);//object references are passed, holding each other's references    Echo' <br/> '; $person->_run ($desktop);?>

PHP polymorphic Simple Example

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.