Application (interfaces and implements) of interfaces in PHP object-oriented development)

Source: Internet
Author: User
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:

  1. Interface demo {
  2. Const NAME = "constant object attributes ";
  3. Function fun1 ();
  4. Function fun2 (); // abstract method.
  5. }

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

  1. Class MyPc implements demo, demo2, demo3 {
  2. ...
  3. }

2. Format: example of an abstract class application interface

  1. Abstract class MyPc implements demo, demo2, demo3 {
  2. ...
  3. }

3. Format: inherit the coexistence of parent class reference interfaces

  1. Class MyPc extends Root implements demo, demo2, demo3 {
  2. ...
  3. }

The interface is inherited first, and the interface is inherited by multiple interfaces.

4. Format: interface and interface inheritance

  1. Interface demo3 extends demo {
  2. ...
  3. }

The instance code is as follows:

  1. Interface demo {
  2. Const NAME = "NAME ";
  3. Function fun1 ();
  4. Function fun2 ();
  5. }
  6. Interface demo2 {
  7. Function fun3 ();
  8. Function fun4 ();
  9. }
  10. Interface demo3 {
  11. Const TEST = "Test ";
  12. Function fun5 ();
  13. }
  14. Class MyPc implements demo, demo2 {
  15. Function fun1 (){
  16. Echo "++ ";
  17. }
  18. Function fun2 (){
  19. Echo "---------- ";
  20. }
  21. Function fun3 (){
  22. Echo & quot; 1111111111 ";
  23. }
  24. Function fun4 (){
  25. Echo & quot; 2222222222 ";
  26. }
  27. }
  28. Class MyPs extends MyPc implements demo3 {
  29. Function fun5 (){
  30. Echo "references interfaces after inheriting classes ";
  31. }
  32. }
  33. $ P = new MyPs;
  34. $ P-> fun1 ();
  35. $ P-> fun2 ();
  36. $ P-> fun3 ();
  37. $ P-> fun4 ();
  38. $ P-> fun5 ();
  39. ?>

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:

  1. // Define the interface
  2. Interface User {
  3. Function getDiscount ();
  4. Function getUserType ();
  5. }
  6. // VIP user interface implementation
  7. Class VipUser implements User {
  8. // VIP user discount coefficient
  9. Private $ discount = 0.8;
  10. Function getDiscount (){
  11. Return $ this-> discount;
  12. }
  13. Function getUserType (){
  14. Return "VIP user ";
  15. }
  16. }
  17. Class Goods {
  18. Var $ price = 100;
  19. Var $ vc;
  20. // Define the User interface type parameter. at this time, you do not know who the User is.
  21. Function run (User $ vc ){
  22. $ This-> vc = $ vc;
  23. $ Discount = $ this-> vc-> getDiscount ();
  24. $ Usertype = $ this-> vc-> getUserType ();
  25. Echo $ usertype. "product price:". $ this-> price * $ discount;
  26. }
  27. }
  28. $ Display = new Goods ();
  29. $ Display-> run (new VipUser); // it can be more user types
  30. ?>

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:

  1. Class subclass extends parent class implemtns Interface 1, Interface 2 ,...
  2. {
  3. ......
  4. }

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.