Class inheritance in PHP 5.2: Overriding static variable in extension class?

Posted by Christoffer on Stack Overflow See other posts from Stack Overflow or by Christoffer
Published on 2010-06-10T19:31:49Z Indexed on 2010/06/10 19:42 UTC
Read the original article Hit count: 306

Filed under:
|

Hi,

I need to bea be able to use a static variable set in a class that extends a base class... from the base class.

Consider this:

class Animal {
    public static $color = 'black';

    public static function get_color()
    {
        return self::$color;
    }
}

class Dog {
    public static $color = 'brown';
}

echo Animal::get_color(); // prints 'black'
echo Dog::get_color(); // also prints 'black'

This works wonderfully in PHP 5.3.x (Dog::get_color() prints 'brown') since it has late static binding. But my production server runs PHP 5.2.11 and so I need to adapt my script.

Is there a somewhat pretty workaround to solve this issue?

Cheers!
Christoffer

© Stack Overflow or respective owner

Related posts about php

Related posts about inheritance