Simple use of the concept of PHP interface

Source: Internet
Author: User

Interface is an important feature of object-oriented, but also an indispensable concept of object-oriented development, the following is a simple concept of the interface, first look at a simple code:

1 InterfaceIcaneat {2     Public functionEat$food);3 }4 5 //the human class implements the Icaneat interface6 classHumanImplementsIcaneat {7    Public functionEat$food){8     Echo"Human eating".$food. "<br/>";9   }Ten } One  A //the animal class also implements the Icaneat interface - classAnimalImplementsIcaneat { -    Public functionEat$food){ the     Echo"Animal eating".$food. "\ n"; -   } - } -  + //different classes can implement the same interface, defining different implementations of interface methods - $man=NewHuman (); + $man->eat ("Apple"); A $monkey=NewAnimal (); at $monkey->eat ("Banana"); -  -  - //A class that implements an interface must provide a method defined in the interface -  - //Cannot create an object with an interface, but can tell if an object implements an interface in //$EATOBJ = new Icaneat (); - Var_dump($manInstanceof Icaneat);//determine if an object implements an interface to  + functionCheckeat ($obj){ -   //defines a method to determine whether an object implements an interface the   if($objinstanceof Icaneat) { *     $obj->eat (' Apple '); $}Else{Panax Notoginseng     Echo"The Cant ' t eat"; -   } the } +  ACheckeat ($man); theCheckeat ($monkey); +  - //interfaces can inherit interfaces $ InterfaceIcandrinkextendsIcaneat { $    Public functiondrink (); - } -  the classHuman1Implementsicandrink{ -    Public functionpee () {}Wuyi    Public functionEat$food){} the}

Code is messy, the focus is a little bit, first we define an interface icaneat, the definition of the statement is interface icaneat{}, which can be defined in some way, these methods must be public common type, and this method is not specific implementation, Next define the two classes human and animal, all implement the Icaneat this interface, is all can eat, but the specific eating method is not the same, this is the meaning of the interface, that is, define some common methods many classes are implemented at the same time, but the implementation of the method is not the same, The purpose of the interface is to note that all the methods defined in the interface must be fully implemented in the class that implements the interface, and not several of them can be implemented, and the Checkeat method is to determine whether an object implements an interface.

In addition, it is not possible to create an object with an interface, or it is wrong to say that the class implements the interface and the object implements the interface, which is an implementation relationship

Interfaces can inherit the interface, such as Icandrink can drink, although not too suitable, the statement is: interface Icandrink extends icaneat{} This defines the method drink, so that the implementation of the interface inheritance

Then defines the class Human1 implements the Icandrink interface, then this class must implement the Icandrink interface and all the methods in the parent interface Icaneat, that is, the implementation interface must implement all the methods in the interface and all the methods of the parent interface

Said the more chaotic, but the most central place is to understand the role and meaning of the interface, it is to provide some unified way to describe different objects, different objects have their specific implementation, basically these things.

Simple use of the concept of PHP interface

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.