有没有这样的功能,所以我可以这样做:
populate_array($array, 'key', $value)
而不是这样做:
$array['key'] = $value;
?
我问这个是因为我想规避在使用魔法 getter 和 setter 并且想要设置数组属性时发生的“间接修改重载属性”错误:
class MyClass
{
private $string;
private $array;
public function __set($attribute, $value)
{
$this->$attribute = $value;
}
public function __get($attribute)
{
if (isset($attribute))
{
return $this->$attribute;
}
}
}
$obg = new MyClass();
$obg->string = 'Hey it works!';
$obg->array = ['This works', 'too!'];
$obg->array['key'] = 'This will throw `Indirect modification of overloaded property MyClass::$array has no effect`';