如何在PHP中修改类的静态属性? [英] How do I modify static properties of a class in PHP?
本文介绍了如何在PHP中修改类的静态属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑以下代码:
class MyClass
{
public static $test = 'foo';
public function example()
{
return Self::$test;
}
}
// What I'm trying to do
MyClass->$test = 'bar';
$test = new MyClass();
echo $test->example(); // Should return `bar` instead of `foo`.
在PHP中,这是否可能或遥不可及?
Is this or anything remotely close to this possible in PHP?
推荐答案
在正确的轨道上,您只需要以Class :: $ test
You're on the right track you just need to access the variable as Class::$test
class MyClass
{
public static $test = 'foo';
public function example()
{
return Self::$test;
}
}
MyClass::$test = 'bar';
$test = new MyClass();
echo $test->example(); // returns bar
这篇关于如何在PHP中修改类的静态属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文