silex - PHP Lazy loading with Pimple Dependency Injection Container? -
silex - PHP Lazy loading with Pimple Dependency Injection Container? -
recently have started using pimple (together silex). depending on how pimple used can either service locator or dependency injection container. aware of reasons why service locator pattern should avoided. nevertheless 1 thing seems haunting me moment when dependency instance created.
in case of dependency injection, instances of required classes created , passed constructor:
class foo{ public $depend1; public $depend2; public function __construct($depend1, $depend2) { $this->depend1=$depend1; $this->depend2=$depend2; } public function task1() { homecoming $this->depend1->run(); } public function task2() { homecoming $this->depend2->run(); } }
in case pass container class constructor, dependency instances not need created until needed.
class foo{ public $app; public function __construct(\silex\application $app) { $this->app=$app; } public function task1() { homecoming $app['depend1']->run(); } public function task2() { homecoming $app['depend2']->run(); } }
as result, if going phone call 1 of 2 methods on foo class, in first illustration still both dependency instances created. code simple example, expect problem grow in case of more complex classes more dependency structures. notice other dependency injection containers utilize proxy classes, not able find library. there improve alternative lazy load dependencies pimple?
in cases not problem. if initializing dependencies become actual performance problem, should either split service 2 separate services, or, create proxy lazyloads dependency on first call.
there library php provides automatic proxy generation, called proxymanager. without knowing requirements, first guess it's overkill you. don't worry until sure there actual performance bottleneck can solve way.
php silex pimple
Comments
Post a Comment