Here’s the context:
$values = $form->getValues();
foreach($values as $key=>$value) {
$obj->{$key} = $value;
}
If $key is a valid key, __set($name, $value) is not called. If $key is not a valid key, it is. Here’s what my set looks like:
public function __set($name, $value) {
$method = 'set' . ucfirst($name);
if(method_exists($method)) {
$this->$method($value);
} else {
throw new RuntimeException('Attempt to access a non-existant property with method ' . $method);
}
}
In the object to which the __set($name, $value) method belongs, all properties are private and underscored. So for key ‘name’ there would be the following:
private $_name;
public function setName($name) {
$this->_name = $name;
return $this;
}
I know that it isn’t called because I tried inserting an exception just after the $method = 'set' . ucfirst($name);. That exception was hit when $name did not reference a valid setter, but was not hit when it didn’t. It should have been hit every time. Anyone have any clue what’s happening here?
This is by design. From the manual
If you have
publicproperties matching$key, they will be set because they are accessible.Also, your use of
method_exists()is incorrect. It should be