php 通过 $var::$reference 访问静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/675676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access a static variable by $var::$reference
提问by chuckg
I am trying to access a static variablewithin a class by using a variable class name. I'm aware that in order to access a functionwithin the class, you use call_user_func():
我试图通过使用变量类名访问类中的静态变量。我知道为了访问类中的函数,您可以使用call_user_func():
class foo {
function bar() { echo 'hi'; }
}
$class = 'foo';
call_user_func(array($class, 'bar')); // prints hi
However, this does not work when trying to access a static variablewithin the class:
但是,这在尝试访问类中的静态变量时不起作用:
class foo {
public static $bar = 'hi';
}
$class = "foo";
call_user_func(array($class, 'bar')); // nothing
echo $foo::$bar; // invalid
How do I get at this variable? Is it even possible? I have a bad feeling this is only available in PHP 5.3 going forward and I'm running PHP 5.2.6.
我如何获得这个变量?甚至有可能吗?我有一种不好的感觉,这仅在 PHP 5.3 中可用,而且我正在运行 PHP 5.2.6。
采纳答案by Paul Dixon
You can use reflectionto do this. Create a ReflectionClassobject given the classname, and then use the getStaticPropertyValue method to get the static variable value.
您可以使用反射来做到这一点。给定类名创建一个ReflectionClass对象,然后使用 getStaticPropertyValue 方法获取静态变量值。
class Demo
{
public static $foo = 42;
}
$class = new ReflectionClass('Demo');
$value=$class->getStaticPropertyValue('foo');
var_dump($value);
回答by Karol
I think there is much better (more elegant) way then creating ReflectionClass instance. I also edited this code (and my answer) after few comments. I added example for protected variables (you can't of course access them from outside the class, so I made static getter and call it using variable pattern as well). You can use it in few different ways:
我认为有比创建 ReflectionClass 实例更好(更优雅)的方法。几条评论后,我还编辑了此代码(和我的答案)。我添加了受保护变量的示例(您当然不能从类外部访问它们,所以我制作了静态 getter 并使用变量模式调用它)。您可以通过几种不同的方式使用它:
class Demo
{
public static $foo = 42;
protected static $boo = 43;
public static function getProtected($name) {
return self::$$name;
}
}
$var1 = 'foo';
$var2 = 'boo';
$class = 'Demo';
$func = 'getProtected';
var_dump(Demo::$$var1);
var_dump($class::$foo);
var_dump($class::$$var1);
//var_dump(Demo::$$var2); // Fatal error: Cannot access protected property Demo::$boo
var_dump(Demo::getProtected($var2));
var_dump($class::getProtected($var2));
var_dump($class::$func($var2));
Documentation is here:
文档在这里:
回答by Armin Ronacher
For calling static members you can use a code like this:
对于调用静态成员,您可以使用这样的代码:
call_user_func("MyClass::my_static_method");
// or
call_user_func(array("MyClass", "my_static_method"));
Unfortunately the only way to get static members from an object seems to be get_class_vars():
不幸的是,从对象获取静态成员的唯一方法似乎是get_class_vars():
$vars = get_class_vars("MyClass");
$vars['my_static_attribute'];
回答by Nanang El Sutra
Have you try this?
你试过这个吗?
class foo {
static public $bar = "Hi";
static public function bar() {
echo "Hi";
}
}
echo foo::$bar; // Output: Hi
foo::bar(); // Output: Hi
$class = "foo";
echo $class::$bar; // Output: Hi
$class::bar(); // Output: Hi
call_user_func($class, 'bar'); // Output: Hi

