Symfony2 API
Class

Symfony\Component\DependencyInjection\Container

class Container implements IntrospectableContainerInterface

Container is a dependency injection container.

It gives access to object instances (services).

Services and parameters are simple key/pair stores.

Parameter and service keys are case insensitive.

A service id can contain lowercased letters, digits, underscores, and dots. Underscores are used to separate words, and dots to group services under namespaces:

A service can also be defined by creating a method named getXXXService(), where XXX is the camelized version of the id:

The container can have three possible behaviors when a service does not exist:

Methods

__construct(ParameterBagInterface $parameterBag = null)

Constructor.

compile()

Compiles the container.

bool isFrozen()

Returns true if the container parameter bag are frozen.

ParameterBagInterface getParameterBag()

Gets the service container parameter bag.

mixed getParameter(string $name)

Gets a parameter.

bool hasParameter(string $name)

Checks if a parameter exists.

setParameter(string $name, mixed $value)

Sets a parameter.

set(string $id, object $service, string $scope = self::SCOPE_CONTAINER)

Sets a service.

bool has(string $id)

Returns true if the given service is defined.

object get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)

Gets a service.

bool initialized(string $id)

Returns true if the given service has actually been initialized

array getServiceIds()

Gets all service ids.

enterScope(string $name)

This is called when you enter a scope

leaveScope(string $name)

This is called to leave the current scope, and move back to the parent scope.

addScope(ScopeInterface $scope)

Adds a scope to the container.

bool hasScope(string $name)

Returns whether this container has a certain scope

bool isScopeActive(string $name)

Returns whether this scope is currently active

static string camelize(string $id)

Camelizes a string.

static string underscore(string $id)

A string to underscore.

Details

at line 86
public __construct(ParameterBagInterface $parameterBag = null)

Constructor.

Parameters

ParameterBagInterface $parameterBag A ParameterBagInterface instance

at line 103
public compile()

Compiles the container.

This method does two things:

at line 117
public bool isFrozen()

Returns true if the container parameter bag are frozen.

Return Value

bool true if the container parameter bag are frozen, false otherwise

at line 129
public ParameterBagInterface getParameterBag()

Gets the service container parameter bag.

Return Value

ParameterBagInterface A ParameterBagInterface instance

at line 145
public mixed getParameter(string $name)

Gets a parameter.

Parameters

string $name The parameter name

Return Value

mixed The parameter value

Exceptions

InvalidArgumentException if the parameter is not defined

at line 159
public bool hasParameter(string $name)

Checks if a parameter exists.

Parameters

string $name The parameter name

Return Value

bool The presence of parameter in container

at line 172
public setParameter(string $name, mixed $value)

Sets a parameter.

Parameters

string $name The parameter name
mixed $value The parameter value

at line 192
public set(string $id, object $service, string $scope = self::SCOPE_CONTAINER)

Sets a service.

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

Parameters

string $id The service identifier
object $service The service instance
string $scope The scope of the service

Exceptions

RuntimeException When trying to set a service in an inactive scope
InvalidArgumentException When trying to set a service in the prototype scope

at line 238
public bool has(string $id)

Returns true if the given service is defined.

Parameters

string $id The service identifier

Return Value

bool true if the service is defined, false otherwise

at line 273
public object get(string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)

Gets a service.

If a service is defined both through a set() method and with a get{$id}Service() method, the former has always precedence.

Parameters

string $id The service identifier
int $invalidBehavior The behavior when the service does not exist

Return Value

object The associated service

Exceptions

InvalidArgumentException if the service is not defined
ServiceCircularReferenceException When a circular reference is detected
ServiceNotFoundException When the service is not defined
Exception if an exception has been thrown when the service has been resolved

See also

Reference

at line 353
public bool initialized(string $id)

Returns true if the given service has actually been initialized

Parameters

string $id The service identifier

Return Value

bool true if service has already been initialized, false otherwise

at line 371
public array getServiceIds()

Gets all service ids.

Return Value

array An array of all defined service ids

at line 395
public enterScope(string $name)

This is called when you enter a scope

Parameters

string $name

Exceptions

RuntimeException When the parent scope is inactive
InvalidArgumentException When the scope does not exist

at line 443
public leaveScope(string $name)

This is called to leave the current scope, and move back to the parent scope.

Parameters

string $name The name of the scope to leave

Exceptions

InvalidArgumentException if the scope is not active

at line 490
public addScope(ScopeInterface $scope)

Adds a scope to the container.

Parameters

ScopeInterface $scope

Exceptions

InvalidArgumentException

at line 524
public bool hasScope(string $name)

Returns whether this container has a certain scope

Parameters

string $name The name of the scope

Return Value

bool

at line 540
public bool isScopeActive(string $name)

Returns whether this scope is currently active

This does not actually check if the passed scope actually exists.

Parameters

string $name

Return Value

bool

at line 552
static public string camelize(string $id)

Camelizes a string.

Parameters

string $id A string to camelize

Return Value

string The camelized string

at line 564
static public string underscore(string $id)

A string to underscore.

Parameters

string $id The string to underscore

Return Value

string The underscored string