7

Let's start off with some code:

class Super {

    protected static $color;

    public static function setColor($color){
        self::$color = $color;
    }

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

}

class ChildA extends Super { }

class ChildB extends Super { }

ChildA::setColor('red');
ChildB::setColor('green');

echo ChildA::getColor();
echo ChildB::getColor();

Now, late static binding in PHP 5.3 using the static keyword works great with static methods, so I assumed it would do the same magic on static variables. Well, seems it doesn't. The example above does not print out "red" and then "green" as I first expected, but "green" and "green". Why doesn't this work on variables when it works on methods? Is there any other way to achieve the effect I expected?

1 Answer 1

13

Late static binding will only work for new definitions of variables / methods. Thus, in your example, the $color property of Super will always be modified instead of ChildA or ChildB. To make use of late static binding, you need to use the static keyword instead of self. Furthermore, you need to redefine the $color property of your ChildA and ChildB classes:

class Super {

    protected static $color;

    public static function setColor($color){
        // static instead of self
        static::$color = $color;
    }

    public static function getColor() {
        // static instead of self
        return static::$color;
    }

}

class ChildA extends Super {
    protected static $color;
}
class ChildB extends Super {
    protected static $color;
}

ChildA::setColor('red');
ChildB::setColor('green');

echo Super::getColor(); // prints nothing (NULL = ''), expected
echo ChildA::getColor();// prints red
echo ChildB::getColor();// prints green
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, Lekensteyn. That does sort of ruin some of the comfort of inheritance a little bit, but I'll manage :)
Thanks this really helped. I fell into the same trap; I was missing redefining the variable in the child classes! Do you have any literature on this as I seemed to miss this vital piece in the PHP docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.