Home > Article > Backend Development > What is the php object-oriented interface? how to use?
Interface is a very important concept in PHP object-oriented programming. This article describes the usage of the PHP interface in more detail in the form of examples. The details are as follows:
Interface: interface
In PHP, we can specify which public external operations an object should have, which can be specified using interface.
The public method is the interface. 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 method (Interface method)
Definition: A structure used to limit the public operation methods that an object must have, called interface (interface)
Syntax: To define the interface structure, 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 cannot have members Variables
3. The interface can only contain unimplemented methods, also called abstract methods, but do not use the abstract keyword.
The 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() { } }
phpObject 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 above is the detailed content of What is the php object-oriented interface? how to use?. For more information, please follow other related articles on the PHP Chinese website!