PHP Object-oriented instance

Source: Internet
Author: User

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



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.