Documentation

Container
in package
implements ContainerInterface, ResetInterface

Container is a dependency injection container.

It gives access to object instances (services). Services and parameters are simple key/pair stores. The container can have four possible behaviors when a service does not exist (or is not initialized for the last case):

  • EXCEPTION_ON_INVALID_REFERENCE: Throws an exception (the default)
  • NULL_ON_INVALID_REFERENCE: Returns null
  • IGNORE_ON_INVALID_REFERENCE: Ignores the wrapping command asking for the reference (for instance, ignore a setter if the service does not exist)
  • IGNORE_ON_UNINITIALIZED_REFERENCE: Ignores/returns null for uninitialized services or invalid references
Tags
author

Fabien Potencier fabien@symfony.com

author

Johannes M. Schmitt schmittjoh@gmail.com

Table of Contents

Interfaces

ContainerInterface
ContainerInterface is the interface implemented by service container classes.
ResetInterface
Provides a way to reset an object to its initial state.

Properties

$aliases  : mixed
$factories  : mixed
$fileMap  : mixed
$loading  : mixed
$methodMap  : mixed
$parameterBag  : mixed
$privates  : mixed
$resolving  : mixed
$services  : mixed
$syntheticIds  : mixed
$compiled  : bool
$envCache  : array<string|int, mixed>
$getEnv  : Closure

Methods

__construct()  : mixed
camelize()  : string
Camelizes a string.
compile()  : mixed
Compiles the container.
get()  : mixed
Gets a service.
getParameter()  : array<string|int, mixed>|bool|string|int|float|UnitEnum|null
Gets a parameter.
getParameterBag()  : ParameterBagInterface
Gets the service container parameter bag.
getRemovedIds()  : array<string|int, mixed>
Gets service ids that existed at compile time.
getServiceIds()  : array<string|int, string>
Gets all service ids.
has()  : bool
Returns true if the container can return an entry for the given identifier.
hasParameter()  : bool
initialized()  : bool
Returns true if the given service has actually been initialized.
isCompiled()  : bool
Returns true if the container is compiled.
reset()  : mixed
set()  : mixed
Sets a service.
setParameter()  : mixed
underscore()  : string
A string to underscore.
getEnv()  : mixed
Fetches a variable from the environment.
load()  : mixed
Creates a service by requiring its factory file.
__clone()  : mixed
make()  : mixed
Creates a service.

Properties

$syntheticIds

protected mixed $syntheticIds = []

$envCache

private array<string|int, mixed> $envCache = []

Methods

camelize()

Camelizes a string.

public static camelize(string $id) : string
Parameters
$id : string
Return values
string

compile()

Compiles the container.

public compile() : mixed

This method does two things:

  • Parameter values are resolved;
  • The parameter bag is frozen.

get()

Gets a service.

public get(string $id[, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE ]) : mixed
Parameters
$id : string

Identifier of the entry to look for.

$invalidBehavior : int = self::EXCEPTION_ON_INVALID_REFERENCE
Tags
throws
ServiceCircularReferenceException

When a circular reference is detected

throws
ServiceNotFoundException

When the service is not defined

throws
Exception

if an exception has been thrown when the service has been resolved

see
Reference
Return values
mixed

Entry.

getParameter()

Gets a parameter.

public getParameter(string $name) : array<string|int, mixed>|bool|string|int|float|UnitEnum|null
Parameters
$name : string
Tags
throws
InvalidArgumentException

if the parameter is not defined

Return values
array<string|int, mixed>|bool|string|int|float|UnitEnum|null

getRemovedIds()

Gets service ids that existed at compile time.

public getRemovedIds() : array<string|int, mixed>
Return values
array<string|int, mixed>

getServiceIds()

Gets all service ids.

public getServiceIds() : array<string|int, string>
Return values
array<string|int, string>

has()

Returns true if the container can return an entry for the given identifier.

public has(string $id) : bool

Returns false otherwise.

has($id) returning true does not mean that get($id) will not throw an exception. It does however mean that get($id) will not throw a NotFoundExceptionInterface.

Parameters
$id : string

Identifier of the entry to look for.

Return values
bool

hasParameter()

public hasParameter(string $name) : bool
Parameters
$name : string
Return values
bool

initialized()

Returns true if the given service has actually been initialized.

public initialized(string $id) : bool
Parameters
$id : string
Return values
bool

isCompiled()

Returns true if the container is compiled.

public isCompiled() : bool
Return values
bool

set()

Sets a service.

public set(string $id, object|null $service) : mixed

Setting a synthetic service to null resets it: has() returns false and get() behaves in the same way as if the service was never created.

Parameters
$id : string
$service : object|null

setParameter()

public setParameter(string $name, array<string|int, mixed>|bool|string|int|float|UnitEnum|null $value) : mixed
Parameters
$name : string
$value : array<string|int, mixed>|bool|string|int|float|UnitEnum|null

underscore()

A string to underscore.

public static underscore(string $id) : string
Parameters
$id : string
Return values
string

getEnv()

Fetches a variable from the environment.

protected getEnv(string $name) : mixed
Parameters
$name : string
Tags
throws
EnvNotFoundException

When the environment variable is not found and has no default value

load()

Creates a service by requiring its factory file.

protected load(string $file) : mixed
Parameters
$file : string

make()

Creates a service.

private make(string $id, int $invalidBehavior) : mixed

As a separate method to allow "get()" to use the really fast ?? operator.

Parameters
$id : string
$invalidBehavior : int

        
On this page

Search results