PHP object-oriented instance _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.