Search code examples
phpstaticoverriding

Overriding static methods in 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();
    }
};

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>";    
};

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

Why? What can I do?

EDIT

I'm running PHP 5.2.17


Solution

  • 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();
    

    Some things to take into account:

    • It is generally better not to use print/echo when generating a view content. Instead try to create the string and do the print/echo as a last step. This makes it easier to later write tests.

    Example:

    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();
    
    • 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