From 6f1a620522989026783b0d1160c66b3965fc1295 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Mon, 13 Feb 2023 22:15:39 +0100 Subject: [PATCH] [DependencyInjection] Document Container invalid behavior --- components/dependency_injection.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/components/dependency_injection.rst b/components/dependency_injection.rst index 470bcc7f2fc..26fd4d1c87f 100644 --- a/components/dependency_injection.rst +++ b/components/dependency_injection.rst @@ -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 ------------------------------------------------------ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy