Wish: Getter/Setter support for 3rd party classes in DI

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.

Won’t be added to Yii 2 but it’s already in Yii 3.

Great, thank you!
BTW: Are there plans to make constructor arguments configurable by name instead of of just order? This way it would be nicer to define, not all argument would be necessary and a DI definition via ID or interface could even merge with defaults defined with the class.

Currently not but it could be added later. Only DI container code needs to be modified in order to add support for it.