Skip to content

[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

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Dec 29, 2016


The disadvantages of getter injection are:

* You must call the getter everytime you need the dependency internally.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't any property holding the dependency to use anyway. Is it worth mentioning this as a drawback? 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually the same drawback as outlined here, reformulated :)

Copy link
Contributor

@ogizanagi ogizanagi Dec 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant this should probably not appear in the drawbacks section at all. It isn't one. Just a consequence of the above. Which has no impact.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh! removed then!

app.newsletter_manager:
class: AppBundle\Mail\NewsletterManager
getters:
- getMailer: '@mailer'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it rather be:

     app.newsletter_manager:
         class: AppBundle\Mail\NewsletterManager
         getters:
             getMailer: '@mailer'
             getLogger: '@logger'

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@nicolas-grekas nicolas-grekas force-pushed the getter-injection branch 2 times, most recently from f9144d5 to f8d3457 Compare December 29, 2016 19:11
// ...
abstract class NewsletterManager
{
abstract protected function getMailer(): MailerInterface;
Copy link
Member

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.

Copy link
Member Author

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.

@xabbuh xabbuh added the On hold label Dec 30, 2016
@xabbuh xabbuh added this to the 3.3 milestone Dec 30, 2016
----------------

.. versionadded:: 3.3
Getter Injection was introduced in Symfony 3.3.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should mention that this is an experimental feature

Getter Injection
----------------

.. versionadded:: 3.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The leading spaces should be removed.

fabpot added a commit to symfony/symfony that referenced this pull request Jan 30, 2017
This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Add getter injection

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20657
| License       | MIT
| Doc PR        | symfony/symfony-docs#7300

Getter overriding by the container will allow a new kind of dependency injection which enables easier laziness and more immutable classes, by not requiring any corresponding setter. See linked issue for more.

This is WIP:
- [x] wire the concept
- [x] dump anonymous classes with PhpDumper
- [x] generate at runtime in ContainerBuilder::createService
- [x] tests
- [x] make it work on PHP 5

Commits
-------

cb49858 [DI] Add getter injection
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull request Jan 30, 2017
This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Add getter injection

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20657
| License       | MIT
| Doc PR        | symfony/symfony-docs#7300

Getter overriding by the container will allow a new kind of dependency injection which enables easier laziness and more immutable classes, by not requiring any corresponding setter. See linked issue for more.

This is WIP:
- [x] wire the concept
- [x] dump anonymous classes with PhpDumper
- [x] generate at runtime in ContainerBuilder::createService
- [x] tests
- [x] make it work on PHP 5

Commits
-------

cb498580d1 [DI] Add getter injection
@xabbuh xabbuh removed the On hold label Jan 31, 2017

// ...
$container->register('app.newsletter_manager', NewsletterManager::class)
->addOverriddenGetter('getMailer', new Reference('mailer'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be setOverriddenGetter (same below).

@nicolas-grekas
Copy link
Member Author

Reverted, one less thing to document for 3.3 :)

@nicolas-grekas nicolas-grekas deleted the getter-injection branch March 25, 2017 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants