Code example of PHP parent class calling subclass method_PHP tutorial

WBOY
Release: 2016-07-13 10:34:27
Original
1102 people have browsed it

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:

Copy code The code is as follows:

/**
* The parent class calls the subclass method base class
* @author LNMP100
*
*/
class BaseApp
{
/**
* Call subclass method
* @version Creation time: 2013-07-10
*/
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 DefaultApp();
$default->Go();
//DefaultApp->index() invoked

will be displayed

?>

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/751514.htmlTechArticleToday 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. Through practice Discovery is okay too. Example: Copy the code. The code is as follows: ?php /** * Parent class call...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template