Follow Baidu Learn Php[4]oop Face Object programming -12-Object Interface Technology (interface)

Source: Internet
Author: User
Tags learn php

Like most object-oriented programming languages, PHP does not support multiple inheritance. This means that each class can inherit only one parent class .

Interface It is the problem of solving that each class can inherit only one parent class.

What permissions the interface uses, and what permissions are used by the inherited method.

The declaration of the interface is used: interface

Interface inheritance using: Implements

Catalogue ++++++++++++++++++++++++++++++++++++++++++++

Declaration of the 00X1 interface and reference to the interface (case i)

00x2 how to inherit multiple interfaces (case two)

00X3 determine if an object implements an interface (case three)

++++++++++++++++++++++++++++++++++++++++++++++

00x1 Case One

<?PHPInterfaceicaneat{#使用interface声明一个接口 Publicfunction Eat ($food);//interface does not require the implementation of a method}classHuman implements icaneat{#使用implements继承接口 (cannot use extends), the properties or methods that will be docked to the interface later are consistent.  Publicfunction Eat ($food) {#如该行所示, to be consistent with the properties or methods of the interface. Echo"I eating". $food."\ n";    Once you have implemented an interface, you must provide a concrete implementation of the methods defined in the interface. }23} $test=NewHuman (); $test->eat ('Apple'); ?>
The output effect is as follows:
I eating apple

00x2 Case Two

The inheritance of multiple interfaces can be achieved by separating them with commas after implments.

Format:

Implements interface 1, interface 2,....

<?PHPInterfaceicaneat{ Publicfunction Eat ($food);}Interfacehello{ Publicfuction Hello ($nihao);}classHuman implements icaneat,hello{#使用逗号隔开即可实现对多个接口的继承 Publicfunction Eat ($food) {echo"I eating". $food."\ n"; }} $test=NewHuman (); $test->eat ('Apple'); ?>

00x3 Case Three

Using instanceof keyword Test

Example: Var_dump ($object instanceof Hello); #对象 $object Whether the Hello interface is implemented

<?PHPInterfacetest{ Publicfunction one ($a);}Interfacetest2{ Publicfunction ($b);}classChengdaniu implements test,test2{ Publicfunction One ($a) {echo"I love Web security technology! "; }     Publicfunction ($b) {echo"I want to become Daniel! "; }} $shi=NewChengdaniu (); Var_dump ($shi instanceof test);?>
The output effect is as follows:
Boolean true

The END

Follow Baidu Learn Php[4]oop Face Object programming -12-Object Interface Technology (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.