Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/meadsteve/container
Very basic dependency injection container
https://github.com/meadsteve/container
Last synced: 5 days 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 (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-12-19T16:52:49.000Z (almost 11 years ago)
- Last Synced: 2024-10-11T03:11:14.456Z (about 1 month 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 | [![Build Status](https://travis-ci.org/meadsteve/Container.png?branch=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
```