PHP implements multiple inheritance through interfaces

Source: Internet
Author: User
[Switch] PHP implements multiple inheritance through interfaces. Although PHP classes are single-inherited, multiple inheritance can be implemented through other special methods, such as using interfaces, you only need to abstract the features of a class as an interface and implement the interface to make the object have multiple identities. in this way, you can simulate multiple inheritance. The following is an example of simulating multiple inheritance using interfaces. the source code is as follows: & lt ;?? Interface? U [to] PHP implements multiple inheritance through interfaces

Although PHP classes are single-inherited, multiple inheritance can be implemented in other special ways. for example, if you use interfaces to abstract the features of a class as interfaces, the interface is implemented to make the object have multiple parts, so that you can simulate multiple inheritance.


The following is an example of simulating multiple inheritance using interfaces. the source code is as follows:

  1. Interface? UserInterface {? // Define the User interface?
  2. ? Function? Getname ();?
  3. }?
  4. Interface? TeacherInterface {? // Teacher related interfaces?
  5. ? Function? GetLengthOfService ();?
  6. }?
  7. Class? User? Implements? UserInterface? {? // Implement the UserInterface interface?
  8. ? Private? $ Name? =? "Tom ";?
  9. ? Public? Function? GetName (){?
  10. ?? Return? $ This-> name ;?
  11. ?} ?
  12. }?
  13. Class? Teacher? Implements? TeacherInterface? {? // Implement the TeacherInterface interface?
  14. ? Private? $ LengthOfService? =? 5 ;? //? Working age? ?
  15. ? Public? Function? GetLengthOfService (){?
  16. ?? Return? $ This-> lengthOfService ;?
  17. ?} ?
  18. }?
  19. //? Inherits from the User class and implements the TeacherInterface interface .?
  20. Class? GraduateStudent? Extends? User? Implements? TeacherInterface? {?
  21. ? Private? $ Teacher ?; ?
  22. ? Public? Function? _ Construct (){?
  23. ?? $ This-> teacher? =? New? Teacher ();?? ?
  24. ?}? ?
  25. ? Public? Function? GetLengthOfService (){?
  26. ?? Return? $ This-> teacher-> getLengthOfService ();?
  27. ?} ?
  28. }?
  29. Class? Act {?
  30. ? // Note that the type prompt here is changed to the interface type?
  31. ? Public? Static? Function? GetUserName (UserInterface? $ _ User ){?
  32. ?? Echo? "Name? Is? "?.? $ _ User-> getName ()?. "
    ";?
  33. ?} ?
  34. ? // The type prompt here is changed to the TeacherInterface type .?
  35. ? Public? Static? Function ?? GetLengthOfService (TeacherInterface? $ _ Teacher ){?
  36. ?? Echo? "Age? Is? "?. $ _ Teacher-> getLengthOfService ()?. "
    ";?
  37. ?} ?
  38. }?
  39. $ GraduateStudent? =? New? GraduateStudent ();?
  40. Act: getUserName ($ graduateStudent );?
  41. Act: getLengthOfService ($ graduateStudent );?
  42. // The result is an object with multiple identities, as we need .?
  43. ?>?


The running result is as follows:

Name is tom

Age is 5

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.