PHP Interface Primer Analysis Example

Source: Internet
Author: User
Tags iusb

1. Quick Start case :

Case 1 :

<?php//Definition specification/Method  interface Iusb { public  Function start ();  public function stop ();  } //write the phone class and let it implement the interface  //when a class implements an interface, Requires that the class must implement all methods of this interface   class Camera implements Iusb  {  public  Function start ()   { echo  "camera starts working";  }  public function  Stop ()   { echo  "camera stopped working";  }  }   class phone  Implements iusb  {  public function start ()   { echo  " Mobile phone to work ";   }  public function stop ()   { echo " phone stop working ";   }  }  //How to use    $camera 1= 
New Camera ();  $camera 1->start ();  $phone 1=new Phone (); $phone 1->start ();? >

effect :

The camera starts working and the phone starts to work

from < http://localhost/interface/interface01.php >

2. Basic syntax used by the interface :

Interface Interface name

{

   // Properties

  // Method

}

- the method in the interface has no method body

- How to implement an interface

Class class name implements Interface name

{

}

** can implement multiple interfaces

Class class name implements Interface name 1, interface name 2 ...

{

}

-interface offunction:declare some methods,for other classes to implement, the interface also embodies the effect we want in programming: cohesion Poly-Low coupling the characteristics , seeFigure1

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/80/4F/wKioL1c9v4ugprceAAA3wqxS8v0320.png "title=" Capture 1. PNG "alt=" Wkiol1c9v4ugprceaaa3wqxs8v0320.png "/>

In- depth discussion - Interface Interface

when to use the interface ?

1. Fixed Specifications

2. set up the specification , let other program person to realize

3. when more than one class , They have a lateral relationship , These classes are going to intern a function , but the implementation of different ways

Details -interface

1. interfaces cannot be instantiated;

2. all methods in the interface cannot have a method body;

3. A class can implement multiple interfaces , but separated by "," ;

4. the interface can have attributes (different from methods), but must be constant and public

Case 2 :

<?php interface isusb { const  a=555;// No $ } class test implements  isusb{    public  required in front of constants FUNCTION&NBSP;AA ()   { echo   "<br> inherited constants are:". ISUSB::A;//constant Reference Method: interface Name:: Constant name   }} $test 1=new test (); $test 1->aa ();  echo  "<br > The constant that does not inherit is: ". ISUSB::A;//constant Reference Method: interface Name:: constant name; 

5. Span style= "Font-family:simsun;" lang= "ZH-CN" xml:lang= "ZH-CN" > interfaces are public protected private If not written, the default is Span style= "FONT-FAMILY:CALIBRI;" lang= "en-us" xml:lang= "en-US" >public

6. interfaces cannot inherit other classes, but can inherit other interfaces, and an interface can inherit multiple interfaces, and a class cannot inherit multiple classes ; A class can inherit a class at the same time to implement the interface;

 For example:Inerface Interface NameextendsInterface Name1,Interface Name2 ...asFigure2


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/80/52/wKiom1c9vo3wTmPeAABRMnUauig363.png "title=" capture. PNG "alt=" Wkiom1c9vo3wtmpeaabrmnuauig363.png "/>

Case 3 :

<?php interface isusb01 { public function  a ();  } interface isusb02 { public function b (); }   interface isusb03 extends  isusb01,isusb02{   }class  class1{ &NBSP;&NBSP;PUBLIC&NBSP;FUNCTION&NBSP;TST ()   { echo  "Inheriting class name class1<br>";   }}  class Test extends Class1 implements IsUSB03 {  public  Function __construct ()   { echo  "interface IsUSB03 inherits Interface IsUSB01 and Interface isusb02<br>"; 
} public Function A () {echo "Implements the interface IsUSB01 a () <br>";   } public Function B () {echo "Implements the interface IsUSB02 B () <br>"; }} $test 1=new Test (); $test 1->a (); $test 1->b (); $test 1->tst ();?>

Results:

Interface IsUSB03 inherits the interface IsUSB01 and interface IsUSB02

Implement Interface IsUSB01 A ()

Implement the interface IsUSB02 B ()

Inheriting the class name Class1

from < http://localhost/interface/interface04.php >

where the interface IsUSB03 inherits the IsUSB0 1 and IsUSB0 2 , because it is an interface inheritance interface, there is no need to implement the method, and, Test class references the IsUSB0 3 interface, you need to implement the method.

3. differences between inheritance and interfaces

Implementation interface can be seen as a complement to a single inheritance class, while inheritance is hierarchical, less flexible, multi-level inheritance If you modify a class, it will break the balance of inheritance, and the interface is not so troublesome, more flexible


This article is from "Uncle Wei Xiaobao" blog, please be sure to keep this source http://darmi.blog.51cto.com/11607923/1775173

PHP Interface Primer Analysis 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.