开发者

How do late static bindings work in this scenario?

The following code outputs 'x as set in class A', h开发者_如何学JAVAow would I make it output 'x as set in class B' without changing class B?

<?php

class A
{
    public static $x = 'x as set in class A';

    public static function getX()
    {
        return self::$x;
    }
}


class B extends A
{
    public static $x = 'x as set in class B';
}


echo B::getX();


self always refers to the class, where it is defined. What you are looking for is "Late Static Binding" (as you already suggest, but dont use). The static keyword within a code block refers to the "actual" class, means: Either the called class (XY::method()), or the class of the called object ($x->method()).

return static::$x;

The static keyword at the property declaration has nothing to to with LSB. It is just the common declartion for class properties.

Note, that LSB is not available in PHP<5.3

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜