Object-oriented PHP (iv)

Source: Internet
Author: User

    The /*** interface defines the common behavior of different classes and then implements different functions within different classes. * Because the method implementation of the interface can be many, so the implementation of the method defined in the interface is various, this characteristic we call polymorphic * /Header"content-type:text/html; Charset=utf-8 ");//Remove Chinese garbled characters//interface keyword for defining interfaces interfaceicaneat{ Public functioneat($food);}The //implements keyword is used to indicate that a class implements an interface classHumanimplementsicaneat {//Implement an interface, you must provide a concrete implementation of the methods defined in the interface Public functioneat($food){Echo"Human::eat ()".$food."
"; } } classAnimalimplementsicaneat {//Implement an interface, you must provide a concrete implementation of the methods defined in the interface Public functioneat($food){Echo"Animal::eat ()".$food."
"; } }$pzy=NewHuman ();$pzy->eat ("Big Watermelon");$dog=NewAnimal ();$dog->eat ("big bones.");//instanceof is used to determine whether an object implements an interfaceVar_dump ($doginstanceofIcaneat); functioncheckeat($obj){if($objinstanceofIcaneat) {$obj->eat (' food '); }Else{Echo"The obj can ' t eat.
"; } }//The same line of code, for the implementation of the different interfaces of the object, the performance of different, called polymorphicCheckeat ($pzy); Checkeat ($dog);//Use extends to let interface inherit interface interfaceicanflyextendsicaneat{ Public functionfly();}//When a class implements a Subinterface, the method defined by the parent interface is also implemented in this class classHuman1implementsicanfly{ Public functionfly(){} Public functioneat($food){}}?>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the object-oriented PHP (iv), including aspects of the content, I hope that the PHP tutorial interested in a friend to help.

  • 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.