Download the PHP package quanta/container without Composer
On this page you can find all versions of the php package quanta/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download quanta/container
More information about quanta/container
Files in quanta/container
Package container
Short Description Minimalist dependency injection container implementing Psr-11
License MIT
Homepage https://github.com/quantaphp/container
Informations about the package container
Quanta Psr-11 container
This package provides a minimalist dependency injection container implementing Psr-11.
The goal is to implement a container working out of the box with minimal configuration, implementing interface aliasing and a basic autowiring mechanism.
- Getting started
- Basic usage
- Interface aliasing
- Autowiring
Getting started
Require php >= 7.4
Installation composer require quanta/container
Run tests php ./vendor/bin/phpunit
Testing a specific php version using docker:
docker build . --build-arg PHP_VERSION=7.4 --tag quanta/container/tests:7.4
docker run --rm quanta/container/tests:7.4
Basic usage
- container entries are defined using any iterable as long as keys can be casted as strings
- any non callable value is returned as is like an associative array
- any callable value is treated as a factory building the associated value (the results are cached so
the callable is run only once and the same value is returned on every
->get()
call)
Interface aliasing
- interface names associated to strings are treated as aliases
Autowiring
The container will try to build instances of non defined classes using simple rules to infer constructor parameter values:
- when the type of a parameter is a defined interface name, its value is retrieved from the container
- when the type of a parameter is a class name, its value is retrieved from the container (and also autowired if not defined)
- when the type of a parameter is not an interface/class name, its default value is used if present
- null is used as a fallback when the parameter allows null
- a
Quanta\Container\ContainerException
is thrown when:- no value can be infered for a parameter (not an interface/class name as type, no default value, not allowing null)
- trying to infer the value of a parameter with union/intersection type, without default value, not allowing null (php 8.0/8.1)
- trying to autowire an abstract class or a class with protected/private constructor
- the
->has()
method returns true for any existing classes - the objects built through autowiring are cached
A factory must be defined when more control over the class instantiation is needed.