Container
in package
implements
ContainerInterface
PSR-11 compatible Dependency Injection Container.
This container supports:
- Service registration (singletons, factories, instances)
- Auto-wiring via reflection
- Service aliases
- Lazy loading
Usage:
$container = new Container();
// Register a singleton (created once, reused)
$container->singleton(LanguageFacade::class, function($c) {
return new LanguageFacade($c->get(LanguageRepositoryInterface::class));
});
// Register a factory (new instance each time)
$container->bind(SomeService::class, function($c) {
return new SomeService();
});
// Register an existing instance
$container->instance('config', $configArray);
// Auto-wire a class (container resolves dependencies automatically)
$service = $container->get(LanguageFacade::class);
Tags
Table of Contents
Interfaces
- ContainerInterface
- PSR-11 compatible container interface.
Properties
- $aliases : array<string, string>
- Service aliases
- $bindings : array<string, array{factory: callable, singleton: bool}>
- Registered service bindings
- $instance : Container|null
- The global container instance (singleton pattern for app-wide access)
- $instances : array<string, mixed>
- Resolved singleton instances
- $resolving : array<string, bool>
- Services currently being resolved (for circular dependency detection)
Methods
- alias() : void
- Register an alias for a service.
- bind() : void
- Register a binding in the container.
- call() : mixed
- Create a new instance with method injection.
- get() : mixed
- Finds an entry of the container by its identifier and returns it.
- getInstance() : Container
- Get the global container instance.
- getRegisteredServices() : array<string|int, string>
- Get all registered service IDs.
- getTyped() : T
- Get a service with type inference for static analysis.
- has() : bool
- Returns true if the container can return an entry for the given identifier.
- instance() : void
- Register an existing instance in the container.
- make() : object
- Make a new instance of a class (always fresh, never cached).
- reset() : void
- Reset the container (primarily for testing).
- setInstance() : void
- Set the global container instance.
- singleton() : void
- Register a singleton binding.
- autoWire() : object
- Auto-wire a class by resolving its constructor dependencies.
- canAutoWire() : bool
- Check if a class can be auto-wired.
- doGet() : mixed
- Internal service resolution logic.
- resolve() : mixed
- Resolve a service from the container.
- resolveAlias() : string
- Resolve an alias to its actual service name.
- resolveDependencies() : array<int, mixed>
- Resolve constructor dependencies.
- resolveDependency() : mixed
- Resolve a single dependency.
- resolveParametersWithOverrides() : array<int, mixed>
- Resolve parameters with optional overrides.
Properties
$aliases
Service aliases
private
array<string, string>
$aliases
= []
$bindings
Registered service bindings
private
array<string, array{factory: callable, singleton: bool}>
$bindings
= []
$instance
The global container instance (singleton pattern for app-wide access)
private
static Container|null
$instance
= null
$instances
Resolved singleton instances
private
array<string, mixed>
$instances
= []
$resolving
Services currently being resolved (for circular dependency detection)
private
array<string, bool>
$resolving
= []
Methods
alias()
Register an alias for a service.
public
alias(string $alias, string $abstract) : void
Parameters
- $alias : string
-
The alias name
- $abstract : string
-
The actual service name
bind()
Register a binding in the container.
public
bind(string $abstract, callable $factory[, bool $singleton = false ]) : void
Parameters
- $abstract : string
-
The abstract type or service name
- $factory : callable
-
Factory function that creates the service
- $singleton : bool = false
-
Whether to cache the instance
call()
Create a new instance with method injection.
public
call(string $class, string $method[, array<string, mixed> $params = [] ]) : mixed
Parameters
- $class : string
-
The class name
- $method : string
-
The method name
- $params : array<string, mixed> = []
-
Additional parameters to pass
Return values
mixed —The method return value
get()
Finds an entry of the container by its identifier and returns it.
public
get(string $id) : mixed
Parameters
- $id : string
-
Identifier of the entry to look for
Return values
mixed —Entry
getInstance()
Get the global container instance.
public
static getInstance() : Container
Return values
ContainergetRegisteredServices()
Get all registered service IDs.
public
getRegisteredServices() : array<string|int, string>
Return values
array<string|int, string>getTyped()
Get a service with type inference for static analysis.
public
getTyped(T> $id) : T
This method is identical to get() but provides better type inference for static analysis tools like Psalm. Use this when retrieving services by their class name.
Parameters
- $id : T>
-
The service class name
Tags
Return values
T —The resolved service instance
has()
Returns true if the container can return an entry for the given identifier.
public
has(string $id) : bool
Parameters
- $id : string
-
Identifier of the entry to look for
Return values
boolinstance()
Register an existing instance in the container.
public
instance(string $abstract, mixed $instance) : void
Parameters
- $abstract : string
-
The abstract type or service name
- $instance : mixed
-
The service instance
make()
Make a new instance of a class (always fresh, never cached).
public
make(string $class[, array<string, mixed> $params = [] ]) : object
Parameters
- $class : string
-
The class name
- $params : array<string, mixed> = []
-
Constructor parameters to override
Return values
object —The new instance
reset()
Reset the container (primarily for testing).
public
reset() : void
setInstance()
Set the global container instance.
public
static setInstance(Container|null $container) : void
Parameters
- $container : Container|null
-
Container instance or null to reset
singleton()
Register a singleton binding.
public
singleton(string $abstract, callable $factory) : void
The factory will be called once, and the result cached for subsequent calls.
Parameters
- $abstract : string
-
The abstract type or service name
- $factory : callable
-
Factory function that creates the service
autoWire()
Auto-wire a class by resolving its constructor dependencies.
private
autoWire(string $class) : object
Parameters
- $class : string
-
The class name
Tags
Return values
object —The instantiated class
canAutoWire()
Check if a class can be auto-wired.
private
canAutoWire(string $class) : bool
Parameters
- $class : string
-
The class name
Return values
booldoGet()
Internal service resolution logic.
private
doGet(string $id) : mixed
Parameters
- $id : string
-
The service identifier
Tags
Return values
mixed —The resolved service
resolve()
Resolve a service from the container.
private
resolve(string $id) : mixed
Parameters
- $id : string
-
The service identifier
Tags
Return values
mixed —The resolved service
resolveAlias()
Resolve an alias to its actual service name.
private
resolveAlias(string $id) : string
Parameters
- $id : string
-
The service identifier
Return values
string —The resolved service name
resolveDependencies()
Resolve constructor dependencies.
private
resolveDependencies(array<string|int, ReflectionParameter> $parameters) : array<int, mixed>
Parameters
- $parameters : array<string|int, ReflectionParameter>
-
Constructor parameters
Tags
Return values
array<int, mixed> —Resolved dependencies
resolveDependency()
Resolve a single dependency.
private
resolveDependency(ReflectionParameter $parameter) : mixed
Parameters
- $parameter : ReflectionParameter
-
The parameter to resolve
Tags
Return values
mixed —The resolved dependency
resolveParametersWithOverrides()
Resolve parameters with optional overrides.
private
resolveParametersWithOverrides(array<string|int, ReflectionParameter> $parameters, array<string, mixed> $overrides) : array<int, mixed>
Parameters
- $parameters : array<string|int, ReflectionParameter>
-
Parameters to resolve
- $overrides : array<string, mixed>
-
Override values by parameter name
Return values
array<int, mixed> —Resolved dependencies