This article mainly introduces the PHP interface technology, combined with an example of the basic concept of PHP interface, function, definition, use methods and related considerations, the need for friends can refer to the following
1. Interface is a special kind of abstract class, why do you say so? If all the methods in an abstract class are abstract methods, then we will call them "interfaces".
2. The variable cannot be declared in another interface.
3. All the members in the interface are public permissions. When implemented, all subclasses must also use the public permission to implement.
4. When declaring a class, we use the keyword "class", while declaring the interface, we use the keyword "interface".
<?php//defines an interface using the interface keyword, "One" for the interface name interface one{ //defines a constant const constant = ' constant value '; Defines an abstract method fun1 public function fun1 (); Defines the abstract method fun2 public function fun2 ();}? >
5. Because all the methods in the interface are abstract methods, it is not necessary to declare an abstract method to use the keyword "abstract" as an abstract class, which is added by default.
6. The access rights inside the interface must be public, the default is public, and the "private" and "protected" permissions cannot be used.
7. Interface is a special kind of abstract class, all the methods are abstract methods, so the interface can not produce an instance object.
8. We can use the "extends" keyword to allow an interface to inherit another interface.
Interface-Extends one{ function fun3 (); function Fun4 ();}
9. We define a subclass of an interface to implement the keyword that all abstract methods in the interface use are "implements", rather than "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 single-inheritance, a class can have only one parent class, but a class may implement multiple interfaces, which is equivalent to a class complying with multiple specifications. To implement multiple interfaces using implements, you must implement the methods in all the interfaces to instantiate the object.
11.PHP can not only implement multiple interfaces, but also inherit a class at the same time to implement multiple interfaces, it is important to inherit the class to implement the interface.
<?php//uses extends to inherit a class using implements to implement multiple interfaces class Test extends class name one implements interface one, interface two, ... {//All methods in the interface must be implemented to instantiate the object ... }