Code example for the PHP parent class to call the subclass method

Source: Internet
Author: User
This article mainly introduces the code example for the PHP parent class to call the subclass method. For more information about the code example, you can refer to the code that suddenly finds that you need to call the subclass method in the parent class, it has never been used before, and can be found through practice. Example:

The code is as follows:
/**
* The parent class calls the subclass method base class.
* @ Author LNMP100
*
*/
Class BaseApp
{
/**
* Call the subclass method
* @ Version creation time:
*/
Function _ run_action ()
{
$ Action = "index ";
$ This-> $ action ();
}
}

Class DefaultApp extends BaseApp
{

/**
* This method will be called in the parent class
*/
Function index ()
{
Echo "DefaultApp-> index () invoked ";
}

Function Go (){
// Call the parent class
Parent: _ run_action ();
}
}

$ Default = new defaapp app ();
$ Default-> Go ();
// DefaultApp-> index () invoked will be displayed

?>

However, it seems that this is not the parent class's subclass, but the method that the subclass calls itself, because the instantiation is a subclass. if you instantiate the parent class, there is a problem with the method that the subclass can be called.

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.