Methods |
public
|
__construct()
Creates a new service container
Creates a new service container
|
#
|
public
static
|
hasGlobalContainer(): bool
Check if the global container is set
Check if the global container is set
Implemented by
|
#
|
public
static
|
getGlobalContainer(): ContainerInterface
Get the global container, creating it if necessary
Get the global container, creating it if necessary
Implemented by
|
#
|
public
static
|
setGlobalContainer(?ContainerInterface $container): void
Set or unset the global container
Set or unset the global container
Dispatches {@see BeforeGlobalContainerSetEvent} if the global container
will change.
Implemented by
|
#
|
public
|
inContextOf(class-string $id): static
Apply contextual bindings to a copy of the container
Apply contextual bindings to a copy of the container
Implemented by
|
#
|
public
|
get<T>(class-string<T> $id, mixed[] $args = []): T&object
Resolve a service from the container
Resolve a service from the container
Values in $args are passed to constructor parameters after:
- matching objects to parameters with compatible type declarations
- matching keys in
$args to parameters with the same name
- matching values to parameters by type and position
Throws
|
#
|
public
|
getAs<TService, T is TService>(class-string<T> $id, class-string<TService> $service, mixed[] $args = []): T&object
Resolve a partially-resolved service from the container
Resolve a partially-resolved service from the container
$id is resolved normally, but $service is passed to
{@see ServiceAwareInterface::setService()} instead of $id .
Throws
Implemented by
|
#
|
public
|
getClass<T>(class-string<T> $id): class-string<T>
Resolve a service from the container without returning an instance
Resolve a service from the container without returning an instance
Returns the class name of the object {@see get()} would return.
Implemented by
|
#
|
public
|
has(class-string $id): bool
Check if a service is bound to the container
Check if a service is bound to the container
|
#
|
public
|
hasSingleton(class-string $id): bool
Check if a shared service or instance is bound to the container
Check if a shared service or instance is bound to the container
Implemented by
|
#
|
public
|
hasInstance(class-string $id): bool
Check if a service resolves to a shared instance
Check if a service resolves to a shared instance
Implemented by
|
#
|
public
|
hasProvider(class-string $provider): bool
Check if a service provider is registered with the container
Check if a service provider is registered with the container
Implemented by
|
#
|
public
|
getProviders(): array<class-string>
Get a list of service providers registered with the container
Get a list of service providers registered with the container
Implemented by
|
#
|
public
|
bind<TService, T is TService>(
class-string<TService> $id,
((Closure(self): T)&object)|class-string<T>|null $class = null,
): $this
Bind a service to the container
Bind a service to the container
Subsequent requests for $id resolve to an instance of $class , or
$id if $class is null .
If $class is a closure, it is called every time $id is resolved.
Implemented by
|
#
|
public
|
bindIf<TService, T is TService>(
class-string<TService> $id,
((Closure(self): T)&object)|class-string<T>|null $class = null,
): $this
Bind a service to the container if it isn't already bound
Bind a service to the container if it isn't already bound
Implemented by
|
#
|
public
|
singleton<TService, T is TService>(
class-string<TService> $id,
((Closure(self): T)&object)|class-string<T>|null $class = null,
): $this
Bind a shared service to the container
Bind a shared service to the container
Subsequent requests for $id resolve to the shared instance created when
$id is first requested.
Implemented by
|
#
|
public
|
singletonIf<TService, T is TService>(
class-string<TService> $id,
((Closure(self): T)&object)|class-string<T>|null $class = null,
): $this
Bind a shared service to the container if it isn't already bound
Bind a shared service to the container if it isn't already bound
Implemented by
|
#
|
public
|
instance<TService, T is TService>(class-string<TService> $id, T&object $instance): $this
Bind a shared instance to the container
Bind a shared instance to the container
Implemented by
|
#
|
public
|
removeInstance(class-string $id): $this
Remove a shared instance from the container
Remove a shared instance from the container
Implemented by
|
#
|
public
|
addContextualBinding<TService, T is TService>(
class-string[]|class-string $context,
class-string<TService>|non-empty-string $id,
((Closure(self): T)&object)|class-string<T>|(T&object)|null $class = null,
): $this
Add a contextual binding to the container
Add a contextual binding to the container
Subsequent requests for $id from the given contexts resolve to:
- the return value of
$class (if it is a closure)
- an instance of
$class (if it is a string)
$class itself (if it is an object), or
- an instance of
$id (if $class is null )
If $id starts with '$' , it is matched with constructor parameters of
the same name, and $class cannot be null .
Implemented by
|
#
|
public
|
provider(
class-string $provider,
class-string[]|null $services = null,
class-string[] $excludeServices = [],
ContainerInterface::* $providerLifetime = ContainerInterface::LIFETIME_INHERIT,
): $this
Register a service provider with the container, optionally specifying
which of its services to bind or ignore
Register a service provider with the container, optionally specifying
which of its services to bind or ignore
For performance reasons, classes bound to the container with
{@see bind()} or {@see singleton()} are not loaded until they are
resolved. Classes registered with {@see provider()} are loaded
immediately to check for {@see HasServices}, {@see SingletonInterface}
and other implementations.
Parameters
$services |
Services of $provider to bind to
the container, or null to bind every service returned by
{@see HasServices::getServices()} (if implemented).
|
$excludeServices |
Services of $provider to exclude
from binding.
|
Throws
Implemented by
|
#
|
public
|
providers(
array<class-string|int, class-string> $providers,
ContainerInterface::* $providerLifetime = ContainerInterface::LIFETIME_INHERIT,
): $this
Register an array that maps services (usually interfaces) to service
providers (classes that extend or implement theā¦
Register an array that maps services (usually interfaces) to service
providers (classes that extend or implement the mapped service)
Multiple services may be mapped to the same service provider. Unmapped
providers are mapped to themselves.
Implemented by
|
#
|