When extending a class that gets automatically injected with dependencies through the container, one has to define all the base class' dependencies again in the derived class' constructor.
class A {
public function __construct( CacheRepository $cache ) { /* ... */ }
}
Then B looks like:
class B extends A {
public function __construct( CacheRepository $cache, Router $router ) {
parent::__construct( $cache );
// ...
}
}
Which is tedious and error-prone.
So, I suggest to add a public method to Container that resolves the dependencies of a base class. Following is a mock-up that is but briefly tried out.
class B extends A {
public function __construct( Container $container, Router $router ) {
$container->resolveParent( $this );
// ...
}
}
With the method resolveParent( $object ) looking like this:
public function resolveParent( $object )
{
// Get the reflection of the current class
$reflector = new \ReflectionClass( $object );
// Get its parent
if( ! ( $parent = $reflector->getParentClass() ) )
return;
// Get its constructor
if( ! ( $constructor = $parent->getConstructor() ) )
return;
// Get its dependencies
$dependencies = $constructor->getParameters();
// Conventionally resolve dependencies
$instances = $this->resolveDependencies( $dependencies );
// Invoke the parent constructor on the object
$constructor->invokeArgs( $object, $instances );
}
Any cons before I implement and PR this?
I don't like the idea of this:
Thanks for your feedback. I'll leave it be then and use the resolve callback and extend method to adapt some classes.
Most helpful comment
I don't like the idea of this: