Explain in detail the application of interfaces in PHP

Source: Internet
Author: User
This article mainly introduces the application of interfaces in PHP, which is the basic knowledge of PHP beginners. For more information, see

This article mainly introduces the application of interfaces in PHP, which is the basic knowledge of PHP beginners. For more information, see

Interface
PHP classes are single inheritance, that is, they do not support multi-inheritance. When a class requires multiple class functions, inheritance is powerless. For this reason, PHP introduces the interface technology.

If all the methods in an abstract class are abstract methods without declaring variables and all the Members in the interface are public, this special abstract class is called Interface

The interface is defined using the interface keyword and implements is used to implement the interface method, and must be fully implemented

Implement an Interface

The following is an actual example of PHP interface implementation. An interface named IPillage is created and implemented here. The IPillage interface is as follows:

Interface IPillage {function emptyBakAccount (); function burnDocument ();}

Then implement this interface through the Executive class:

Class Executive extends Employee implements IPillage {private $ totalStockOptions; function emptyBankAccount () {echo "Call CFO and ask to transfer funds to Swiss bank account. ";} function burnDocuments () {echo" Torch the office suite. ";}}

This interface can be implemented by the Assistant class because the employees of all levels in the company can carry out encroaching:

Class Assistant extends Employee implements IPillage {function takeMome () {echo "Taking memo... ";} function emptyBankAccount () {echo" Go on shopping spree with office credit card. ";} function burnDocuments () {echo" Start small fire in the trash can. ";}}

The interface is particularly useful. Although they define how many methods are required for a row and the names of each method, the interface allows different classes to implement these methods in different ways. In this example, the mongodnt class only burns the file in the garbage box ,, the Executive class is implemented in a more excessive way (burning its office ).

Implement multiple interfaces

It is unfair to allow external contractors to intrude into the company. After all, the company was established with the efforts of all full-time employees. That is to say, how can we provide employees with work and encroaching on the company, while limiting contractors to only complete the necessary tasks? The solution is to divide these tasks into several tasks and then implement the necessary interfaces. PHPS supports this feature. Consider the following example:

<? Php interface IEmployee {...} interface IDeveloper {...} interface IPillage {...} class Employee implements IEmployee, IDeveloper, iPillage {...} class Contractor implements IEmployee, IDeveloper {...}?>


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:

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.
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.
Abstract classes can only be inherited. When a subclass needs to implement a function that inherits multiple parent classes, the interface must be used.


Sample Code


<? Php/*** declare interface Demo * @ author wzy **/interface Demo {const NAME = "wangzhengyi"; const AGE = 25; function fun1 (); // The default declaration method is public effecactfunction fun2 ();}/*** declares the inheritance of the interface Demo2 ** @ author wzy **/interface Demo2 extends Demo {function fun3 (); function fun4 ();}/*** declaration interface Demo3 ** @ author wzy **/interface Demo3 {function fun5 (); function fun6 ();} /*** declare parent class ParentClass ** @ author wzy **/class ParentClass {function fun7 ();} /*** subclass must implement all methods in the interface ** @ author wzy **/class ChildClass extends ParentClass implements Demo2, Demo3 {function fun1 (); function fun2 (); function fun3 (); function fun4 (); function fun5 (); function fun6 ();}

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.