如何修改 PHP 中 class 的静态属性?
How do I modify static properties of a class in 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 中是否可能出现这种情况或任何与这种情况相差甚远的情况?
你走在正确的轨道上,你只需要访问变量 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
考虑以下代码:
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 中是否可能出现这种情况或任何与这种情况相差甚远的情况?
你走在正确的轨道上,你只需要访问变量 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