Why doesn't late static binding work with variables in PHP 5.3?
Asked Answered
W

1

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?

Wershba answered 14/5, 2011 at 7:18 Comment(0)
B
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
Bradlybradman answered 14/5, 2011 at 7:29 Comment(2)
Thanks a lot, Lekensteyn. That does sort of ruin some of the comfort of inheritance a little bit, but I'll manage :)Wershba
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.Paramount

© 2022 - 2024 — McMap. All rights reserved.