The application keywords of interfaces in php are interface and implements. interfaces are special abstract classes whose member attributes are all abstract or constants. let's look at several instances. Class interface
The application keywords of interfaces in php are interface and implements. interfaces are special abstract classes whose member attributes are all abstract or constants. let's look at several instances.
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 fun1 ();
- Function fun2 (); // abstract method.
- }
2. Application and specification of interfaces
Interface reference is different from the class inheritance keyword extends. Inheritance can only be single, and interfaces can use the keyword implements to separate multiple references with commas.
1. Format: common class reference interface
- Class MyPc implements demo, demo2, demo3 {
- ...
- }
2. Format: example of an abstract class application interface
- Abstract class MyPc implements demo, demo2, demo3 {
- ...
- }
3. Format: inherit the coexistence of parent class reference interfaces
- Class MyPc extends Root implements demo, demo2, demo3 {
- ...
- }
The interface is inherited first, and the interface is inherited by multiple interfaces.
4. Format: interface and interface inheritance
- Interface demo3 extends demo {
- ...
- }
The instance code is as follows:
-
- Interface demo {
- Const NAME = "NAME ";
- Function fun1 ();
- Function fun2 ();
- }
- Interface demo2 {
- Function fun3 ();
- Function fun4 ();
- }
- Interface demo3 {
- Const TEST = "Test ";
- Function fun5 ();
- }
- Class MyPc implements demo, demo2 {
- Function fun1 (){
- Echo "++ ";
- }
- Function fun2 (){
- Echo "---------- ";
- }
- Function fun3 (){
- Echo & quot; 1111111111 ";
- }
- Function fun4 (){
- Echo & quot; 2222222222 ";
- }
- }
- Class MyPs extends MyPc implements demo3 {
- Function fun5 (){
- Echo "references interfaces after inheriting classes ";
- }
- }
- $ P = new MyPs;
- $ P-> fun1 ();
- $ P-> fun2 ();
- $ P-> fun3 ();
- $ P-> fun4 ();
- $ P-> fun5 ();
- ?>
For example, the interface is defined by the keyword interface, and the keyword implements is used to implement the methods in the interface, and must be fully implemented.
The instance code is as follows:
-
- // Define the interface
- Interface User {
- Function getDiscount ();
- Function getUserType ();
- }
- // VIP user interface implementation
- Class VipUser implements User {
- // VIP user discount coefficient
- Private $ discount = 0.8;
- Function getDiscount (){
- Return $ this-> discount;
- }
- Function getUserType (){
- Return "VIP user ";
- }
- }
- Class Goods {
- Var $ price = 100;
- Var $ vc;
- // Define the User interface type parameter. at this time, you do not know who the User is.
- Function run (User $ vc ){
- $ 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); // it can be more user types
- ?>
Run this example and output:
VIP User Product Price: 80 RMB
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.
This example only demonstrates the usage of the PHP interface and does not involve science or not.
Implement multiple interfaces
PHP can also implement multiple interfaces when inheriting a class:
- Class subclass extends parent class implemtns Interface 1, Interface 2 ,...
- {
- ......
- }
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.