如何使用变量访问动态属性? [英] How to access dynamic property by using variable?
本文介绍了如何使用变量访问动态属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请参阅:
$class_members = get_class_vars(__CLASS__);
foreach($class_members as $key => $value)
{
if (strpos($key, '_output') === 0)
{
// I want to eval() this
$code = '$this->' . $key . ' = 0;';
}
}
假设我想将值0
分配给所有以_output
开头的类成员.我计划使用eval
. 好主意还是坏主意?
Assume I want to assign the value 0
to all class members that begin with _output
. I plan to use eval
. Good or bad idea?
推荐答案
您不需要eval()
.您可以像$this->{$key}
一样使用变量:
You don't need eval()
for this. You can use a variable as in $this->{$key}
:
foreach($class_members as $key => $value)
{
if (strpos($key, '_output') === 0)
{
// Look mom, no eval()!
$this->{$key} = 0;
}
}
这篇关于如何使用变量访问动态属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文