有没有办法在没有 foreach 循环的情况下实现以下 PHP 类(否则不变)?
<?php
class foo {
private static $_thing = array(
'kee' => 'valyu'
);
public static function fetch($property, $key = '') {
if (property_exists('foo', $property)) {
if ($key == '') return self::$$property;
else {
# i cannot seem to do self::$$property[$key].
foreach (self::$$property as $_key => $_value) {
if ($_key == $key) return $_value;
}
}
}
return false;
}
}
var_dump(foo::fetch('bad')); # null
var_dump(foo::fetch('_thing')); # array(1) { ... }
var_dump(foo::fetch('_thing', 'bad')); # null
var_dump(foo::fetch('_thing', 'kee')); # string(5) "valyu"
?>
self::$$property[$key]
让我“访问未声明的静态属性:foo::$x”(x是$key字符串值中的第一个字符)。