Application (interfaces and implements) of interfaces in PHP object-oriented development)

Source: Internet
Author: User

The application Keywords of interfaces in php are interface and implements. interfaces are special abstract classes whose member attributes are all abstract or constants. Let's look at several instances.

Application of interfaces in the class

1. Keyword: interface
2. Keyword: implements

1. Interface introduction and Creation

Interface: A special abstract class in which all member attributes are abstract or constants.

Rules:

1. All classes are abstract methods.
2. abstract methods do not need to be added.
3. The interface abstract method property is public.
4. The member attribute must be a constant.

Format:

The Code is as follows: Copy code

Interface demo {
Const NAME = "constant object attributes ";

Function fun1 ();
Function fun2 (); // abstract method.
}


2. Application and specification of interfaces

Interface reference is different from the class inheritance keyword extends. inheritance can only be single, and interfaces can use the keyword implements to separate multiple references with commas.

1. Format: Common class reference Interface

Class MyPc implements demo, demo2, demo3 {
...
}

2. Format: Example of an abstract class application interface

Abstract class MyPc implements demo, demo2, demo3 {
...
}

3. Format: Inherit the coexistence of parent class reference Interfaces

Class MyPc extends Root implements demo, demo2, demo3 {
...
}

The interface is inherited first, and the interface is inherited by multiple interfaces.

4. Format: interface and interface inheritance

Interface demo3 extends demo {
...
}

Instance:

The Code is as follows: Copy code

<? Php
Interface demo {
Const NAME = "NAME ";
Function fun1 ();
Function fun2 ();
}

Interface demo2 {
Function fun3 ();
Function fun4 ();
}

Interface demo3 {
Const TEST = "Test ";
Function fun5 ();
}

Class MyPc implements demo, demo2 {
Function fun1 (){
Echo "++ <br/> ";
}
Function fun2 (){
Echo "---------- <br/> ";
}
Function fun3 (){
Echo "1111111111 <br/> ";
}
Function fun4 (){
Echo "2222222222 <br/> ";
}
}

Class MyPs extends MyPc implements demo3 {
Function fun5 (){
Echo "references interfaces after inheriting classes ";
}
}
$ P = new MyPs;
$ P-> fun1 ();
$ P-> fun2 ();
$ P-> fun3 ();
$ P-> fun4 ();
$ P-> fun5 ();
?>

Example

The interface is defined by the keyword interface, and the keyword implements is used to implement the methods in the interface, and must be fully implemented.

Example:

The Code is as follows: Copy code

<? Php
// Define the interface
Interface User {
Function getDiscount ();
Function getUserType ();
}
// VIP User Interface implementation
Class VipUser implements User {
// VIP user discount coefficient
Private $ discount = 0.8;
Function getDiscount (){
Return $ this-> discount;
}
Function getUserType (){
Return "VIP user ";
}
}
Class Goods {
Var $ price = 100;
Var $ vc;
// Define the User interface type parameter. At this time, you do not know who the User is.
Function run (User $ vc ){
$ This-> vc = $ vc;
$ Discount = $ this-> vc-> getDiscount ();
$ Usertype = $ this-> vc-> getUserType ();
Echo $ usertype. "product price:". $ this-> price * $ discount;
}
}

$ Display = new Goods ();
$ Display-> run (new VipUser); // It can be more user types
?>

Run this example and output:

VIP user product price: 80 RMB

This example demonstrates a simple application of a PHP interface. In this example, the User interface achieves the User discount, while the VipUser class implements the specific discount coefficient. Finally, Goods provides different User quotations based on the User interface.

This example only demonstrates the usage of the PHP interface and does not involve science or not.

Implement multiple interfaces

PHP can also implement multiple interfaces when inheriting a class:

Class subclass extends parent class implemtns interface 1, interface 2 ,...
{
......
}


Differences between abstract classes and interfaces


An interface is a special abstract class and can also be seen as a model specification. The interface and abstract class are roughly different as follows:

1. If a subclass implements an interface, all methods in the interface must be implemented (whether or not required); if an abstract class is inherited, you only need to implement the required methods.
2. if the method name defined in an interface is changed, all subclasses implementing this interface must synchronously update the method name. If the method name in the abstract class is changed, the method name corresponding to its subclass is not affected, but is changed to a new method (relatively old method implementation ).
3. abstract classes can only be inherited. Interfaces must be used when a subclass needs to implement functions inherited from multiple parent classes.

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.