Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ddrv/php-container
Simple PSR-11 Container
https://github.com/ddrv/php-container
dependency-injection dependency-injection-container di php php-library psr-11
Last synced: 3 days ago
JSON representation
Simple PSR-11 Container
- Host: GitHub
- URL: https://github.com/ddrv/php-container
- Owner: ddrv
- License: mit
- Created: 2020-07-23T09:28:27.000Z (over 4 years ago)
- Default Branch: 2.x
- Last Pushed: 2023-12-12T19:22:33.000Z (11 months ago)
- Last Synced: 2024-11-02T01:19:08.975Z (13 days ago)
- Topics: dependency-injection, dependency-injection-container, di, php, php-library, psr-11
- Language: PHP
- Homepage: https://packagist.org/packages/ddrv/container
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[![Latest Stable Version](https://img.shields.io/packagist/v/ddrv/container.svg?style=flat-square)](https://packagist.org/packages/ddrv/container)
[![Total Downloads](https://img.shields.io/packagist/dt/ddrv/container.svg?style=flat-square)](https://packagist.org/packages/ddrv/container/stats)
[![License](https://img.shields.io/packagist/l/ddrv/container.svg?style=flat-square)](https://github.com/ddrv/php-container/blob/master/LICENSE)
[![PHP](https://img.shields.io/packagist/php-v/ddrv/container.svg?style=flat-square)](https://php.net)# ddrv/container
Simple PSR-11 Container
# Install
```bash
composer require ddrv/container:^2.1
```# Using
```php
value('var', 'value1');
$container->get('var'); // returns 'value1'/*
* Set factory for service
*/
$container->service('service', function(ContainerInterface $container) {
$service = new stdClass();
$service->var = $container->get('var');
return $service;
});
$service1 = $container->get('service');
$service2 = $container->get('service');
$equal = $service1 === $service2; // true
$service1->var; // 'value1'
$service2->var; // 'value1'
$service1->var = 'value2';
$service1->var; // 'value2'
$service2->var; // 'value2'/*
* Set factory for recreated instance
*/
$container->instance('instance', function(ContainerInterface $container) {
$instance = new stdClass();
$instance->var = $container->get('var');
return $instance;
});
$instance1 = $container->get('instance');
$instance2 = $container->get('instance');
$equal = $service1 === $service2; // false
$instance1->var; // 'value1'
$instance2->var; // 'value1'
$instance1->var = 'value2';
$instance1->var; // 'value2'
$instance2->var; // 'value1'/*
* Aliasing
*/
$container->service('some-service', function() {
return new ArrayObject();
});
$container->bind('alias', 'some-service');
$service = $container->get('alias');/*
* Delegating to other containers
*//** @var ContainerInterface $delegate1 */
/** @var ContainerInterface $delegate2 */
$delegate1->has('key-1'); // true
$delegate1->has('key-2'); // false$delegate2->has('key-1'); // false
$delegate2->has('key-2'); // true$container->has('key-1'); // false
$container->has('key-2'); // false$container->delegate($delegate1);
$container->has('key-1'); // true
$container->has('key-2'); // false$container->delegate($delegate2);
$container->has('key-1'); // true
$container->has('key-2'); // true
```