Download the PHP package selective/container without Composer
On this page you can find all versions of the php package selective/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package container
selective/container
Description
A PSR-11 container implementation with optional autowiring.
Requirements
- PHP 8.1+
Installation
Usage
Enable Autowiring
The container is able to automatically create and inject dependencies for you. This is called "autowiring".
To enable autowiring you have to add the ConstructorResolver
:
Defining DI Container Definitions
You can use a factories (closures) to define injections.
Defining Multiple DI Container Definitions
Service Providers
Service providers give the benefit of organising your container definitions along with an increase in performance for larger applications as definitions registered within a service provider are lazily registered at the point where a service is retrieved.
To build a service provider create a invokable class and return the definitions (factories) you would like to register.
Set definitions directly
In addition to defining entries in an array of factories / callbacks, you can also set the value directly as shown below:
Fetching DI container entries
To fetch a value use the get
method:
Testing
- Make sure that your container will be recreated for each test. You may use the phpunit
setUp()
method to initialize the container definitions. - You can use the
set()
method to overwrite existing container entries.
Mocking
The set
method can also be used to set mocked objects directly into the container.
This example requires phpunit:
Slim 4 integration
Example to boostrap a Slim 4 application using the container:
The container.php
file must return an array of factories (closures):
PhpStorm Integration
If you use PhpStorm, then create a new file .phpstorm.meta.php
in your project root directory and copy/paste the following content:
Performance Comparison
selective/container
is about:
- 11% faster then
php-di/php-di
. - 5.4% faster then
league/container
.
All tests where made with enabled autowiring.
Migrating from PHP-DI
This PSR-11 container implementation mimics the behavior of PHP-DI.
If you already use factories for your container definitions, the switch should be very simple.
Replace this:
... with this:
That's it.
Credits
- Dominik Zogg (chubbyphp)
Similar libraries
- https://github.com/chubbyphp/chubbyphp-container
- http://php-di.org/
- https://container.thephpleague.com/
License
The MIT License (MIT). Please see License File for more information.