-
-
Notifications
You must be signed in to change notification settings - Fork 0
Exception handling
This library exposes two exception types.
Both are runtime exceptions, and both follow the PSR container exception interfaces.
ServiceContainerException is the general-purpose container exception type. It implements Psr\Container\ContainerExceptionInterface.
You will most commonly see it when invalid values are passed to Container::set():
$container->set("not an object");Only objects may be registered directly. If a string, array, integer, or null is supplied, this exception is thrown with a message explaining the invalid type.
ServiceNotFoundException extends ServiceContainerException and implements Psr\Container\NotFoundExceptionInterface.
It is thrown when the container cannot resolve a requested type:
$container->get(Greeter::class);The same exception may be raised indirectly by Injector when invoking a callable that requires a non-nullable service the container does not have.
There are two cases where missing services do not raise an exception:
-
Container::has()returnsfalseinstead of throwing. -
Injectorinjectsnullfor a missing service when the parameter type is nullable.
These two behaviours make it possible to model genuinely optional services without special container configuration.
PHP.GT/ServiceContainer is a separately maintained component that powers dependency injection features in PHP.GT/WebEngine.