PHP interface entry-level analysis example

Source: Internet
Author: User
Tags iusb
PHP interface Entry Analysis example 1. Quick Start case:

Case 1:

    
new Camera();  $camera1->start();  $phone1=new Phone();  $phone1->start();?>

Effect:

Camera started working mobile phone started working

From

2. basic syntax used by the interface:

Interface name

{

// Attributes

// Method

}

-> The method in the interface does not have a method body.

-> How to implement interfaces

Class name implements interface name

{

}

** Multiple interfaces can be implemented.

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

{

}

-> Interface function: declare some methods for implementation by other classes. The interface also reflects the expected effect in programming: high cohesion and low coupling. for details, see

In-depth discussion-interface

When to use interfaces?

1. set specifications

2. set standards for other programmers to implement

3. when there are multiple classes in a level relationship between them, all these classes need to practice a function, but the implementation method is different.

Details-interface

1. the interface cannot be instantiated;

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

3. a class can implement multiple interfaces, but are separated by commas;

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

Case 2:

  The inherited constant is :". isUSB: A; // constant reference method: Interface name: constant name }}$ test1 = new Test (); $ test1-> aa (); echo"
The constant that is not inherited is: ". IsUSB: A; // constant reference method: Interface name: constant name?>

5. the interface methods are all public, that is, there is no protected and private. if not written, the default value is public;

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

Example: inerface interface name: extends interface name 1, interface name 2 ...... 2


Case 3:

  ";}} Class Test extends Class1 implements IsUSB03 {public function _ construct () {echo" interface IsUSB03 inherits interface IsUSB01 and interface IsUSB02
";
} Public function a () {echo "implements a () of the IsUSB01 interface ()
";} Public function B () {echo" implements B () of the IsUSB02 interface ()
";}}$ Test1 = new Test (); $ test1-> a (); $ test1-> B (); $ test1-> TST ();?>

Result:

The interface IsUSB03 inherits the interface IsUSB01 and the interface IsUSB02

Implement a () of the IsUSB01 interface ()

B () implementing the IsUSB02 interface ()

The inherited class name is Class1.

From

Among them, the interface IsUSB03 inherits IsUSB01 and IsUSB02, because it is an interface inherited interface, there is no need to implement the method, and the Test class references the IsUSB03 interface, you need to implement the method.

3. differences between inheritance and interfaces

The implementation interface can be seen as a supplement to a single inheritance class. at the same time, inheritance is hierarchical and not flexible. multi-level inheritance will break the inheritance balance if a class is modified, the interface is not so troublesome and flexible.

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.