PHP Object-Oriented-interface
Interface: interface
In PHP, we can specify what public external operations an object should have, using interface.
The common method is the interface.
Used to specify which public operation methods (interfaces) An object should be used for, which is also called an interface (a collection of public action methods)
Interface (interface structure, public method collection)
Public methods (interface methods)
Defined:
A structure, called an interface (interface), used to qualify an object to have a common method of operation.
Grammar:
Define the interface structure, using the interface keyword. The interfaces are defined in a number of public methods.
Interface Interface Name
{
List of common methods of operation
}
Example:
Interface I_goods
{
Public function sayname ();
Public function Sayprice ();
}
Attention:
1. Interface methods, access permissions must be public
2. There can be only public methods within the interface, no member variables exist
3. Interfaces can only contain methods that are not implemented, also called abstract methods, but do not use the abstract keyword.
The implements class implements the interface and completes with the keyword.
Example:
Interface I_goods
{
Public function sayname ();
Public function Sayprice ();
}
Class Goods implements I_goods
{
Public Function Sayname ()
{
}
Public Function Sayprice ()
{
}
}
Thus, the class that implements the interface must implement all the abstract methods within the interface. And it is certain that this method must be a public external operation method.
Multi-Implementation
The above functions, in theory, can be implemented by abstract classes, but abstract classes, not professional.
interface specialized in, implementation, because PHP supports multiple implementations, and only supports single inheritance.
Example:
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 ()
{
}
}
Can also inherit between interfaces
Example:
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
Example:
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::P ai;
Output: 3.14