Dependency injection
<?php
/*
This class can be extended to support automatic dependency injection.
The child class can have injected dependencies and needs only to call the parent (i.e. this) constructor.
public function __construct(SubClass $subClass=null) {
parent::__construct();
}
*/
abstract class SelfInjector {
public function __construct() {
$reflector = new ReflectionClass($this);
$constructor = $reflector->getConstructor();
$dependencies = $constructor->getParameters();
foreach ($dependencies as $dependency) {
$variableName = $dependency->name;
if (!!$class = $dependency->getClass()) {
$this->{$variableName} = new $class->name;
}
}
}
}