Home  >  Article  >  Backend Development  >  Detailed explanation of PHP object-oriented interface technology examples

Detailed explanation of PHP object-oriented interface technology examples

伊谢尔伦
伊谢尔伦Original
2017-06-29 09:50:411087browse

PHP5 Interface Technology

PHP, like most Object-orientedprogramming languages, does not support multiple inheritance. That is to say, each A class can only inherit from one parent
class. In order to solve this problem, PHP introduced interfaces. The idea of ​​​​the interface 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 the interface said to be a special abstract class? If all the methods in an abstract class are
abstract methods, then we change the declaration method to use "interface"; that is to say, all the methods in the interface must be
declared as abstract methods. In addition Variables cannot be declared in the interface, and all members in the interface have public permissions.
So subclasses must also use public permissions when implementing them.
The keyword we use when declaring a class is "class", and the interface is a special class, and the keyword
used is "interface";
Definition of class: class class name {… }, interface declaration: interface interface name { ... }
Code snippet

In the above example, an interface "one" is defined, which declares two abstract methods "fun1" and "fun2", Because all the methods in the interface are abstract methods, there is no need to use the
"abstract" keyword like an abstract class when declaring an abstract method. This keyword is already added by default. In addition, The "public" access permission in the interface can also be removed, because the default is public, because all members in the interface must be public, so we cannot use "private" for members in the interface and "protected" permissions, use public or the default
. In addition, we also declared a constant "constant" in the interface. Because variables cannot be used as members in the interface, we have to use the const keyword declaration.
Because the interface is a special abstract class, all the methods in it are abstract methods, so the interface cannot produce real
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;
Code snippet


And we define a subclass of an interface to implement the key to using all abstract methods in the interface The word is "implements", and
is not the "extends" we mentioned earlier;
Code snippet

We can also use abstract classes to implement some abstract methods in the interface, but we must If you want 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, and 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

if we are in school, we must abide by the school rules. ;

Code snippet


In PHP, not only one class can implement multiple interfaces, but you can also implement multiple interfaces while inheriting a class. You must
inherit the class first and then implement the interface;
Code snippet


The above is the detailed content of Detailed explanation of PHP object-oriented interface technology examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn