Home > Article > Backend Development > Code example of PHP parent class calling subclass method_PHP tutorial
Today I suddenly discovered that I need to call the method of the subclass in the parent class. I have never used it in this way before. I found that it can also be done through practice. Example:
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 DefaultApp();
$default->Go();
//DefaultApp->index() invoked
?>
But it feels like this is not called the parent class adjusting the subclass, but the subclass adjusting its own method. Because instantiation is a subclass, if you instantiate the parent class and can also adjust the subclass's method, there will be a problem.