@@ -170,6 +170,36 @@ like this::
170170
171171 $newsletterManager = $containerBuilder->get('newsletter_manager');
172172
173+ Getting Services That Don't Exist
174+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
175+
176+ By default, when you try to get a service that doesn't exist, you see an exception.
177+ You can override this behavior as follows::
178+
179+ use Symfony\Component\DependencyInjection\ContainerBuilder;
180+ use Symfony\Component\DependencyInjection\ContainerInterface;
181+
182+ $containerBuilder = new ContainerBuilder();
183+
184+ // ...
185+
186+ // the second argument is optional and defines what to do when the service doesn't exist
187+ $newsletterManager = $containerBuilder->get('newsletter_manager', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
188+
189+
190+ These are all the possible behaviors:
191+
192+ * ``ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE ``: throws an exception
193+ at compile time (this is the **default ** behavior);
194+ * ``ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE ``: throws an
195+ exception at runtime, when trying to access the missing service;
196+ * ``ContainerInterface::NULL_ON_INVALID_REFERENCE ``: returns ``null ``;
197+ * ``ContainerInterface::IGNORE_ON_INVALID_REFERENCE ``: ignores the wrapping
198+ command asking for the reference (for instance, ignore a setter if the service
199+ does not exist);
200+ * ``ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE ``: ignores/returns
201+ ``null `` for uninitialized services or invalid references.
202+
173203Avoiding your Code Becoming Dependent on the Container
174204------------------------------------------------------
175205
0 commit comments