Skip to content

Commit

Permalink
Merge pull request #11262 from nextcloud/feat/devmanual/optional-di
Browse files Browse the repository at this point in the history
feat(devmanual): Optional dependency injection services
  • Loading branch information
ChristophWurst authored Nov 3, 2023
2 parents fcc8cfd + 236828b commit dedaae3
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions developer_manual/basics/dependency_injection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------------
Expand Down

0 comments on commit dedaae3

Please sign in to comment.