This article mainly uses the code primarily, the main function is through the example understanding PHP object-oriented Interface (interface) and the memo.
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 modification, 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 different methods, and then achieve different actions
Digital camera: Insert on the computer pop-up picture browser U shield: Install drive, open browser phone: charge
Class Shouji implements usb,chapai{//classes can implement multiple interfaces
function Connetc () {
Echo ' phone charge, show mobile content ';
}
function quit () {
echo "Cell phone stop charging, exit";
}
function Charu () {//implementation of interpolation interface method
echo "Mobile through". Self::D ianya. " Voltage charging, Plug and discharge charging ";
}
function Bachu () {
echo "Mobile Power unplugged, left". Self::D ianya. " Voltage Plug and Row ";
}
}
Class Xiangji implements usb{
function Connetc () {
echo "camera inserted on USB, display picture";
}
function quit () {
echo "Camera pull-out";
}
}
Class pc{
function Usbconnect ($USB) {//incoming to different electronic devices, get the object of the device and then invoke the link method of the electronic device
$obj = new $usb ();
$obj->connect ();
}
function Usbquit ($USB) {/ibid., calling the corresponding device's exit method by passing in different devices
$obj = new $usb ();
$obj->quit ();
}
}
$apple = new PC ();
$apple->usbconnetc (' Shouji '); New to the object of a computer, the incoming phone will call the phone to connect the USB method
Through the above computer class can be understood as:
Mobile phone through the USB connection to the computer, the opportunity to call the mobile phone method, the camera through the USB connection to the computer, will call the camera method