PHP5 Interface Technology
PHP, like most object-oriented programming languages, does not support multiple inheritance. In other words, each class can inherit only one parent
Class. To solve this problem, PHP introduced the interface, the idea of the interface is to specify a implementation of the interface of the class must be
Implementation of a series of methods. Interface is a special kind of abstract class, abstract class is a special class, so the interface is also a
Kind of special class, why is the interface a special kind of abstract class? If all the methods inside an abstract class are
Abstract method, then we will use the "interface" for the declaration 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.
So the subclass must also use the public permission limit when implementing it.
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 name {...}, declaration of the Interface: interface interface Name {...}
Code Snippets
<?phpabstract class demo{var $test, abstract function fun1 (), abstract function fun2 (), function fun3 () {...}} $demo =new Demo (); The abstract class is capable of producing an instance object, so it is wrong to instantiate the object to the subclass class Test extends demo{function fun1 () {...} function fun2 () {...}} $test =new Test (); Subclasses can instantiate an object because all abstract methods in the parent class are implemented?>
The above example defines an interface "one", which declares two abstract methods "Fun1" and "fun2", because the connection
All the methods in the mouth are abstract, 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 all the members of the interface is publicly owned, where
Interface members We can not use the "private" and "protected" permission, all use public or the default
Recognition. Also in the interface we declare a constant "constant", because in the interface can not use the variable into
So we're going to use the Const keyword declaration.
Because the interface is a special kind of abstract class, all the methods are abstract methods, so the interface can not produce real
Example object; It is also a specification, and all abstract methods require subclasses to be implemented.
We can use the "extends" keyword to allow an interface to inherit another interface;
Code Snippets
<?php//Use "extends" to inherit another interface interface two extends one{function fun3 (); function fun4 ();}?>
While we define the subclass of an interface to implement the keyword that all abstract methods in the interface use are "implements", and
Not the "extends" we said before;
Code Snippets
<?php//Use the keyword "implements" to implement an abstract method in the interface class three implements one{function Fun1 () {...} function fun2 () {...}}//implementation All the way, 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 abstraction
The class also has subclasses that can implement all of its abstract methods;
As we said earlier, PHP is single-inheritance, a class can have only one parent class, but a class may implement multiple
Mouth, is equivalent to a class to comply with multiple specifications, just as we must not only abide by national laws, if it is in school,
and obey the school rules;
Code Snippets
<?php//using implements to implement multiple interfaces Class four Implemtns interface one, interface two, .... {//You must implement all the methods in the interface to instantiate the object. }?>
In PHP, not only a class can implement multiple interfaces, but also can inherit a class at the same time to implement multiple interfaces, certain
Inherit the class first to implement the interface;
Code Snippets
<?php//Use extends to inherit a class, use implements to implement multiple interfaces class four extends class name one Implemtns interface One, interface two, .... {//All methods in the interface must be implemented to instantiate the object ...} ?>