Skip to content

Commit 6f1a620

Browse files
alamiraultjaviereguiluz
authored andcommitted
[DependencyInjection] Document Container invalid behavior
1 parent 123af46 commit 6f1a620

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::
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 the behavior
186+
$newsletterManager = $containerBuilder->get('newsletter_manager', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
187+
188+
189+
Possible behaviors are:
190+
191+
* ``ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE``: Throws an exception at compile time **default**
192+
* ``ContainerInterface::NULL_ON_INVALID_REFERENCE``: Returns null
193+
* ``ContainerInterface::IGNORE_ON_INVALID_REFERENCE``: Ignores the wrapping command asking for the reference (for instance, ignore a setter if the service does not exist)
194+
* ``ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE``: Ignores/returns null for uninitialized services or invalid references
195+
* ``ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE``: Throws an exception at runtime, when trying to access the missing service
196+
173197
Avoiding your Code Becoming Dependent on the Container
174198
------------------------------------------------------
175199

0 commit comments

Comments
 (0)