我正在尝试构建一个动态关联数组值查找函数(在一个类中):
class Family
{
public static $members = array(
'one' => array(
'child' => 0,
'children' => 5
),
'two' => array(
'child' => 2,
'children' => null
)
);
public static function resolveMemberValue()
{
$chain = func_get_args();
$lookup = 'members' . '[\'' . implode('\'][\'', $chain) . '\']';
var_dump( $lookup );
return static::$$lookup;
}
}
Family::resolveMemberValue('one', 'child');
但这会导致:
string(23) "members['one']['child']"
致命错误:访问未声明的静态属性:Family::$members['one']['child'] in /family.php on line 23
PHP致命错误:访问未声明的静态属性:第 23 行 /family.php 中的 Family::$members['one']['child']
但是,复制转储值并粘贴到脚本中+附加美元符号,它会返回预期的内容:
var_dump( Family::$members['one']['child'] );
整数(0)
我需要这个的原因是,因为它将与多个变量一起使用,并从生成器函数中调用。
片段有什么问题?