开发者

php5 static properties

Is it possible to get property default value in static context an property value in non-static context?

class A{

$property = 1;

static function test(){
    echo $this->property;
}

function test1(){
    echo $this->property;
}
}

$v = new A();
A::test();
A::test1()

;

outputs 11开发者_StackOverflow中文版


static function test(){
    echo $this->property;
}

This is not the correct way to use $this. You should not use $this in static function, or any function which is is statically called.

You will get unwanted result from this.

In functions which are statically called, $this will not be the object of Same Class.

Edit :

To get default value you can use

class A{

static $property = 1;

static function test(){
    echo self::$property;
}

function test1(){
    echo self::$property;  // or you can use class name instead of self
}
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜