7

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

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
IluTov
  • 6,807
  • 6
  • 41
  • 103
  • 13
    Use `static` instead of `self`. – air4x Nov 01 '12 at 09:41
  • 1
    possible duplicate of [when using self, parent, static and how?](http://stackoverflow.com/questions/10504129/when-using-self-parent-static-and-how) – deceze Nov 01 '12 at 09:42
  • not a duplicate, didn't work for me. – IluTov Nov 01 '12 at 09:44
  • Then it's a duplicate of [Static Inheritance prior to PHP 5.3](http://stackoverflow.com/questions/8581435/static-inheritance-prior-to-php-5-3?lq=1). Why are you still on 5.2? – deceze Nov 01 '12 at 09:47
  • It's similar, but how should I call a static method, using the ReflectionClass? – IluTov Nov 01 '12 at 10:00
  • Maybe your page shouldn't be a static class :) – Ja͢ck Nov 01 '12 at 10:02
  • The page class isn't static. Just the method display is static. Also, I can't make an instance of the ReflectionClass, because display() is static. – IluTov Nov 01 '12 at 10:05
  • Look at the second answer in that thread. Late static binding is simply not really possible in PHP 5.2. That's why they added it to the language in 5.3. – deceze Nov 01 '12 at 10:16
  • I understand that. My question was, how can I do a workaround using this ReflectionClass, when I don't have a $this keyword, and I want to call a static method – IluTov Nov 01 '12 at 10:18
  • 2
    Why is the `display()` method static? Don't do it, if you already have an object. And then inheritance works with `$this`. – Sven Nov 01 '12 at 10:22
  • I don't have an object, I don't want to make an object if I just call this one method anyway – IluTov Nov 01 '12 at 10:23
  • 1
    Then you don't get inheritance in return. :) `B::foo()` will pass through to `A:foo()` due to static binding and it will look to `A::foo()` as if it was called directly. Nothing really you can do about it. – deceze Nov 01 '12 at 10:28
  • Nice! :) I guess I'm gonna have to override the display() method until I upgrade to PHP 5.3 – IluTov Nov 01 '12 at 10:41
  • @Ilija Tovilo : Why don't you want to make an object ? It will be way easier to test, to code, more powerful, and the memory cost is trivial - or even 0. – theredled Nov 01 '12 at 11:49

2 Answers2

8

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

Franz Holzinger
  • 913
  • 10
  • 20
Andrés Fortier
  • 1,705
  • 11
  • 12
  • Thanks a lot for your answer! Ok, it worked with non-static methods. What I do with printing out is following: ob_start(); $this->output = ob_get_contents(); ob_get_clean(); Is this too dirty? – IluTov Nov 01 '12 at 12:22
  • This was meant, so I can print stuff out by closing the php tags, inserting my html code, and opening the php tags again. So I get syntax highlighting. – IluTov Nov 01 '12 at 12:23
  • 1
    Well, that's a most of all a matter of taste and each person's view on "elegant" coding. I prefer leaving the ob_start(); mechanism for times when it is the only choice. From my POV returning a string is a more elegant approach, at the cost of loosing syntax highlighting. As many other things is programming there is always a trade-off and you should choose the one that fits best your needs. However, if you go with the ob_start() approach I would advise to create a "output grabber" object that encapsulates that in a method, so that you don't repeat the ob_* process N times. – Andrés Fortier Nov 01 '12 at 13:23
0

Returned back to this question. Was looking for solution for Symfony (5.4). And I finally came with this "Service - method call" solution.

#services_dev.yaml:
Company\Core\PinGenerator\PinGenerator:
    calls:
        - [setDebugMode, [true]]

#PinGenerator:
class PinGenerator implements PinGeneratorInterface
{

    public static bool $inDebugMode = false;

    public static function setDebugMode(bool $inDebugMode): void
    {
        self::$inDebugMode = $inDebugMode;
    }

    public static function generate(int $length = self::DEFAULT_PIN_CODE_LENGTH, bool $numbersOnly = true): string
    {
        if (!self::$inDebugMode) {
            return PinGeneratorProd::generate($length, $numbersOnly);
        } else {
            return PinGeneratorDev::generate($length, $numbersOnly);
        }
    }
}

Honesly hoping, that this will help someone, someday.

Fappie.
  • 482
  • 6
  • 18