PHP Object-oriented instances

Source: Internet
Author: User
This article is mainly code-based, the main role is to understand the PHP object-oriented interface (interface) and memo through an example.

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 decoration, 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 differently, and then implement different actions

Digital camera: Plug in computer pop up picture browser u shield: Install driver, open browser phone: charge

Class Shouji implements usb,chapai{//classes can implement multiple interfaces

function Connetc () {

echo ' mobile phone charging, display phone content ';

}

function Quit () {

echo "Phone stop charging, exit";

}

function Charu () {//method to implement the Plug and discharge interface

echo "Phone through". Self::D ianya. " Voltage charging, Plug and discharge charging ";

}

function Bachu () {

echo "Phone unplugged, leave." Self::D ianya. " Voltage insertion and discharge ";

}

}

Class Xiangji implements usb{

function Connetc () {

echo "Camera plugged into USB, display picture";

}

function quit () {

echo "Camera unplugged";

}

}

Class pc{

function Usbconnect ($USB) {//pass in different electronic devices, get the object of the device and then call the link method of this electronic device

$obj = new $usb ();

$obj->connect ();

}

function Usbquit ($USB) {///Ibid., passing in different devices call the appropriate device exit method

$obj = new $usb ();

$obj->quit ();

}

}

$apple = new PC ();

$apple->usbconnetc (' Shouji '); New out of a computer object, the incoming phone will call the phone to connect USB method

The above computer class can be understood as:

Mobile phone via USB connection to the computer, the opportunity to call the method of the phone, the camera connected to the computer via USB, will call the camera method

The above describes the PHP object-oriented examples, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.