Download the PHP package mmghv/dependency-pocket without Composer
On this page you can find all versions of the php package mmghv/dependency-pocket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mmghv/dependency-pocket
More information about mmghv/dependency-pocket
Files in mmghv/dependency-pocket
Package dependency-pocket
Short Description A new form of Dependency Injection in PHP
License MIT
Homepage https://github.com/mmghv/dependency-pocket
Informations about the package dependency-pocket
DependencyPocket
A new form of "Dependency Injection" in PHP
Not to be confused with Dependency Injection Container or Ioc Container
like Symfony DI Container, Pimple or Laravel Service Container,
DependencyPocket is not a DI Container
in that way, It's rather a new form of Dependency Injection
.
Essentially, there're two ways of dependency injection : Constructor Injection
and Setter Injection
,
I'd like to think of this technique as the third type, the Pocket Injection
.
Installation
Using composer
usage
We use this pocket inside our classes to hold the dependencies, we first create a new pocket :
Then we add the dependencies, We first define them (defining the Name
and the Type
of each dependency) :
Then we set our dependencies (set dependencies values) :
Then when we want to get a dependency we simply do :
Or we can use Property Overloading
to easily access our dependencies from our class (or subclasses) :
When it's useful and the technique to use it
Basically Its useful when your class has many dependencies which not all of them are required so you don't want your constructor to have all these dependencies but still need a way to easily change them in the subclasses and tests.
Imagine you have a class with 5 dependencies but only 2 of them are essential and the other 3 can be set to some default values, Then you extend this class and the subclass can resolve one of the two essential dependencies to a default value and needs to replace one of the optional dependencies of the parent class, You need to be able to do that and want your final class to have only one dependency in the constructor but still be able to change any default ones from any future subclasses as well as the ability to mock any of these dependencies in the tests.
Using DependencyPocket you can achieve that like the following without using a setter for each dependency and also mocking dependencies for tests is easier than using Setter Injection
method :
Then when we instantiate the ArticleManager
class we only need to pass one dependency like this :
But also we have the ability to easily replace any default dependencies when we want like this :
Contributing
This is a relatively new technique so any contributions (suggestions, enhancements to the technique used) are welcome. PSR-2 standards are used and tests should cover any changes in case of PRs.
License & Copyright
Copyright © 2016, Mohamed Gharib. Released under the MIT license.