Download the PHP package semperton/container without Composer
On this page you can find all versions of the php package semperton/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download semperton/container
More information about semperton/container
Files in semperton/container
Package container
Short Description Dynamic PSR-11 dependency injection container with reflection based autowiring.
License MIT
Informations about the package container
Semperton Container
A lightweight PSR-11 dependency injection container
with reflection based autowiring.
Installation
Just use Composer:
Container requires PHP 8.2+
Interface
The container ships with these public methods:
Usage
Classes can be resolved automatically as long as they do not require any special configuration (autowiring). Autowiring is disabled by default and has to be enabled explicitly:
Without autowiring, only configured entries, factories and the container itself (, ) can be resolved.
Note that the container only creates (shared) instances once. It does not work as a factory. You should consider the Factory Pattern or use the method instead:
The method will automatically resolve the dependency for . It always builds a new instance, either with the registered factory or through autowiring. Existing entries and delegate entries are never returned by .
Parameter resolving
Factory and constructor params are resolved in this order:
- params passed to (by name)
- class-typed params the container can resolve
- declared default values
Note that a resolvable class type always wins over a default value: receives the container's when one can be resolved. Builtin types (, , ...), union and intersection types are never guessed. Pass them to or use a factory, otherwise a is thrown. Variadic params only receive values passed explicitly as an array (), otherwise they stay empty.
If an entry is known but one of its dependencies is missing, and throw a (not a ), with the original exception as .
Configuration
You can configure the container with definitions. are always treated as factories and should be used to bootstrap class instances. If you like to use as factories: .
The method also treats as factories.
Immutability
Once the container is created, it is immutable. If you like to add an entry after instantiation, keep in mind that the method always returns a new container instance:
A new container instance does not share already resolved instances with its origin, so replaced entries are always taken into account:
Upgrading from 3.x
- PHP 8.2+ is required.
- Autowiring is disabled by default. Call to restore the previous behavior.
- Factory params are no longer resolved by name. Replace with , or pass the value to .
- Containers returned by , and no longer share resolved instances with the original container. Services are rebuilt with the new configuration.
- on such a container returns the new container, not the original one.
- returns for classes that cannot be instantiated (abstract classes, private constructors).
- Exception classes are .
- implements .
- A missing dependency inside a factory throws a instead of letting the inner bubble up.
- Variadic params are no longer autowired. Pass them explicitly as an array.