https://github.com/appzcoder/container
Dependency Injection Container
https://github.com/appzcoder/container
container di-container psr-11
Last synced: 11 months ago
JSON representation
Dependency Injection Container
- Host: GitHub
- URL: https://github.com/appzcoder/container
- Owner: appzcoder
- Created: 2015-10-04T08:02:54.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-03-21T04:45:31.000Z (almost 8 years ago)
- Last Synced: 2024-12-09T07:38:39.558Z (about 1 year ago)
- Topics: container, di-container, psr-11
- Language: PHP
- Homepage: https://packagist.org/packages/appzcoder/container
- Size: 6.84 KB
- Stars: 8
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DI Container
> Dependency Injection Container
## Installation
1. Run
```
composer require appzcoder/container:"dev-master"
```
2. Add bellow lines to your script
```php
require 'vendor/autoload.php';
```
## Usage
```php
class Foo // Has dependencies
{
protected $bar;
protected $fooBar;
protected $name;
public function __construct(Bar $bar, $name='Sohel Amin', $param2=null) // Dependency Injected
{
$this->bar = $bar;
$this->name = $name;
}
public function setterMethod(FooBar $fooBar) // Dependency Injected on method
{
return $this->fooBar = $fooBar;
}
}
class Bar { } // No dependencies
class FooBar { } // No dependencies
// Instantiate the container
$container = new Appzcoder\Container\Container();
// Registering class with dependencies
$container->set('Foo');
// Registering class with another name
$container->set('foo', 'Bar');
// Binding a closure object with a name
$container->setInstance('FooBar', function () {
return new FooBar();
});
// Registering class with parameters
$container->set('Foo', 'Foo', ['param 1', 'param 2']);
// Binding an instance with a name
$instance = new FooBar();
$container->setInstance('FooBar', $instance);
// Binding an instance/object with container's array
$container['FooBar'] = new FooBar();
// Calling a setter method with dependencies
$container->set('Foo', 'Foo', ['param 1', 'param 2']);
$instance = $container->get('Foo');
$container->call([$instance, 'setterMethod'], ['param 1', 'param 2']);
// Accessing container or getting instances
$instance1 = $container->get('Foo');
$instance2 = $container['Foo']; // For this should have registered or bounded "Foo"
```
## Author
[Sohel Amin](http://www.sohelamin.com)