Recently I have started using Pimple (together with Silex). Depending on how Pimple is used it can either be a Service Locator or a Dependency Injection Container. I am aware of the reasons why a Service Locator pattern should be avoided. Nevertheless one thing which seems to be haunting me is the moment when the dependency instance is created.
In case of a Dependency Injection, instances of the required classes are created and passed to the constructor:
class Foo{
public $depend1;
public $depend2;
public function __construct($depend1, $depend2) {
$this->depend1=$depend1;
$this->depend2=$depend2;
}
public function task1() {
return $this->depend1->run();
}
public function task2() {
return $this->depend2->run();
}
}
In case we pass the container itself to the class constructor, the dependency instances do not need to be created until they are needed.
class Foo{
public $app;
public function __construct(\Silex\Application $app) {
$this->app=$app;
}
public function task1() {
return $app['depend1']->run();
}
public function task2() {
return $app['depend2']->run();
}
}
As a result, even if we are only going to call one of the two methods on the Foo class, in the first example still both dependency instances will be created. This code is a very simple example, but I expect the problem will grow in case of more complex classes with more dependency structures. I do notice some other Dependency Injection Containers use proxy classes, but was not able to find anything for this library. Is there any better alternative to lazy load the dependencies with Pimple?
In most cases this is not a problem. If initializing your dependencies become an actual performance problem, you should either split your service into two separate services, or, create a proxy that lazyloads the dependency on the first call.
There is a library for PHP that provides automatic proxy generation, called ProxyManager. Without knowing your requirements, my first guess is that it's probably overkill for you. Don't worry about this until you are sure that there is an actual performance bottleneck that you can solve this way.