: This article mainly introduces php object-oriented OOP-interface. For more information about PHP tutorials, see. PHP, like most object-oriented programming languages, does not support multiple inheritance. That is to say, each class can only inherit one parent class. To solve this problem, PHP introduces interfaces. the interface idea is to specify a series of methods that must be implemented by a class that implements the interface. An interface is a special abstract class, and an abstract class is a special class, so an interface is also a special class. why is an interface a special abstract class? If all the methods in an abstract class are abstract methods, we use "interfaces" for another declaration method. that is to say, all the methods in the interface must be declared as abstract methods, in addition, variables (but constant can be declared) cannot be declared in the interface, and all the members in the interface have the public permission. Therefore, subclass must also use the public permission to implement it.
When declaring a class, the keyword we use is "class", and the interface is a special class, the keyword used is "interface ";
Class Definition: class name {... },
Interface declaration: interface name {...}
// Define an interface using the interface keyword. "One" is 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 ();
}
?>
In the above example, an interface "one" is defined, which declares two abstract methods "fun1" and "fun2", because all methods in the interface are abstract methods, therefore, when declaring an abstract method, you do not need to use the "abstract" keyword like an abstract class. This keyword is added by default, in addition, the "public" access permission in the interface can also be removed, because it is public by default, because all the members in the interface are public, for the members in the interface, we cannot use the "private" and "protected" permissions, both of which must be public or default. In addition, we also declare a constant "constant" in the interface. because variable members cannot be used in the interface, we need to use the const keyword for declaration.
Because an interface is a special abstract class, all the methods in it are abstract methods, so the interface cannot generate instance objects; it is also a standard, and all abstract methods need to be implemented by subclasses.
We can use the "extends" keyword to let an interface inherit from another interface:
// Use extends to inherit another interface
Interface Two extends One
{
Function fun3 ();
Function fun4 ();
}
?>
We define a subclass of an interface to implement the keyword "implements" used by all the abstract methods in the interface, rather than the "extends" mentioned above ";
// Use the keyword "implements" to implement the abstract method between the interface and the class
Class Three implements One
{
Function fun1 (){
...
}
Function fun2 (){
...
}
}
// All methods are implemented. we can use subclasses to instantiate objects.
$ Three = new Three ();
?>
We can also use abstract classes to implement partial abstract methods in interfaces. however, to instantiate an object, this abstract class must have a subclass to implement all its abstract methods;
As we have mentioned earlier, PHP is a single inheritance. a class can only have one parent class, but a class can implement multiple interfaces, which is equivalent to a class complying with multiple specifications, just as we must not only abide by national laws, but also comply with school rules if we are in school;
// Use implements to implement multiple interfaces
Class Four implemtns Interface 1, Interface 2 ,...
{
// The object can be instantiated only when all methods in the interface are implemented.
}
?>
In PHP, not only can a class implement multiple interfaces, but also can inherit a class and implement multiple interfaces at the same time. you must inherit the class before implementing the interface;
// Use extends to inherit a class and implements to implement multiple interfaces
Class Four extends class name 1 implemtns Interface 1, Interface 2 ,...
{
// Objects can be instantiated only when methods in all interfaces are implemented
...
}
?>
The above content is derived from "fried peanuts". thank you for sharing it.
The above introduces the php object-oriented OOP-interface, including the content, hope to be helpful to friends who are interested in PHP tutorials.