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, for this PHP introduced interface technology
If all the methods inside an abstract class are abstract and do not declare variables, and all members of the interface are public permissions, then this particular abstract class is called an interface
interface uses the interface keyword definition and uses implements to implement the method of the interface, and must fully implement the
Implement an interface
Here is a practical example of the implementation of the PHP interface, where an interface named Ipillage is created and implemented, and 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 CFO and ask to transfer funds to Swiss bank account.";
}
function burndocuments ()
{
echo "Torch the Office suite.";
}
}
Because all levels of people in the company can be overrun, there are assistant classes that 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 ()
{
echo "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 an action to occur, and the names of each method, the interface allows different classes to implement these methods in different ways. In this case, the Assistdnt class simply burns the file in the Trash, and the executive class does it in a much more extreme way (burning its office).
Implementing multiple interfaces
If we allow outside contractors to encroach on the company is unfair, after all, the company is established under the efforts of all full-time employees. In other words, how to provide employees with work and encroach on the functions of the company, and limit the contractor to complete the required tasks? The solution is to divide the 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, or it can be seen as a specification of a model. The interface differs roughly from an abstract class as follows:
If a subclass implements an interface, it must implement all the methods in the interface (whether or not it is needed), and if you inherit an abstract class, you need to implement the desired method
If a method name that is defined in a socket changes, then all subclasses that implement this interface need to update the method name synchronously, and if the method name in the abstract class has changed, the method names corresponding to their subclasses will not be affected, but become a new method.
Abstract classes can only be inherited, and when a subclass needs to implement a function that requires inheriting multiple parent classes, you must use an interface
code example
<?php
/**
* Declaration Interface Demo
* @author wzy
* */
Interface Demo
{
const NAME = "Wangzhengyi ";
Const AGE =;
function fun1 (); The declaring method defaults to the public abstract
function fun2 ();
}
/**
* Declaration interface Inheritance 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 ();
}