PHP object-oriented instances. PHP object-oriented instances this article mainly focuses on code and mainly serves to understand PHP object-oriented interfaces and memos through instances. Define the interface using the interface keyword, not the PHP object-oriented instance
This article mainly focuses on code and mainly serves to understand PHP object-oriented interfaces and memos through instances.
Define the interface using the interface keyword, not the class keyword;
Constants can be defined in the interface, but member attributes and member methods cannot be defined. this is different from abstract classes (abstract classes can be defined)
All methods in the interface are abstract methods, but they do not need to be modified using abstract keywords. there is no entity content.
Interface usb {
Function connect (); // connect to USB
Function quit (); // exit USB
Interface chapai {
Const DIANYA = '220v ';
Function charu (); // Insert
Function bachu (); // unplug
}
Take three different electronic devices as an example: different devices implement different USB interfaces in different ways, thus achieving different actions.
Digital camera: Plug in the picture on the computer pop-up browser U Shield: install the driver, open the browser mobile phone: Charging
Class shouji implements usb, chapai {// One class can implement multiple interfaces
Function connetc (){
Echo 'mobile phone charging, display mobile phone content ';
}
Function quit (){
Echo "the mobile phone stops charging and exits ";
}
Function charu () {// method for implementing the plug-in interface
Echo "cell phone via". self: DIANYA. "voltage charging, plug-in charging ";
}
Function bachu (){
Echo "cell phone power-off, exit". self: DIANYA. "voltage plug-in ";
}
}
Class xiangji implements usb {
Function connetc (){
Echo "the camera is inserted on USB to display images ";
}
Function quit (){
Echo "camera unplugging ";
}
}
Class pc {
Function usb Connect ($ usb) {// Pass in different electronic devices, get the device object, and then call the connection method of the electronic device.
$ Obj = new $ usb ();
$ Obj-> connect ();
}
Function usbQuit ($ usb) {// same as above, transfer different devices to call the exit method of the corresponding device
$ Obj = new $ usb ();
$ Obj-> quit ();
}
}
$ Apple = new pc ();
$ Apple-> usbConnetc ('shouji '); // A new computer object is generated, and the USB connection method is called when a mobile phone is input.
The above computer categories can be understood:
The mobile phone is connected to the computer via USB and the method of the mobile phone is called. when the camera is connected to the computer via USB, the method of the camera is called.
This article focuses on code. it mainly serves to understand PHP object-oriented interfaces and memos through instances. Define the interface using the interface keyword, no...