PHP Interface Primer Analysis Example

Source: Internet
Author: User
Tags iusb
1. Quick Start Case:

Case 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

2. Basic syntax used by the interface:

Interface Interface Name

{

Property

Method

}

The method in the interface has no method body

How to implement an interface

Class name implements Interface name

{

}

* * Multiple interfaces can be implemented

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

{

}

The role of interface: declaring some methods for other classes to implement, the interface also embodies the effect we want in programming: cohesion Poly low coupling characteristics, see Figure 1

In-depth discussion-interface interface

When do I 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:

 
   The inherited constants are: ". ISUSB::A;//constant Reference Method: interface Name:: Constant Name  }} $test 1=new Test (); $test 1->aa ();  echo "
The constant that does not inherit is: ". ISUSB::A;//constant Reference Method: interface Name:: Constant name?>

5. The methods of the interface are public, that is, there is no protected, and private, if not written, the default is 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, and a class can inherit a class at the same time to implement the interface;

For 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 the interface IsUSB01 and interface IsUSB02
";
    the Public Function A ()  {echo "implements the interface IsUSB01 a ()
"; } Public Function B () { echo "implements interface IsUSB02 B ()
"; } } $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

Where interface IsUSB03 inherits IsUSB01 and IsUSB02, because it is an interface inheritance interface, no implementation method is required, and the test class references the IsUSB03 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

  • 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.