20.PHP5 interface technology
PHP, like most object-oriented programming languages, does not support multiple inheritance. That is to say, each class can only inherit 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 methods in an abstract class are
abstract methods, then we change the declaration method to use "interface"; that is to say, all 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.
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
Copy code The code is as follows:
< ;?
abstract class Demo{
var $test;
abstract function fun1();
abstract function fun2();
function fun3(){
… .
}
}
$demo=new Demo(); //Abstract classes can generate instance objects, so this is wrong. The instantiated objects are handed over to subclasses
class Test extends Demo{
function fun1(){
…
}
function fun2(){
…
}
}
$test=new Test(); //Subclasses can Instantiate the object, because all abstract methods in the parent class are implemented
?>
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, so when declaring an abstract method, you don't need to use the
"abstract" keyword like an abstract class. This is already added by default. Keyword, 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 for the members in the
interface, we cannot When using "private" 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 methods in it are abstract methods, so the interface cannot produce instance objects
; it is also a specification, all abstract methods need to be implemented by subclasses .
We can use the "extends" keyword to let one interface inherit another interface;
Code snippet
Copy code
The code is as follows: //Use "extends" to inherit another interface
interface Two extends One{
function fun3();
function fun4();
}
?>
And when we define a subclass of an interface to implement all the abstract methods in the interface, the keyword used is "implements", and
is not what we said earlier "extends";
Code snippet
Copy code
The code is as follows: / /Use the keyword "implements" to implement the abstract method in the interface
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 abstract classes to implement some abstract methods in the interface, but in order to instantiate objects, this abstract
class must have subclasses to implement all its abstract methods Only then;
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 not only have to abide by the laws of the country, but also if we are in school,
we must also abide by the school rules;
Code snippet
Copy code
The code is as follows: //Use implements to implement multiple interfaces
class Four implemtns interface one, interface two, … .{
//required All methods in the interface must be implemented before the object can be instantiated.
}
?>
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 snippets
Copy code The code is as follows:
//Use extends to inherit a class and implements to implement multiple interfaces
class Four extends class name one implementmtns interface one, interface two, … .{
//All methods in the interface must be implemented before the object can be instantiated
… … … ..
}
?>
http://www.bkjia.com/PHPjc/320624.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320624.htmlTechArticle20.PHP5 interface technology PHP, like most object-oriented programming languages, does not support multiple inheritance. That is to say, every A class can only inherit from one parent class. To solve this problem, PHP introduced...
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