PHP parent class calls subclass method instance

Source: Internet
Author: User
Today, I suddenly found that the subclass method needs to be called in the parent class, which has never been used before. It can also be found through practice. the instance code is as follows: & lt ;? Php *** parent class calls subclass method base class * @ author

Today, we suddenly found that the subclass method needs to be called in the parent class, which has never been used before. It can also be found through practice. the instance code is as follows:

  1. /**
  2. * The parent class calls the subclass method base class.
  3. * @ Author LNMP100
  4. *
  5. */
  6. Class BaseApp
  7. {
  8. /**
  9. * Call the subclass method
  10. * @ Version creation time:
  11. */
  12. Function _ run_action ()
  13. {
  14. $ Action = "index ";
  15. $ This-> $ action ();
  16. }
  17. }
  18. Class DefaultApp extends BaseApp
  19. {
  20. /**
  21. * This method will be called in the parent class
  22. */
  23. Function index ()
  24. {
  25. Echo "DefaultApp-> index () invoked ";
  26. }
  27. Function Go (){
  28. // Call the parent class
  29. Parent: _ run_action ();
  30. }
  31. }
  32. $ Default = new defaapp app ();
  33. $ Default-> Go ();
  34. // DefaultApp-> index () invoked will be displayed
  35. ?>

We called the go () method of the parent class.

$ Default-> Go ();

Swim is a subclass-defined method (note that we have not defined abstract function swim () in the parent class). This shows that this is not a polymorphism. we have not only called the method, but also called the members, yes.

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.