Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/drhino/container

Lightweight PSR-11 Container
https://github.com/drhino/container

composer-library container-injection dependency-injection dependency-manager php php-library psr-11

Last synced: about 22 hours ago
JSON representation

Lightweight PSR-11 Container

Awesome Lists containing this project

README

        

# PSR-11: Container


Install with Composer:
```bash
composer require drhino/container:^3.0.0
```


## Example use:

```php
class Dependency
{
public string $var = '';
}

class Init
{
public function __construct(Dependency $dependency, String $value)
{
$dependency->var = $value;
}
}
```

```php
$container = new drhino\Container\Container;

$container
->add(Init::class, [
// The arguments of the constructor
'dependency' => $container->ref(Dependency::class),
'value' => 'Hello world',
])
->add(Dependency::class)
;
```


> Use $container->ref() to reference an identifier before it has been added into the container.


```php
// Executes __construct()
$init = $container->get(Init::class);

// Prints 'Hello world'
echo $container->get(Dependency::class)->var;
```


## Signature:

The following are exactly the same:

```php
$container->add(Dependency::class);
```
```php
$container->add($id = Dependency::class, $resource = Dependency::class, $arguments = []);
```
```php
$container->add($id = Dependency::class, $arguments = []);
```