Home > Backend Development > PHP Tutorial > PHP object-oriented examples

PHP object-oriented examples

WBOY
Release: 2016-08-08 09:27:50
Original
882 people have browsed it

This article is mainly based on code, and its main purpose is to understand PHP's object-oriented interface (interface) and memos through examples.

Use the interface keyword instead of the class keyword to define an interface;

Constants can be defined in an interface, but member properties and member methods cannot be defined. This is different from abstract classes (abstract classes can be defined)

The methods in the interface are all abstract methods, but they are not modified with the abstract keyword and have no entity content

interface usb{

function connect();//Link 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 USB interfaces in different ways, and thus implement different actions

Digital camera: Plug it into the computer and the picture browser will pop up U-shield: Install Driver, open the browser Mobile phone: charging

class shouji implements usb,chapai{ //A class can implement multiple interfaces

function connetc(){

echo 'Charging mobile phone, displaying mobile phone content' ;

}

function quit(){

echo "The phone stops charging, exit";

}

function charu( ){ //Method to implement plug-in interface通过 Echo "mobile phone through" .Self :: diany. "Charging voltage, storing charging"; Leave ".self::DIANYA."voltage strip";

}

}

class xiangji implements usb{

function connetc(){

echo "camera plug On the USB, display the picture ";

}

" function quit(){

" echo "Camera unplugged";

}

}

class pc{

function usbConnect($usb){ //Pass in different electronic devices, get the device object and then call the link method of this electronic device

$obj = new $usb();

                $obj->connect();

                                        using using using           using using             through using ’’s ’ through out using ’s ’ s  ‐ ‐‐‐‐ ​ ​                    $ usb (); gt;usbConnectc( 'shouji'); //new a computer object, and when the mobile phone is passed in, it will call the method of connecting the mobile phone to USB

Through the above computer class, it can be understood as:

The mobile phone is connected to the computer through USB, the opportunity Call the method of the mobile phone; when the camera is connected to the computer via USB, the method of the camera will be called

The above has introduced PHP object-oriented examples, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template