-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[DI] Add section about getter injection #7300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,6 +179,94 @@ The disadvantages of setter injection are: | |
* You cannot be sure the setter will be called and so you need to add checks | ||
that any required dependencies are injected. | ||
|
||
Getter Injection | ||
---------------- | ||
|
||
.. versionadded:: 3.3 | ||
Getter Injection was introduced in Symfony 3.3. | ||
|
||
.. caution:: | ||
|
||
This feature is marked as **experimental**. The next Symfony versions could | ||
change its behavior or even remove this feature entirely. | ||
|
||
Another possible injection point into a class is by overriding a getter method | ||
to make it return the dependency:: | ||
|
||
// ... | ||
abstract class NewsletterManager | ||
{ | ||
abstract protected function getMailer(): MailerInterface; | ||
|
||
protected function getLogger(): LoggerInterface | ||
{ | ||
return new NullLogger(); | ||
} | ||
|
||
// ... | ||
} | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
services: | ||
# ... | ||
|
||
app.newsletter_manager: | ||
class: AppBundle\Mail\NewsletterManager | ||
getters: | ||
getMailer: '@mailer' | ||
getLogger: '@logger' | ||
|
||
.. code-block:: xml | ||
|
||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<!-- ... --> | ||
|
||
<service id="app.newsletter_manager" class="AppBundle\Mail\NewsletterManager"> | ||
<getter name="getMailer" type="service" id="mailer" /> | ||
<getter name="getLogger" type="service" id="logger" /> | ||
</service> | ||
</services> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
use AppBundle\Mail\NewsletterManager; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
// ... | ||
$container->register('app.newsletter_manager', NewsletterManager::class) | ||
->addOverriddenGetter('getMailer', new Reference('mailer')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
->addOverriddenGetter('getLogger', new Reference('logger')) | ||
; | ||
|
||
This time the advantages are: | ||
|
||
* The dependency can be created lazily - ie only when it is actually needed. | ||
|
||
* It works well with both optional and required dependencies: either provide | ||
a default implementation for optional ones, throw an exception or make the | ||
getter abstract for required ones. | ||
|
||
* You can be sure that the dependency will not change during the object's | ||
lifetime. | ||
|
||
* It works well with class hierarchies since you can also override getters of | ||
parent classes. | ||
|
||
The disadvantage of getter injection is: | ||
|
||
* By using inheritance to override methods, it doesn't work with final classes | ||
and requires such getters to be made protected or public. | ||
|
||
Property Injection | ||
------------------ | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these return types are mandatory, we must add somewhere that this only works with PHP 7. Otheriwse, we should remove them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to mention that PHP 7 is required when using getter injection. Added now.
About the type hints, they are not required, but highly recommended. Given the PHP 7 requirement, I think we should keep them here.