The interface defines a general specification for implementing a service, declares the required functions and constants, but does not specify how to implement them. The details of the implementation are not given because different entities may need to implement a common method definition in different ways. The key is to establish a set of general principles that must be achieved, and only if these principles are met can the interface be realized.
The class member is not defined in the interface! The definition of a class member is completely given to the implementation class.
Let's look at a simple example with a comprehensive understanding:
<?phpinterface fruit{ Const max_weight = 5; There is no declaration here, is a static constant function SetName ($name); function GetName ();} Implement Interface class Apple implements fruit{ private $name; function GetName () { return $this->name; } function SetName ($_name) { $this->name = $_name; }} $apple = new Apple (); Create Object $apple->setname ("Apple"); echo "created a". $apple->getname (); echo "<br/>"; echo "Max_grade is". Apple::max_weight; Static constant?>
Take embezzlement of company property as an example. This can be done in a number of ways, depending on who is doing this disgraceful thing. For example, an ordinary employee might use an office credit card to buy shoes and movie tickets, and then write "office supplies" on the purchase document, while a supervisor might have his assistant remit money to his Swiss bank account through an online account system. Both of these employees can accomplish this task, but in a different way. In this case, the goal of the interface is to define a set of principles that encroach on the company, and then require each class to implement the interface separately. For example, an interface might include only two methods:
Emptybankaccount () burndocuments ()
The employee and executive classes can then be required to implement these features. In this section, we will learn about these things. First, however, it takes some time to understand how PHP5 implements the interface. In PHP, you create an interface like this:
Interface Iinterfacename{const 1; CONST n;function methodName1 ();.. function methodnamen ();}
Typically, the letter i is preceded by the interface name to identify it for easier identification.
When the class implements the interface through the Implements keyword, a contract is completed. All methods in the interface must be implemented, and if the implementation class does not implement all of the methods, it must be declared as an abstract class, or the fatal error shown below will appear:
Fatal Error:class AAA contains 1 abstract method and must therefore be declared abstract or implement the remaining Metho DS (Iinterfacename::methodnamen) in D:\www\test.php on line 11
The following is the general syntax for implementing the above interfaces:
class Implements iinterfacename{ function methodName1 () { echo "FDA"; } function methodnamen () { echo "Methodnamen"; }}
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 Swiss bank account."; } function burndocuments () { echo "Torch the Office suite."; }}
Because all levels of the company can be overrun, there are assistant classes that can 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.";} }
name, but the interface allows 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{... }classimplements Iemployee,ideveloper,ipillage { ... }classimplements IEmployee,ideveloper { ... }?>
As you can see, the employee class can implement all 3 interfaces, while the contractor class only implements IEmployee and Ideveloper.
PHP interface Implementation (GO) (note)