Interface is a very important concept in the object-oriented programming of PHP. This paper describes the usage of PHP interface in detail in the form of an instance. Specifically as follows:
Interface: interface
In PHP, we can specify what public external operations an object should have, and use interface to specify it.
The common method is the interface. Used to specify which public action method (interface) An object should be used for, which is also called an interface (a collection of common action methods).
namely: interface (interface structure, public method collection)
Public method (interface method)
Definition: A structure that defines the public action methods that an object must have, called an Interface (interface)
Syntax: Defines the interface structure, using the interface keyword. Some common methods are defined within an interface.
Interface interface Name
{
Public action method list
}
Examples are as follows:
Interface I_goods
{public
function sayname ();
Public function Sayprice ();
}
Attention:
1. Interface method, access permission must be public
2. The interface can only have public methods, can not exist member variables
3. An interface can contain only methods that are not implemented, also called abstract methods, but do not use the abstract keyword.
The class implements the interface and completes 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 ()
{
}
}
Thus, the class that implements the interface must implement all the abstract methods within the interface. And to be sure, the method must be a public external operation.
Multi-Implementation: This function can theoretically be implemented by abstract classes, but abstract classes are not professional.
The use of interfaces is professional, implemented, because PHP supports multiple implementations, and only single inheritance is supported .
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 ()
{
}
}
You can also inherit between interfaces
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::P AI;
Run output: 3.14