Home > Article > Backend Development > php object-oriented OOP—interface interface
PHP, like most object-oriented programming languages, does not support multiple inheritance. That is to say, each class can only inherit from one parent class. In order to solve this problem, PHP introduced interfaces. The idea of interfaces is to specify a series of methods that a class that implements the interface must implement. The interface is a special abstract class, and the abstract class is a special class, so the interface is also a special class. Why is it said that the interface is a special abstract class? If all the methods in an abstract class are abstract methods, then we use "interface" in another declaration method; that is to say, all methods in the interface must be declared as abstract methods, and variables cannot be declared in the interface ( But constants can be declared), and all members in the interface have public permissions. So when the subclass is implemented Also be sure to use public permissions.
The keyword we use when declaring a class is "class", and the interface is a special class, the keyword used is "interface";
Definition of class: class class name { … },
Interface declaration: interface interface name {…}
//Define an interface using the interface keyword, "One" is the interface name
interface One
{
//Define a constant
const constant = 'constant value';
//An abstract method "fun1" is defined
public function fun1();
//An abstract method "fun2" is defined
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 They are all abstract methods, so when declaring an abstract method, you do not need to use the "abstract" keyword like an abstract class. This keyword has been added by default. In addition, the "public" access permission in the interface can also be removed. , because the default is public, and because all members in the interface must be public, we cannot use "private" and "protected" permissions for members in the interface, and must use public or default. In addition, we also declared a constant "constant" in the interface. Because variable members cannot be used in the interface, we have to use const keyword declaration.
Because the interface is a special abstract class, all the methods in it are abstract methods, so the interface cannot produce instance objects; it is also a specification, and all abstract methods need to be implemented by subclasses.
We can use the "extends" keyword to let one interface inherit another interface://Use "extends" to inherit another interface
interface Two extends One
{
function fun3();
function fun4();
}
?>
The keywords used when we define a subclass of an interface to implement all abstract methods in the interface are "implements", instead of "extends" as we said before;
//Use the keyword "implements" to implement the abstract method interface in the interface and class Three implements One
{
function fun1() {
...
}
function fun2() {
...
}}
//With all methods implemented, we can use subclasses to instantiate objects
$three=new Three();
?>
We can also use abstraction class to implement some abstract methods in the interface, but in order to instantiate an object, this abstract class must have a subclass to implement all its abstract methods; As we said before, PHP is single inheritance , a class can only have one parent class, but a class can implement multiple interfaces, which is equivalent to a class having to comply with multiple specifications, just like we must not only abide by the laws of the country, but also abide by the school's laws if we are in school. The school rules are the same;
//Use implements to implement multiple interfaces
class Four implemtns interface one, interface two, ...
{
//All methods in the interface must be implemented before they can be instantiated object. }
?>
In PHP, not only one class can implement multiple interfaces, but you can also implement multiple interfaces while inheriting a class. You must first inherit the class and then implement the interface;
//Use extends to inherit a class and implements to implement multiple interfaces
class Four extends class name one implementtns interface one, interface two, ...
{
// All the methods in the interface must be implemented before the object can be instantiated
...}
?>
The above content is quoted from "fried peanuts" Brother, thank you for your help share.
The above introduces the PHP object-oriented OOP-interface interface, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.