Merge pull request #11262 from nextcloud/feat/devmanual/optional-di

feat(devmanual): Optional dependency injection services
This commit is contained in:
Christoph Wurst 2023-11-03 12:52:26 +01:00 committed by GitHub
commit dedaae39cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -443,6 +443,26 @@ What not to inject:
.. _`reflection`: https://www.php.net/manual/en/book.reflection.php
Optional services
-----------------
.. versionadded:: 28
If an injected dependency can't be found or build, an exception is thrown. This can be avoided by using the a nullable type notation for a dependency:
.. code-block:: php
:emphasize-lines: 6
namespace OCA\MyApp\MyService;
use Some\Service;
class MyService {
public function __construct(private ?Service $service) {
}
}
If ``\Some\Service`` exists and can be built, it will be injected. Else ``MyService`` will receive ``null``.
Accessing the container from anywhere
-------------------------------------