Php object-oriented? Interface Php object-oriented? Interface
Interface: interface
In php, we can specify which public external operations an object should have and use interfaces to specify.
The common method is the interface.
Specifies which public operation methods (interfaces) an object should be used for. this is also called an interface (a set of public operation methods)
Interface (interface Structure, public method set)
Public method (interface method)
Definition:
A structure used to limit the public operation methods required by an object. it is called an interface)
Syntax:
Define the interface structure and use the interface keyword. Some common methods are defined in the interface.
Interface name
{
List of common operation methods
}
Example:
Interface I _Goods
{
Public function sayName ();
Public function sayPrice ();
}
Note:
1. interface method. the access permission must be public.
2. there can only be public methods in the interface, and member variables cannot exist.
3. the interface can only contain unimplemented methods, also called abstract methods, but does not need abstract keywords.
Class implementation interface, using the keyword implements.
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 this interface must implement all the abstract methods in the interface. It is also certain that this method must be a public external operation method.
Multiple implementations
In theory, the above functions can be implemented through abstract classes, but the abstract classes are not professional.
The interface is specialized in implementation. because php supports multiple implementations, it 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 ()
{
}
}
Interfaces can also be inherited.
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, class constants can be defined
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: PAI;
Output: 3.14