mead-steve / container
Simple Dependency Injection Container.
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/mead-steve/container
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2019-10-05 15:09:36 UTC
README
Very basic dependency injection container.
Build status
branch | status |
---|---|
master |
Example Usage
Setup a resource in the container:
use \Meadsteve\Container\Container; use \Meadsteve\Container\Singleton; $MyContainer = new Container(); $MyContainer->DependancyOne = new Dependancy(); $MyContainer->MyObject = function(Container $Container) { return new MyObject($Container->DependancyOne); };
Then when you need an instance of MyObject:
$InstanceOfMyObject = $MyContainer->MyObject;
For some heavy objects you may not want to run the construction logic each time. Then you may want to use the singleton pattern. This is possible with the provided class:
$MyContainer->DBUser = "DBGuy"; $MyContainer->Password = "SuperSecret10"; $MyContainer->DBBasedObject = new Singleton(function(Container $Container) { return new DBObject($Container->DBUser, $Container->Password); });
Which is then retrieved in exactly the same way:
$DBInstance = $MyContainer->DBBasedObject