https://github.com/meadsteve/container
Very basic dependency injection container
https://github.com/meadsteve/container
Last synced: 5 months ago
JSON representation
Very basic dependency injection container
- Host: GitHub
- URL: https://github.com/meadsteve/container
- Owner: meadsteve
- Created: 2013-01-29T20:37:53.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-12-19T16:52:49.000Z (over 11 years ago)
- Last Synced: 2025-02-15T12:55:45.967Z (5 months ago)
- Language: PHP
- Size: 141 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Container
=========Very basic dependency injection container.
Build status
------------| branch | status |
| ------ | ------ |
| master | [](https://travis-ci.org/meadsteve/Container) |Example Usage
=========Setup a resource in the container:
```php
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:```php
$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:
```php
$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:
```php
$DBInstance = $MyContainer->DBBasedObject
```