问题描述
我有这个:
- 一个保存类名的字符串变量 (
$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;
如果属性是一个方法,我可以使用 call_user_function
if the property were a method, I could have used call_user_function
call_user_func(array($classname, $propertyname));
但就我而言,我只是迷路了.然而,我希望这是可能的.有了 PHP 拥有的数千个函数,他最好也为此准备一些东西.也许我错过了什么?
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?
谢谢!
- 对于那些有 eval() 解决方案的人:谢谢,但它已经结束了问题的
- 对于那些使用 get _class _vars() 解决方案的人:谢谢,但它似乎返回给定类的默认属性"(php.net),是的,我希望该值是可变的(即使它确实在某些方面对我有帮助)案例)
- 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)
推荐答案
如果您使用的是 PHP 5.3.0 或更高版本,您可以使用以下内容:
If you are using PHP 5.3.0 or greater, you can use the following:
$classname::$$propertyname;
不幸的是,如果您使用的是低于 5.3.0 的版本,您将无法使用 eval()
(get_class_vars()
如果值为动态).
我刚刚说过 get_class_vars()如果值是动态的,代码>
将不起作用,但显然,可变静态成员是类的默认属性" 的一部分.您可以使用以下包装器:
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:
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
不幸的是,如果你想设置变量的值,你仍然需要使用eval()
,但是有了一些验证,它没那么邪恶.
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.
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()
使用此验证应该是安全的.如果人们开始将随机的东西放在 $className
和 $property
中,它将退出函数,因为它不是现有的类或属性.从 $value
开始,它实际上从未被解析为代码,因此它们放入的任何内容都不会影响脚本.
set_user_prop()
with this validation should be secure. If people start putting random things as $className
and $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.
这篇关于从 PHP 中具有动态类名的类中获取静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!