Php object-oriented full strategy (14th) php5 interface technology
Source: Internet
Author: User
Like most object-oriented programming languages, PHP does not support multiple inheritance. that is to say, each class can only inherit one parent class. 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. To solve this problem, PHP introduces an interface. the interface idea is to specify a class required to implement this interface.
A series of implementation methods. An interface is a special abstract class, and an abstract class is a special class.
Why is an interface a special abstract class? If all the methods in an abstract class are
Abstract method, then we use "interface" for another declaration method; that is to say, all methods in the interface must be
Declaration is an abstract method. In addition, variables cannot be declared in the interface, and all the members in the interface have the public permission.
Therefore, subclass must also use the public permission to implement it.
When declaring a class, we use the keyword "class", while the interface is a special class, the keyword used
Is "interface ";
Class Definition: class name {... }, Interface declaration: interface name {... }
Code snippet
The code is as follows:
Abstract class Demo {
Var $ test;
Abstract function fun1 ();
Abstract function fun2 ();
Function fun3 (){
... .
}
}
$ Demo = new Demo (); // The abstract class is used to generate instance objects. Therefore, this is an error. The instantiated object is handed over to the subclass.
Class Test extends Demo {
Function fun1 (){
...
}
Function fun2 (){
...
}
}
$ Test = new Test (); // The subclass can instantiate the object, because all the 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 methods in the interface are abstract methods. Therefore, when declaring abstract methods, you do not need to use them like abstract classes.
The keyword "abstract" is added by default, and the keyword "public" is added in the interface.
The permission can also be removed, because the default permission is public, because all the members in the interface are public and
We cannot use the "private" and "protected" permissions for the members in the interface. we must use either public or
Yes. In addition, we also declare a constant "constant" in the interface, because the variable cannot be
So we need to use the const keyword to declare.
Because an interface is a special abstract class, all the methods in it are abstract methods, so the interface cannot generate real
Example object. it is also a standard, and all abstract methods need to be implemented by subclass.
We can use the "extends" keyword to let an interface inherit from another interface;
Code snippet
The code is as follows:
// Use extends to inherit another interface
Interface Two extends One {
Function fun3 ();
Function fun4 ();
}
?>
The key word used by defining a subclass of an interface to implement all abstract methods in the interface is "implements", while
Not the "extends" we mentioned earlier ";
Code snippet
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 (){
... .
}
}
// All methods are implemented. we can use subclasses to instantiate objects.
$ Three = new Three ();
?>
We can also use abstract classes to implement partial abstract methods in interfaces, but to instantiate an object, this abstraction
The class must have a subclass to implement all its abstract methods;
As we have mentioned earlier, PHP is a single inheritance. a class can only have one parent class, but one class can implement multiple interfaces.
It is equivalent to a class that must comply with multiple norms, just as we must not only comply with national laws, but if we are in school,
It must also comply with the school rules;
Code snippet
The code is as follows:
// Use implements to implement multiple interfaces
Class Four implemtns Interface 1, Interface 2 ,... .{
// The object can be instantiated only when all methods in the interface are implemented.
}
?>
In PHP, not only can a class implement multiple interfaces, but also can inherit a class and implement multiple interfaces at the same time.
Inherit the class before implementing the interface;
Code snippet
The code is as follows:
// Use extends to inherit a class and implements to implement multiple interfaces
Class Four extends class name 1 implemtns Interface 1, Interface 2 ,... .{
// Objects can be instantiated only when methods in all interfaces are implemented
... ... ... ..
}
?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service