PHP Object-oriented oop-interface 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 {...}

Define an interface using the interface keyword, "One" for the interface name
Interface One
{
Define a constant
CONST CONSTANT = ' constant value ';
Defines an abstract method "Fun1"
Public function fun1 ();
Defines the abstract method "Fun2"
Public function fun2 ();
}
?>

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 allows an interface to inherit another interface:

Inherit another interface using "extends"
Interface extends One
{
function Fun3 ();
function Fun4 ();
}
?>

and we define the subclass of an interface to implement the keyword that all abstract methods in the interface use are "Implements "Rather than the" extends "which we have previously said;

Use the keyword "implements" to implement an abstract method interface between the interface and the class
Class three implements one
{
function Fun1 () {
...
}
function fun2 () {
...
}
}
To implement all the 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;

Implementing multiple interfaces with implements
Class four Implemtns interface one, interface two, ...
{
You must implement the methods in all interfaces 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;

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

the above content is quoted from " Fried peanuts " eldest brother, thank you for sharing.

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

  • Related Article

    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.