The example of this article describes the PHP interface technology. Share to everyone for your reference, specific as follows:
1. The interface is a special abstract class, why do you say so? If all of the methods in an abstract class are abstract methods, then we will change to a salutation called an "interface".
2. In addition, the variable cannot be declared in the interface.
3. All members of the interface are public permissions. When all subclasses are implemented, they must also be implemented with public permissions.
4. When declaring a class, we use the keyword "class", and when we declare the interface, we use the keyword "interface".
<?php
//Define an interface using the interface keyword, "One" for the interface name
interface one{
//define a constant
const constant = ' constant Value ';
Defines an abstract method fun1 public
function fun1 ();
Defines an abstract method fun2 public
function fun2 ();
>
5. Because all the methods in the interface are abstract methods, declaring an abstract method does not use the keyword "abstract" like an abstract class, and by default it has been added to the keyword.
6. The access rights in the interface must be public, the default is public, and you cannot use private and protected permissions.
7. An interface is a special abstract class in which all methods are abstract, so an interface cannot produce an instance object.
8. We can use the "extends" keyword to allow an interface to inherit another interface.
Interface two extends one{
function fun3 ();
function Fun4 ();
}
9. We define a subclass of an interface to implement all abstract methods in the interface using the keyword "implements" instead of "extends" as we said earlier.
Class Three implements two{
function fun1 () {
;
}
function fun2 () {
;
}
function Fun3 () {
;
}
function Fun4 () {
;
}
}
$three = new Three ();
$three->fun1 ();
10.PHP is a single inheritance, a class can have only one parent class, but a class may implement multiple interfaces, equivalent to a class that adheres to multiple specifications. Using implements to implement multiple interfaces, you must implement the methods in all interfaces to instantiate the object.
11.PHP not only can implement multiple interfaces, also can inherit a class at the same time implement multiple interfaces, must first inherit the class to implement the interface.
<?php
//use extends to inherit a class, use implements to implement multiple interfaces
class Test extends class name one implements interface one, interface two, ... {
//The methods in all interfaces are to be implemented to instantiate objects ...
}
More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.