This article is mainly code-based, the main role is to understand the PHP object-oriented interface (interface) and memo through an example.
Define the interface using the interface keyword, not using the class keyword;
You can define constants in an interface, and you cannot define member properties and member methods, which are different from abstract classes (abstract classes can be defined)
The methods in the interface are all abstract methods, but without the abstract keyword decoration, there is no entity content
Interface usb{
function connect ();//Link USB
function quit ();//Exit USB
Interface chapai{
Const Dianya = ' 220v ';
function Charu ();//Insert
function Bachu ();//Pull Out
}
Take three different electronic devices for example: Different devices implement USB interface differently, and then implement different actions
Digital camera: Plug in computer pop up picture browser u shield: Install driver, open browser phone: charge
Class Shouji implements usb,chapai{//classes can implement multiple interfaces
function Connetc () {
echo ' mobile phone charging, display phone content ';
}
function Quit () {
echo "Phone stop charging, exit";
}
function Charu () {//method to implement the Plug and discharge interface
echo "Phone through". Self::D ianya. " Voltage charging, Plug and discharge charging ";
}
function Bachu () {
echo "Phone unplugged, leave." Self::D ianya. " Voltage insertion and discharge ";
}
}
Class Xiangji implements usb{
function Connetc () {
echo "Camera plugged into USB, display picture";
}
function quit () {
echo "Camera unplugged";
}
}
Class pc{
function Usbconnect ($USB) {//pass in different electronic devices, get the object of the device and then call the link method of this electronic device
$obj = new $usb ();
$obj->connect ();
}
function Usbquit ($USB) {///Ibid., passing in different devices call the appropriate device exit method
$obj = new $usb ();
$obj->quit ();
}
}
$apple = new PC ();
$apple->usbconnetc (' Shouji '); New out of a computer object, the incoming phone will call the phone to connect USB method
The above computer class can be understood as:
Mobile phone via USB connection to the computer, the opportunity to call the method of the phone, the camera connected to the computer via USB, will call the camera method
The above describes the PHP object-oriented examples, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.