PHP class and object interface definition Introduction and usage examples

Source: Internet
Author: User
Tags php class
Implementation of the interface

The <?php//interface keyword is used to define an interface interface icaneat{       //interface inside the method does not need to implement public       function eat ($food);} The Implements keyword is used when a class implements an interface class Human implements icaneat{       //Implements an interface and must provide a concrete implementation of the method in the interface public       function eat ($food) {       echo "Human eating". $food;    }? >

So, after the definition of this pass,

By instantiating an object

$peroson 1=new Human ();

$person 1->eat (' Apple ');

The result of the output is

Human eating apple

The <?php//interface keyword is used to define an interface interface icaneat{       //interface inside the method does not need to implement public       function eat ($food);} The Implements keyword is used when a class implements an interface class Human implements icaneat{       //Implements an interface and must provide a concrete implementation of the method in the interface public       function eat ($food) {       echo "Human eating". $food. " \ n ";    }} $person 1 = new Human (), $person 1->eat (' Apple ');//We can determine whether an object implements an interface Var_dump ($person 1 instanceof by instanceof keyword) Icaneat);//In the example above we can use this keyword to determine whether the Icaneat interface is implemented?>

Then the output should be

Human eating apple

Bool (True)

The <?php//interface keyword is used to define an interface interface icaneat{//interface methods do not need to implement public function eat ($food);} The Implements keyword is used for classes that implement an interface class Human implements icaneat{//implement an interface, you must provide a concrete implementation of the method in the interface public function eat ($food) {echo "Human Eating ". $food." \ n ";}} $person 1 = new Human (), $person 1->eat (' Apple ');//We can determine whether an object implements an interface Var_dump ($person 1 instanceof by instanceof keyword) Icaneat);//In the above example, we can use this keyword to determine whether the implementation of Icaneat this interface//class can inherit, then the interface can also be inherited interface Icansleep extends icaneat{public function sleep ();} When a class implements a Subinterface, the method defined by the parent interface also needs to be implemented in the class to specifically implement class Human1 implements icansleep{public function eat ($food) {} Public Function sleep () { }}?>

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.