Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 547
IluTov Avatar asked Nov 01 '12 09:11

IluTov


People also ask

Can I override static method in PHP?

Here comes the call of static method with static keyword. In case there is no overridden function then it will call the function within the class as self keyword does. If the function is overridden then the static keyword will call the overridden function in the derived class.

Can we overriding static method?

Can we override a static method? No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time. So, we cannot override static methods.

What is static method overriding?

Overloading is the mechanism of binding the method call with the method body dynamically based on the parameters passed to the method call. Static methods are bonded at compile time using static binding. Therefore, we cannot override static methods in Java.

What are static methods in PHP?

Definition and Usage. The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.


1 Answers

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

like image 164
Andrés Fortier Avatar answered Sep 22 '22 00:09

Andrés Fortier