PHP parent class calls subclass method instance_PHP tutorial

WBOY
Release: 2016-07-13 10:50:12
Original
1088 people have browsed it

This article will introduce to you some examples of PHP parent classes calling subclass methods. I hope this tutorial will be helpful to you.

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. Examples are as follows:

/**
* This method will be called in the parent class
​​*/
Function index()
                                  {
echo "DefaultApp->index() invoked";
}
The code is as follows
 代码如下 复制代码

/**
* 父类调用子类方法 基类
* @author LNMP100
*
*/
class BaseApp
{
/**
* 调用子类方法
* @version 创建时间:2013-07-10
*/
function _run_action()
{
$action = "index";
$this->$action();
        }
}

class DefaultApp extends BaseApp
{

    /**
     * 此方法将在父类中调用
     */
    function index()
        {
            echo "DefaultApp->index() invoked";
        }

    function  Go(){
        //调用父类
        parent::_run_action();
    }
}

$default=new DefaultApp();
$default->Go();
//将显示DefaultApp->index() invoked

?>

Copy code

/**
* 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
{

function Go(){
//Call the parent class
          parent::_run_action();
}
}

$default=new DefaultApp();
$default->Go();
//DefaultApp->index() invoked

will be displayed We called in the go() method of the parent class $default->Go(); swim is a method defined by the subclass (note that we did not define abstract function swim() in the parent class), indicating that this is not polymorphic. We not only called the method, but also called the member, which is ok.
http://www.bkjia.com/PHPjc/632664.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632664.htmlTechArticleThis article will introduce to you the examples of PHP parent class calling subclass methods. I hope this tutorial will be useful to all students. It helps. Today I suddenly discovered that I need to call the method of the subclass in the parent class...
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