Yii implements nice magic property support, but many 3rd Party classes do not despite they follow the getTheProperty() and setTheProperty() convention.
In order to have convenient DI support for 3rd party (Non-Yii) classes it would be nice to let the Container implementation use them.
Currently the container does this:
$object = $reflection->newInstanceArgs($dependencies);
foreach ($config as $name => $value) {
$object->$name = $value;
}
What about this implementation (Not tested, just as idea):
$object = $reflection->newInstanceArgs($dependencies);
foreach ($config as $name => $value) {
try {
$object->$name = $value;
}
catch (\Error $exc) {
// Catch the fatal error and try to use the setter
$setter = 'set'.$name;
if (method_exists($o, $setter)) {
$o->$setter($value);
} else {
throw $exc;
}
}
}
Discussion:
This would catch the fatal error caused by access to invisible properties, but would probably not work
for undeclared properties. Thus, another way to handle this would be to turn it around: Check if a setXxx method exists first and call it and just use direct setting if no setter exists.