The application keywords of interfaces in php are interface and implements. interfaces are special abstract classes whose member attributes are all abstract or constants. implements mainly refer to class names, the methods owned by the class, as well as the constraints and specifications of the passed parameters, are a bit like abstract class. Application of interfaces in the class 1. keyword: interface2. keyword: implements1. interface introduction and Creation Interface: a special abstract class where all member attributes are abstract or constants. Rule: 1. the application keywords of interfaces in the php class are interface and implements. interfaces are special abstract classes whose member attributes are all abstract or constants. implements mainly refer to class names, the methods owned by the class, as well as the constraints and specifications of the passed parameters, are a bit like abstract class.
Application of interfaces in the class
1. keyword: interface
2. keyword: implements
1. interface introduction and creation
Interface: A special abstract class in which all member attributes are abstract or constants.
Rules:
1. all classes are abstract methods.
2. abstract methods do not need to be added.
3. the interface abstract method property is public.
4. the member attribute must be a constant.
The format code is as follows:
Interface demo {const NAME = "constant object attributes"; function myfun1 (); // abstract method function myfun2 (); // abstract method, no specific writing logic}
1. Interface definition and call
";}}$ Face = new Order (); echo $ face-> show (); echo Cinema: film;
Note: In the preceding example,The method name of the interface is show, Inherited interfaceClass must have showOtherwise, an error is reported. That is to say, the interface method is false. what actually works is the method in the inherited class. is the interface a bit like the php abstract class here?
2. strict parameter constraints
Number. $ num ;}$ face = new Order (); $ face-> show (new Order, $ num = '3 people'); // output 3 persons in 0011 rows
Note: the preceding example inherits the interface class. when calling an interface method, the passed parameters must be the same as the parameter names in the interface. Otherwise, an error is reported.
3. inheritance between interfaces and parameters passed by calling interfaces
";} Public function show1 (Order1 $ object, $ num) {// var_dump ($ object); echo $ object-> number." $ num
";}} Class Order1 {public $ number =" 0012 "; function fun () {echo '==========================' ;}}$ show = new Order1; $ show-> fun (); $ test = new Order (); $ test-> show (); $ test-> show1 ($ show, $ num = '6 people '); // output ======================= ready, 0012 rows, 6 persons
Note: the preceding example shows that interface Cinemal1 inherits interface Cinemal and class Order inherits interface cinemal1. I don't know if you find that there are two methods in class Order: show, show1, and no less. if there is one fewer, a fatal error is reported. Show1 (Order1Order in $ object, $ num)1The name of the root inheritance class must be the same as the class Order.1. If they are different, a fatal error is also reported. What if an interface is inherited by multiple classes with different class names? So we need to use self, which will be mentioned below
4. Multiple inheritance for one interface
";} Function fun4 () {echo" good for everyone
";}} Class YourDemo extends MyDemo implements demo3 {function fun5 () {echo" reference interface after inheriting class ";}}$ p = new YourDemo; $ p-> fun1 (); $ p-> fun2 (); $ p-> fun3 (); $ p-> fun4 (); $ p-> fun5 ();
Above output
Hello ---------- I am also good for everyone to inherit the class and then reference the interface
In the above example, we can see that all interfaces are defined using the keyword interface, and the keyword implements is used to implement the methods in the interface. Another example is as follows:
Discount;} function getUserType () {return "VIP User" ;}} class Goods {var $ price = 88; var $ vc; function run (User $ vc) {// defines the User interface type parameter. at this time, you do not know the User $ this-> vc = $ vc; $ discount = $ this-> vc-> getDiscount (); $ usertype = $ this-> vc-> getUserType (); echo $ usertype. "Product Price :". $ this-> price * $ discount; }}$ display = new Goods (); $ display-> run (new VipUser); // VIP user product price: 70.4
This example demonstrates a simple application of a PHP interface. In this example, the User interface achieves the User discount, while the VipUser class implements the specific discount coefficient. Finally, Goods provides different User quotations based on the User interface.
Summary:
Differences between abstract classes and interfaces
An interface is a special abstract class and can also be seen as a model specification. The interface and abstract class are roughly different as follows:
1. if a subclass implements an interface, all methods in the interface must be implemented (whether or not required); if an abstract class is inherited, you only need to implement the required methods.
2. if the method name defined in an interface is changed, all subclasses implementing this interface must synchronously update the method name. if the method name in the abstract class is changed, the method name corresponding to its subclass is not affected, but is changed to a new method (relatively old method implementation ).
3. abstract classes can only be inherited. interfaces must be used when a subclass needs to implement functions inherited from multiple parent classes.
The above is the details of implements, an application of interfaces in the php class. For more information, see other related articles in the first PHP community!