Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重写父函数_Php_Class_Parent_Overloading - Fatal编程技术网

Php 重写父函数

Php 重写父函数,php,class,parent,overloading,Php,Class,Parent,Overloading,这是一个被问了很多次的问题,但没有一个答案在这里适用 我有以下PageBase课程: <?php class PageBase { public $templatefile = '404'; function Before() {} function After() {} function Display() { $smarty = System::GetSmarty();

这是一个被问了很多次的问题,但没有一个答案在这里适用

我有以下
PageBase
课程:

<?php
    class PageBase {
        public $templatefile = '404';

        function Before() {}
        function After() {}

        function Display() {
            $smarty = System::GetSmarty();
            $smarty->display('views/'.$this->templatefile.'.tpl');
        }

        function Run() {
            self::Before();
            self::Display();
            self::After();
        }
    }
我遇到的问题是加载了正确的模板文件(
home
),但是
home::Before()
中的代码没有通过实例化的
$home->Before()
,而是运行
PageBase::Before()

解决这个问题的正确方法是什么?PHP是否提供了覆盖父函数的方法


就我个人而言,我会使用一个统一的接口,在设置变量后调用父级,如下所示:

    <?php
Interface PageBase {
    public function Before();
    public function After();
    public function Display();
    public function Run();
}

class Page Implements PageBase {
    public $templatefile = '404';

    function Before() {}
    function After() {}

    public function Set($page) {
        if (class_exists($page)) {
            $class = new $page($page);
            return $class;
        }

        return false;
    }

    function Display()
    {
        $smarty = System::GetSmarty();
        $smarty->display('views/' . $this->templatefile . '.tpl');
    }

    function Run()
    {
        self::Before();
        self::Display();
        self::After();
    }
}

class Home extends Page Implements PageBase
{
    public $templatefile = 'home';

    function After() { parent::After(); }
    function Display() { parent::Display(); }
    function Run() { parent::Run(); }

    function Before() {
        var_dump($_SERVER);
    }
}

$Page = new Page(); //Create a new page instance for all of the functionality
$home = $Page->Set("Home"); //Returns a new class of Home
$home->Display(); //Displays the templatefile "home"

请参阅。或者更好,不要使用静力学。即,代替
self::Before()使用
$this->Before()
@AlexHowansky谢谢,不知道我怎么会错过它,这就解决了它!
    <?php
Interface PageBase {
    public function Before();
    public function After();
    public function Display();
    public function Run();
}

class Page Implements PageBase {
    public $templatefile = '404';

    function Before() {}
    function After() {}

    public function Set($page) {
        if (class_exists($page)) {
            $class = new $page($page);
            return $class;
        }

        return false;
    }

    function Display()
    {
        $smarty = System::GetSmarty();
        $smarty->display('views/' . $this->templatefile . '.tpl');
    }

    function Run()
    {
        self::Before();
        self::Display();
        self::After();
    }
}

class Home extends Page Implements PageBase
{
    public $templatefile = 'home';

    function After() { parent::After(); }
    function Display() { parent::Display(); }
    function Run() { parent::Run(); }

    function Before() {
        var_dump($_SERVER);
    }
}

$Page = new Page(); //Create a new page instance for all of the functionality
$home = $Page->Set("Home"); //Returns a new class of Home
$home->Display(); //Displays the templatefile "home"