The use of interfaces for PHP development

Source: Internet
Author: User

Inheritance simplifies the creation of objects, classes, and increases the weight of the code, but PHP only supports single inheritance, and if multiple inheritance is to be implemented, multiple interfaces will be used.
The interface is declared by the interface keyword, and the class can contain only the methods and some member variables that are not implemented, in the following format:
Interface interfacename{
function interfaceName1 ();
function interfaceName2 ();
...
}

Subclasses are implemented via the Implements keyword, and if multiple interfaces are to be implemented, a comma "," connection should be used between each interface. And all the methods that are not implemented are implemented in subclasses, otherwise PHP will go wrong. The format is as follows:
Class Subclass Implments interfacename1,interfacename2{
function interfaceName1 () {
function implementation
}
function InterfaceName2 () {
function implementation
}

}

The sample code is as follows:

<?php //Declaration interface a interface A{     function Aa();}//Declaration Interface B interface B{     function Bb();} class Am implements A{     function Aa(){        Echo "Aa is a php coder"; } } class Bm implements A,B{     function Aa(){    Echo "Mike is a php coder<br>";Echo "Mike is an iOS coder<br>"; } function Bb(){        Echo "Jack is a Java coder"; } }$jack=NewAm ();$bluce=NewBm ();$jack->aa ();Echo"<br>";$bluce->aa ();$bluce-&GT;BB ();?>

The results of the operation are as follows:

The use of interfaces for PHP development

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.