Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/drhino/container
- Owner: drhino
- License: mit
- Created: 2022-08-02T21:40:14.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-10T17:54:10.000Z (over 1 year ago)
- Last Synced: 2024-09-29T23:23:53.625Z (about 2 months ago)
- Topics: composer-library, container-injection, dependency-injection, dependency-manager, php, php-library, psr-11
- Language: PHP
- Homepage:
- Size: 58.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 = []);
```