在重载方法__set中,我将成员设置为同一个实例。将值更改为小写时,方法在执行预期行为之前运行两次。
是否有其他方法可以设置实例成员以防止过滤器和验证过程在更改键值后运行两次?
class Foo
{
public function __construct(array $structure = array())
{
$this->extend($structure);
}
public function extend(array $structure)
{
foreach ($structure as $key => $value)
$this->$key = $value;
}
public function __set($name, $value)
{
if(is_array($value) && $this->isAssoc($value))
$value = new static($value);
$key = strtolower($name);
$this->$key = $value;
}
protected function isAssoc(array $array)
{
$keys = array_keys($array);
$filtered = array_filter($keys, 'is_string');
$count = count($filtered);
return (bool)$count;
}
}