Interface is a very important concept in PHP object-oriented programming. This article describes the usage of the PHP interface in more detail with examples. The details are as follows:
Interface: interface
In PHP, we can specify what public external operations an object should have, which can be specified using interface.
Public methods are interfaces. Used to specify which public operation methods (interfaces) an object should be used for, this is also called an interface (a collection of public operation methods)
That is: interface (interface structure, public method collection)
Public methods (interface methods)
Definition: A structure used to limit the public operation methods that an object must have, called an interface
Syntax: Define the interface structure and use the interface keyword. What are defined in the interface are some public methods.
interface接口名 { 公共操作方法列表 }
Examples are as follows:
interface I_Goods { public function sayName(); public function sayPrice(); }
Note:
1. Interface methods, access rights must be public public
2. There can only be public methods in the interface and no member variables
3. The interface can only contain unimplemented methods, also called abstract methods, but the abstract keyword is not used.
class implements the interface and uses the keyword implements to complete.
Example:
interface I_Goods { public function sayName(); public function sayPrice(); } class Goods implements I_Goods { public function sayName() { } public function sayPrice() { } }
In this way, the class that implements the interface must implement all abstract methods in the interface. And it is certain that this method must be a public external operation method.
Multiple implementations: This function can theoretically be implemented through abstract classes, but abstract classes are not professional.
Using interfaces is more professional in terms of implementation, because php supports multiple implementations and only supports single inheritance .
Examples are as follows:
interface I_Goods { public function sayName(); public function sayPrice(); } interface I_Shop { public function saySafe(); } class Goods implements I_Goods , I_Shop { public function sayName() { } public function sayPrice() { } public function saySafe() { } }
Interfaces can also be inherited
Examples are as follows:
interface I_Goods { public function sayName(); public function sayPrice(); } interface I_Shop extends I_Goods { public function saySafe(); } class Goods implements I_Shop { public function sayName() { } public function sayPrice() { } public function saySafe() { } }
PHP object interface support, you can define class constants
Examples are as follows:
interface I_Goods { const PAI = 3.14; public function sayName(); public function sayPrice(); } interface I_Shop extends I_Goods { public function saySafe(); } class Goods implements I_Shop { public function sayName() { } public function sayPrice() { } public function saySafe() { } } echo Goods::PAI;
Run output: 3.14
The PHP interface is not used in conjunction with other programming languages, but to develop a template for future development. Everyone can just follow this template.
For example, regarding database operations. The interface can be written like this, setting the four methods of insert, update, select, and delete. The database is nothing more than four operations of add, delete, modify, and check.
Other programmers only need to implement (inplements) this interface and write programs according to the ideas of these four methods. Once written, the database interface is implemented.
In this way, the chief software designer only needs to give the software a few things (interfaces) to do, and the following programmers can do it according to these few things. Are interfaces very advantageous in systems developed by multiple people?
As for common API interfaces, in fact, to put it simply, they are method function classes. We only need to call the methods written by others.
Abstract class:
is used for inheritance. It cannot be instantiated. It is used to set specifications. Subclasses must implement all the abstract methods of the parent class.
Interface:
can be understood as a more strict abstraction. Class
First, like an abstract class, specifications can be set. Because interfaces have characteristics, to implement the interface, all methods inside must be implemented. In this way, the project manager can set a specification in the interface, which functions should be implemented
Second, PHP uses single inheritance. A class can only have one parent class. In order to solve this problem, interfaces appeared. One class can implement multiple interfaces