Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DependencyInjection] Document Container invalid behavior #17903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions 24 components/dependency_injection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@ like this::

$newsletterManager = $containerBuilder->get('newsletter_manager');

Behavior When Service Does Not Exist
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

By default, an exception is throw when a service does not exist. You can override the behavior by::

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;

$containerBuilder = new ContainerBuilder();

// ...

// the second argument is optional and define the behavior
$newsletterManager = $containerBuilder->get('newsletter_manager', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);


Possible behaviors are:

* ``ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE``: Throws an exception at compile time **default**
* ``ContainerInterface::NULL_ON_INVALID_REFERENCE``: Returns null
* ``ContainerInterface::IGNORE_ON_INVALID_REFERENCE``: Ignores the wrapping command asking for the reference (for instance, ignore a setter if the service does not exist)
* ``ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE``: Ignores/returns null for uninitialized services or invalid references
* ``ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE``: Throws an exception at runtime, when trying to access the missing service

Avoiding your Code Becoming Dependent on the Container
------------------------------------------------------

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.