PHP parent class calls subclass method instance _ PHP Tutorial

Source: Internet
Author: User
PHP parent class calls subclass method instance. This article will introduce to you the PHP parent class to call subclass method instances. I hope this tutorial will be helpful to you. Today, I suddenly discovered that the subclass method needs to be called in the parent class. This article will introduce to you the PHP parent class to call the subclass method instance. I hope this tutorial will be helpful to you.

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. 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

?>

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

$ Default-> Go ();

Swim is a subclass-defined method (note that abstract function swim () is not defined in the parent class), indicating that this is not a polymorphism. We not only called the method, but also called the members. it is OK.

Bytes. Today, we suddenly found that we need to call the subclass method in the parent class...

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.