Skip to content

Commit e2d0696

Browse files
committed
[DependencyInjection] Document Container invalid behavior
1 parent 123af46 commit e2d0696

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

components/dependency_injection.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,30 @@ like this::
170170

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

173+
Behavior When Service Does Not Exist
174+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
175+
176+
By default, an exception is throw when a service does not exist. You can override the behavior by specifying it::
177+
178+
use Symfony\Component\DependencyInjection\ContainerBuilder;
179+
use Symfony\Component\DependencyInjection\ContainerInterface;
180+
181+
$containerBuilder = new ContainerBuilder();
182+
183+
// ...
184+
185+
// the second argument is optional and define behavior
186+
$newsletterManager = $containerBuilder->get('newsletter_manager', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
187+
188+
189+
Possible behaviors are:
190+
191+
* ``ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE``: Throws runtime exception
192+
* ``ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE``: Throws an exception **default**
193+
* ``ContainerInterface::NULL_ON_INVALID_REFERENCE``: Returns null
194+
* ``ContainerInterface::IGNORE_ON_INVALID_REFERENCE``: Ignores the wrapping command asking for the reference (for instance, ignore a setter if the service does not exist)
195+
* ``ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE``: Ignores/returns null for uninitialized services or invalid references
196+
173197
Avoiding your Code Becoming Dependent on the Container
174198
------------------------------------------------------
175199

0 commit comments

Comments
 (0)