在PHP中覆盖静态方法 [英] Overriding static methods in PHP

查看:219
本文介绍了在PHP中覆盖静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象的页面类,看起来像这样:

I have an abstract page class looking like this:

abstract class Page {
    public static function display() {
        self::displayHeader();
        self::displayContent();
        self::displayFooter();
    }

    public static function displayContent() {
        print "<p>some content</p>";
    }

    public static function displayHeader() {
        include_once(kContent . "HeaderContent.class.php");
        HeaderContent::display();
    }

    public static function displayFooter() {
        include_once(kContent . "FooterContent.class.php");
        FooterContent::display();
    }
};

我想以此为子类,并且仅覆盖displayContent方法,因此页眉和页脚将自动显示,但仍具有覆盖显示方法的选项,例如.js文件.

I would like to subclass from this, and only override the displayContent method, so the header and footer is being displayed automatically, but still having the option to override the display method, for example for .js files.

现在我还有另一个课,看起来像这样:

Now I have another class, looking like this:

class FooPage extends Page {
    public static function displayContent() {
        print "<p>Foo page</p>";    
};

现在,它没有调用FooPage的displayContent方法,而只是从超类中调用了该方法.

Now, instead of calling the FooPage's displayContent method, it just calls the one from the superclass.

为什么?我该怎么办?

编辑

我正在运行PHP 5.2.17

I'm running PHP 5.2.17

推荐答案

Ilija,PHP< 5.3没有"后期静态绑定"这就是为什么您可能遇到FooPage::displayContent未被调用的原因.如果您运行的是PHP 5.2,则无需执行任何操作(除了一些使用debug_backtrace()的黑客,老实说,我不建议在这种情况下使用它.)

Ilija, PHP < 5.3 doesn't have "Late Static Binding" and that's why you may be experiencing the FooPage::displayContent not being called. If you are running PHP 5.2 then there is nothing much to do (except for some hacks using debug_backtrace(), which honestly I wouldn't recommend for this situation).

现在,真正引起我注意的是您的方法都是静态的.是否有一个原因?他们为什么不实例方法?我会期望像这样:

Now, what it really calls my attention is that your methods are all static; is there a reason for this? Why aren't they instance methods? I would expect something like:

include_once(kContent . "HeaderContent.class.php");
include_once(kContent . "HeaderContent.class.php");

abstract class Page 
{
    protected $header;
    protected $footer;

    public function __construct()
    {
        $this->header = new HeaderContent();
        $this->footer = new FooterContent();
    }

    public function display() 
    {
        $this->displayHeader();
        $this->displayContent();
        $this->displayFooter();
    }

    public function displayContent() 
    {
        print "<p>some content</p>";
    }

    public function displayHeader() 
    {
        $this->header->display();
    }

    public function displayFooter() 
    {
        $this->footer->display();
    }
};

class FooPage extends Page 
{
    public function displayContent() 
    {
        print "<p>Foo page</p>";
    }
}

,然后在您的视图中,您将编写如下内容:

and later in your view you would write something like:

$page = new FooPage();
$page->display();

要考虑的一些事情:

  • 通常最好在生成视图内容时不要使用print/echo.相反,请尝试创建字符串并执行打印/回显作为最后一步.这使得以后编写测试变得更加容易.

示例:

public function display() 
{
    return 
           $this->displayHeader() . 
           $this->displayContent() . 
           $this->displayFooter();
}

public function displayContent() 
{
    return "<p>some content</p>";
}

public function displayHeader() 
{
    return $this->header->display();
}
....
$page = new FooPage();
echo $page->display();

  • 如果您需要在应用程序增长时执行此操作,则可以将页眉和页脚作为Page构造函数参数进行传递.只要它们是了解display()消息的对象(即多态)应该没事.
    • If you need to do it as your application grows, you can pass the header and footer as Page constructor parameters. As long as they are objects that understand the display() message (i.e. polymorphic) things should be ok.
    • HTH

      这篇关于在PHP中覆盖静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆