The Problem:
In the overload method __set I set members to the same instance. When altering the value to lowercase the method is running twice before the expected behavior is performed.
The Question:
Is there an alternative way to set a member of the instance to prevent the filter and validation process to run twice after altering the key value?
The Code:
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;
}
}