The application keywords of the interface in the php class are interface and implements. The interface is a special abstract class with all member attributes abstract or constants. Implements mainly serves constraints and specifications for class names, methods owned by the class, and passed parameters, which is a bit like the abstract abstract class.
Application of interfaces in class
1. Keyword: interface
2. Keywords: implements
1. Introduction and creation of interfaces
Interface: A special abstract class in which all member properties are abstract or constants.
rule:
1. All classes are abstract methods.
2. There is no need to add abstract to abstract method.
3. The interface abstract method attribute is public.
4. Member attributes must be constants.
The format code is as follows:
interface demo { const NAME = "Constant Object Properties"; function myfun1(); //Abstract Methodfunction myfun2(); //Abstract method, no specific writing logic is required}
1. Definition and call of interfaces
<?php interface Cinema { const film = 'Pirates of the Caribbean'; public function show(); } class Order implements Cinema { public function show() { echo "The theater interface is open<br>"; } } $face = new Order(); echo $face->show(); echo Cinema::film;
Note: In the example above, one thing you should pay attention to is that the method name of the interface is show. The class inheriting the interface must have the show method, otherwise an error will be reported. In other words, the interface method is fake, and what really works is the method in the inherited class. Is the interface a bit similar to the abstract class of php?
Second, the parameters are strictly constrained
<?php interface Cinema { public function show(Order $show,$num); } // Display normalclass Order implements Cinema { public $number='0011 row'; public function show(Order $show,$num) { echo $show->number.$num; } } $face= new Order(); $face->show(new Order,$num='3 people');//Output 0011Row3people
Note: The above example inherits the interface class. When calling the interface method, the passed parameters must be the same as the parameter name in the interface. Otherwise, there will be an error.
3. Inheritance between interfaces and calling interface parameters
<?php interface Cinema { public function show(); } interface Cinema1 extends Cinema { public function show1(Order1 $object,$num); } class Order implements Cinema1 { public function show() { echo "Ready<br>"; } public function show1(Order1 $object,$num) { //var_dump($object); echo $object->number."$num<br>"; } } class Order1 { public $number="0012 row"; function fun(){ echo ' ================='; } } $show = new Order1; $show->fun(); $test = new Order(); $test->show(); $test->show1($show,$num='6 people'); // Output ===============Ready to be0012Row6people
Note: As shown in the example above, interface Cinemal1 inherits interface Cinemal, and class Order inherits interface Cinemal1. I don't know if you found that there are two methods in the class Order, one is show and the other is show1, and no one can be missing. If one is missing, a fatal error will be reported. Order1 in show1(Order1 $object,$num) must be the same as the name of the inherited class. If it is different, a fatal error will also be reported. So what if an interface is inherited by multiple classes and the class names are different? Then you have to use self, the following will be mentioned
4. Multiple inheritances of one interface
<?php interface demo { const NAME = "Movie Name"; function fun1(); function fun2(); } interface demo2 { function fun3(); function fun4(); } interface demo3 { const TEST = "Here is the test test"; function fun5(); } class MyDemo implements demo, demo2 { function fun1() { echo "Hello"; } function fun2() { echo "----------"; } function fun3() { echo "I'm fine too<br />"; } function fun4() { echo "Everyone is well<br />"; } } class YourDemo extends MyDemo implements demo3 { function fun5() { echo "Referring to interface after inheriting the class"; } } $p = new YourDemo; $p->fun1(); $p->fun2(); $p->fun3(); $p->fun4(); $p->fun5();
The above output
Hello -------------------------------------------------------------------------------------------------------------------------------
Everyone is well
Referring to the interface after inheriting the class
In the above example, we can see that the interface is defined using the keyword interface and the keyword implements is used to implement the methods in the interface. Let me give you another example:
<?php //Define the interfaceinterface User{ function getDiscount(); function getUserType(); } class VipUser implements User{ //VIP user interface implementation private $discount = 0.8; // VIP user discount coefficient function getDiscount() { return $this->discount; } function getUserType() { return "VIP User"; } } class Goods{ var $price = 88; var $vc; function run(User $vc){ //Define the User interface type parameter, and you don't know which user it is at this time $this->vc = $vc; $discount = $this->vc->getDiscount(); $usertype = $this->vc->getUserType(); echo $usertype."Commodity Price:".$this->price*$discount; } } $display = new Goods(); $display ->run(new VipUser); //VIPUser product price:70.4
This example demonstrates a simple application of a PHP interface. In this example, the User interface implements user discounts, while the specific discount coefficient is implemented in the VipUser class. Finally, product category Goods implements different user quotations based on the User interface.
Finally, summarize:
The difference between abstract classes and interfaces
Interfaces are special abstract classes, and can also be regarded as the specification of a model. The roughly differentiation between interfaces and abstract classes is as follows:
1. A subclass If implements an interface, it must implement all methods in the interface (regardless of whether it is needed); if it inherits an abstract class, it only needs to implement the required methods.
2. If the method name defined in an interface has changed, then all subclasses that implement this interface need to update the method name simultaneously; and if the method name in an abstract class changes, the method name corresponding to its subclass will not be affected, but will just become a new method (a relatively old method implementation).
3. Abstract classes can only be inherited single. When the functions that a subclass needs to implement need to be inherited from multiple parent classes, an interface must be used.
The above is a detailed explanation of the use of implements in php introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!