20.PHP5 Interface Technology
PHP, like most object-oriented programming languages, does not support multiple inheritance. That is, each class can inherit only one parent
Class. To solve this problem, PHP introduces the interface, the idea is to specify a class that implements the interface must
A series of methods to implement. Interface is a special kind of abstract class, abstract class is a special class, so the interface is a
A special kind of class, why is the interface a special abstract class? If all of the methods inside an abstract class are
Abstract method, then we use "interface" for a different declarative method, which means that all the methods in the interface must be
Declared as an abstract method, the other interface cannot declare variables, and all members of the interface are public permissions.
Therefore, the subclass must also use the public permission limit when it is implemented.
When declaring a class, the keyword we use is "class" and the interface is a special class that uses the keyword
Is "interface";
Class definition: Class class name {...}, Interface declaration: Interface interface Name {...}
Code fragment
Copy Code code as follows:
?
Abstract class demo{
var $test;
Abstract function fun1 ();
Abstract function fun2 ();
function Fun3 () {
... .
}
}
$demo =new demo (); Abstract class to be able to produce an instance object, so this is wrong, instantiate the object to the subclass
Class Test extends demo{
function Fun1 () {
...
}
function fun2 () {
...
}
}
$test =new test (); Subclasses can instantiate objects because all abstract methods in the parent class are implemented
?>
The example above defines an interface "one", which declares two abstract methods "Fun1" and "fun2" because the
All the methods in the mouth are abstract methods, so you don't have to use them as abstract classes when declaring abstract methods.
"Abstract" This keyword, the default has been added to this keyword, in addition to the interface inside the "public" this visit
Ask permission can also be removed, because the default is public, because the interface of all members are publicly owned, where the
The members of the interface we can not use the "private" and "protected" of the permission to use the public or the default
Recognized. In addition we also declare a constant "constant" in the interface, because the variable can not be used in the interface
, so we're going to use the Const keyword declaration.
Because the interface is a special kind of abstract class, all the methods inside are abstract methods, so the interface can not produce real
Example object; It is also a specification, and all abstract methods require subclasses to implement.
We can use the "extends" keyword to allow an interface to inherit another interface;
Code fragment
Copy Code code as follows:
<?php
To inherit another interface using "extends"
Interface two extends one{
function Fun3 ();
function Fun4 ();
}
?>
And we define a subclass of an interface to implement all the abstract methods in the interface using the keyword "implements", and
Not the "extends" as we said before;
Code fragment
Copy Code code as follows:
<?php
Use the keyword "implements" to implement abstract methods in the interface
Class Three implements one{
function Fun1 () {
... .
}
function fun2 () {
... .
}
}
With all the methods implemented, we can use subclasses to instantiate objects.
$three =new three ();
?>
We can also use abstract classes to implement some abstract methods in an interface, but to instantiate an object, this abstract
Class also has subclasses to implement all of its abstract methods;
As we said earlier, PHP is a single inheritance, a class can have only one parent class, but a class can implement multiple
, the equivalent of a class to comply with multiple specifications, just as we have to obey not only the laws of the state, if it is in school,
But also to obey the school rules;
Code fragment
Copy Code code as follows:
<?php
Implementing multiple interfaces using implements
Class Four Implemtns interface One, interface two, .... {
The methods in all interfaces must be implemented to instantiate the object.
}
?>
In PHP, not only a class can implement multiple interfaces, it can also implement multiple interfaces while inheriting a class, and must
To inherit the class before implementing the interface;
Code fragment
Copy Code code as follows:
<?php
Using extends to inherit a class, using implements to implement multiple interfaces
Class Four extends name one implemtns interface One, interface two, .... {
Methods in all interfaces are to be implemented to instantiate an object
... ... ... ..
}
?>