How is the interface in PHP applied?

Source: Internet
Author: User
Tags php class
Interface

PHP class is a single inheritance, that is, do not support multiple inheritance, when a class requires multiple classes of functionality, inheritance is powerless, and for this reason PHP introduced interface technology

If all the methods in an abstract class are abstract, and no variables are declared, and all members of the interface are public permissions, then this particular abstract class is called an interface

The interface is defined using the interface keyword and implements the method of the interface using implements, and must fully implement the

Implementing an interface

Here is a practical example of the implementation of the PHP interface, here to create and implement a interface named Ipillage, the Ipillage interface is as follows:

Interface ipillage{function emptybakaccount (); function burndocument ();}

This interface is then implemented through the Executive class:

Class Executive extends Employee implements ipillage{private $totalStockOptions; function Emptybankaccount () {  echo "Call CFOs and ask to transfer funds to the Swiss bank account."; } function Burndocuments () {  echo "Torch the Office suite.";}}

You can have the Assistant class implement this interface:

Class Assistant extends Employee implements ipillage{function Takemome () {  echo "taking memo ...";} function Emptybankaccount () {  echo "Go on shopping spree with Office credit card."} function Burndocuments () {  E Cho "Start small fire in the trash Can."; }}

As you can see, interfaces are particularly useful. Because, although they define how many methods are required for the occurrence of a behavior, and the names of the individual methods, the interfaces allow different classes to implement these methods in different ways. In this case, the Assistdnt class simply burns the file in the garbage bin, and the executive class does it in a much more extreme way (burns its office).

Implementing multiple interfaces

It is unfair for us to allow foreign contractors to encroach on the company, after all, the company was established under the efforts of all full-time employees. That is, how to provide employees with the ability to work and encroach on the functions of the company, while restricting the contractor to complete the required tasks? The solution is to divide these tasks into several tasks and then implement the necessary multiple 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 {...}? >

The difference between an abstract class and an interface
An interface is a special abstract class, and can also be seen as a specification of a model. Interfaces are roughly different from abstract classes:

A subclass if you implements an interface, you must implement all the methods in the interface (whether or not needed), or if you inherit an abstract class, you only need to implement the required method
If the name of the method defined in one of the interfaces changes, then all subclasses implementing this interface need to update the method name synchronously, whereas in an abstract class, if the method name changes, the corresponding method names of its subclasses are not affected, just become a new method.
Abstract classes can only be inherited, and when a subclass needs to implement a function that inherits multiple parent classes, the interface must be used

code example

 <?php/** * Declaration Interface Demo * */interface Demo {Const NAME = "Wangzheng     Yi ";     Const AGE = 25; function fun1 ();  The Declaration method is public abstract function fun2 () by default;     }/** * 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 ();  }/** * Declares the parent class ParentClass * * @author wzy * */class ParentClass {function fun7 ();  /** * Subclasses must implement all the 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.