从 PHP 中具有动态类名的类中获取静态属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1279081/
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
Getting static property from a class with dynamic class name in PHP
提问by treznik
I have this:
我有这个:
- one string variable which holds the class name (
$classname) - one string variable with holds the property name (
$propertyname)
- 一个保存类名的字符串变量 (
$classname) - 一个包含属性名称的字符串变量 (
$propertyname)
I want to get that property from that class, the problem is, the property is static and I don't know how to do that.
我想从那个类中获取那个属性,问题是,这个属性是静态的,我不知道怎么做。
If the property weren't static, it would have been:
如果该属性不是静态的,它将是:
$classname->$propertyname;
if the property were a method, I could have used call_user_function
如果属性是一个方法,我可以使用 call_user_function
call_user_func(array($classname, $propertyname));
But in my case, am I just lost. I am however hoping that it is possible. With the thousands of functions that PHP has, he'd better have something for this as well. Maybe I'm missing something?
但就我而言,我只是迷路了。然而,我希望这是可能的。有了 PHP 拥有的数千个函数,他最好也为此提供一些东西。也许我错过了什么?
Thanks!
谢谢!
Edit:
编辑:
- for those with eval()solutions: thanks, but it is out of the question
- for those with get _class _vars()solutions: thanks, but it seems it returns "the default properties of the given class"(php.net), and yes, I would like that value to be changable (even though it does help me in some of the cases)
- 对于那些有eval()解决方案的人:谢谢,但这是不可能的
- 对于那些使用get _class _vars()解决方案的人:谢谢,但它似乎返回“给定类的默认属性”(php.net),是的,我希望该值是可变的(即使它确实对我有帮助)在某些情况下)
回答by Andrew Moore
If you are using PHP 5.3.0 or greater, you can use the following:
如果您使用的是 PHP 5.3.0 或更高版本,则可以使用以下内容:
$classname::$$propertyname;
Unfortunately, if you are using a version lower than 5.3.0, you are stuck using eval()(get_class_vars()will not work if the value is dynamic).
不幸的是,如果您使用的是低于 5.3.0 的版本,您将无法使用eval()(get_class_vars()如果该值是动态的,则将无法使用)。
$value = eval($classname.'::$'.$propertyname.';');
$value = eval($classname.'::$'.$propertyname.';');
EDIT:I've just said get_class_vars()wouldn't work if the value is dynamic, but apparently, variable static members are part of "the default properties of a class". You could use the following wrapper:
编辑:我刚刚说过get_class_vars()如果值是动态的就行不通,但显然,可变静态成员是“类的默认属性”的一部分。您可以使用以下包装器:
function get_user_prop($className, $property) {
if(!class_exists($className)) return null;
if(!property_exists($className, $property)) return null;
$vars = get_class_vars($className);
return $vars[$property];
}
class Foo { static $bar = 'Fizz'; }
echo get_user_prop('Foo', 'bar'); // echoes Fizz
Foo::$bar = 'Buzz';
echo get_user_prop('Foo', 'bar'); // echoes Buzz
Unfortunately, if you want to set the value of the variable, you will still need to use eval(), but with some validation in place, it's not so evil.
不幸的是,如果你想设置变量的值,你仍然需要使用eval(),但是有了一些验证,它就不是那么邪恶了。
function set_user_prop($className, $property,$value) {
if(!class_exists($className)) return false;
if(!property_exists($className, $property)) return false;
/* Since I cannot trust the value of $value
* I am putting it in single quotes (I don't
* want its value to be evaled. Now it will
* just be parsed as a variable reference).
*/
eval($className.'::$'.$property.'=$value;');
return true;
}
class Foo { static $bar = 'Fizz'; }
echo get_user_prop('Foo', 'bar'); // echoes Fizz
set_user_prop('Foo', 'bar', 'Buzz');
echo get_user_prop('Foo', 'bar'); // echoes Buzz
set_user_prop()with this validation shouldbe secure. If people start putting random things as $classNameand $property, it will exit out of the function as it won't be an existing class or property. As of $value, it is never actually parsed as code so whatever they put in there won't affect the script.
set_user_prop()有了这个验证应该是安全的。如果人们开始将随机的东西作为$classNameand $property,它将退出函数,因为它不会是现有的类或属性。截至$value,它实际上从未被解析为代码,因此它们放入的任何内容都不会影响脚本。
回答by Bahad?r
I think this is the simplest:
我认为这是最简单的:
$foo = new ReflectionProperty('myClassName', 'myPropertyName');
print $foo->getValue();
回答by frodosghost
To return a variable value that is set by a Static Variable you need to call:
要返回由静态变量设置的变量值,您需要调用:
$static_value = constant($classname.'::'.$propertyname);
Check out the documentation :: PHP Constant Documentation
查看文档 :: PHP Constant Documentation
回答by nilamo
'eval' looks so close to 'evil', and I hate using it and/or seeing it in code. With a few ideas from other answers, here's a way to avoid it even if your php isn't 5.3 or higher.
'eval' 看起来非常接近于 'evil',我讨厌使用它和/或在代码中看到它。根据其他答案的一些想法,即使您的 php 不是 5.3 或更高版本,这里也有一种避免它的方法。
Changed to reflect testing based on a comment.
已更改以反映基于评论的测试。
class A {
static $foo = 'bar';
}
A::$foo = 'baz';
$a = new A;
$class = get_class($a);
$vars = get_class_vars($class);
echo $vars['foo'];
Outputs 'baz'.
输出'baz'。
回答by Steven Surowiec
You should be able to do something like:
您应该能够执行以下操作:
eval("echo $classname::$propertyname;");
I just did a quick test and got this to work for me. Not sure if there's a better way or not, but I wasn't able to find one.
我只是做了一个快速测试,让这个对我有用。不确定是否有更好的方法,但我找不到。
回答by mbirth
One thing I noticed is that you can't set variables which are protected in static classes as the eval() command runs in a scope outside the class. The only thing to get around this would be to implement a static method inside the/every class which runs the eval(). This method could be protected as the call_user_func() [to call the setter method] also runs from inside the class.
我注意到的一件事是,您无法设置在静态类中受保护的变量,因为 eval() 命令在类之外的范围内运行。解决这个问题的唯一方法是在/每个运行 eval() 的类中实现一个静态方法。这个方法可以受到保护,因为 call_user_func() [调用 setter 方法] 也从类内部运行。
回答by Garlou
You can use ReflectionClass:
您可以使用反射类:
class foo
{
private static $bar = "something";
}
$class = "foo";
$reflector = new ReflectionClass($class);
$static_vars = $reflector->getStaticProperties();
var_dump($static_vars["bar"]);
回答by Alex Weinstein
Potentially relevant: discussion on late static binding in PHP - When would you need to use late static binding?.
潜在相关:讨论 PHP 中的后期静态绑定 -您何时需要使用后期静态绑定?.
回答by firzen
get_class_varsis not same as get_object_vars.
get_class_vars与 不同get_object_vars。
I think get_clas_varsshould return the original property values.
我认为get_clas_vars应该返回原始属性值。
回答by Marco Demaio
Even if for you said evalis out of the question, prior PHP 5.3 the easiest solution is still by using eval:
即使你说的eval是不可能的,在 PHP 5.3 之前,最简单的解决方案仍然是使用eval:
eval("$propertyval = $classname::$propertyname;");
echo $propertyval;

