PHP Object-oriented (OOP):P HP5 Interface Technology (interface)

Source: Internet
Author: User

Like most object-oriented programming languages, PHP does not support multiple inheritance. This means that each class can inherit only one parent class. To solve this problem, PHP introduced the interface, the idea of the interface is to specify a implementation of the interface of the class must implement a series of methods. interface is a special kind of abstract class, abstract class is a special class, so the interface is a special class, Why is the interface is a special kind of abstract class? If all of the methods in an abstract class are abstract, then we use the "interface" in a different way, that is, all the methods in the interface must be declared as abstract methods, and the interface cannot declare variables (but can declare constant constant). And all the members of the interface are public permissions. So the subclass must also use the public permission limit when implementing it.

Declare a class when we use the keyword is "class", and the interface of a special class, the use of the keyword is "interface";

Class definition: Class name {...},
Declaration of the Interface: interface interface Name {...}

<?PHP//define an interface using the interface keyword, "One" for the interface nameInterfaceone{//Define a constant Const constant= ' constant value '; //defines an abstract method "Fun1" Public functionfun1 (); //defines the abstract method "Fun2" Public functionfun2 ();}?>

The above example defines an interface "one", which declares two abstract methods "Fun1" and "fun2", because all of the methods in the interface are abstract methods, so when declaring an abstract method, it is not necessary to use the "abstract" keyword as an abstraction. The default has been added to this keyword, in addition to the interface inside of the "public" This access can also be removed, because the default is public, because all members of the interface if the member of the interface, we can not use "private" and "protected "Permission to use public or default. In addition we declare a constant "constant" inside the interface, because the variable member cannot be used in the interface, so we want to use the Const keyword declaration.

Because the interface is a special kind of abstract class, all of the methods are abstract methods, so the interface can not produce an instance object ; It is also done as a specification, and all abstract methods require subclasses to be implemented.

We can use the "extends" keyword to get an interface to inherit another interface:

<? PHP // inherit another interface using "extends" Interface extends one{    function  fun3 ();     function fun4 ();}? >

And we define the subclass of an interface to implement the key words that all abstract methods in the interface use are "implements", rather than "extends" as we said earlier;

<? PHP // Use the keyword "implements" to implement an abstract method interface between the interface and the class class Implements one{    function  fun1 () {        ...     }    function  fun2 () {        ...     }}// implements all methods, we can use subclasses to instantiate objects $three=new  Three ( );? >

We can also use abstract classes to implement some of the abstract methods in the interface, but to instantiate an object, the abstract class must have subclasses to implement all of its abstract methods;

As we said earlier, PHP is single-inheritance, a class can have only one parent class, but a class may implement more than one interface, it is equivalent to a class to comply with a number of specifications, just as we have to abide by the laws of the state, if it is in school, but also abide by the school rules;

<? PHP // implementing multiple interfaces with implements class Four Implemtns interface One, interface two, ... {    // You must implement all the methods in the interface to instantiate the object.  }?>

In PHP, not only a class can implement multiple interfaces, but also can inherit a class at the same time to implement multiple interfaces, we must first inherit the class to implement the interface;

<? PHP // use extends to inherit a class and implement multiple interfaces using implements class extends class name one Implemtns interface One, interface two, ... {    // All methods in the interface must be implemented to instantiate the object ...    }?>

PHP Object-oriented (OOP):P HP5 Interface Technology (interface)

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