-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Autowire public typed properties #34769
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
Conversation
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.
Here are some comments. Next step is making AutowirePass able to resolve these references for named autowiring aliases.
src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredPropertiesPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredPropertiesPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/AutowireRequiredPropertiesPass.php
Outdated
Show resolved
Hide resolved
Maybe |
there is a fundamental difference between them: So, definitely not |
@nicolas-grekas Thank you for the explanation :) |
also, we might have detected a bug. I was trying to do:
but I get a
|
good catch, fixed in #34776 |
src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowireRequiredPropertiesPassTest.php
Outdated
Show resolved
Hide resolved
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.
Good replacement for setter injection, but keep in mind to avoid using setter injection (and property injection) as much as possible :)
…las-grekas) This PR was merged into the 4.3 branch. Discussion ---------- [DI] fix resolving bindings for named TypedReference | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - As spotted in #34769 (comment) Commits ------- 62c227e [DI] fix resolving bindings for named TypedReference
Thank you @Plopix. |
… (Plopix) This PR was merged into the 5.1-dev branch. Discussion ---------- [DependencyInjection] Autowire public typed properties | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT ### Description This PR adds the Autowiring of **public typed properties** in PHP 7.4. It is only on "public" properties. It could let people think that services are better injected in "public". I don't know what to think about it, you? ### How about "private" properties - further thinking Even if I think that it would be awesome to be able to inject in "private" properties, we discussed it with @nicolas-grekas, and I agree Symfony should not break any standard logic. If the property is private then it is private the DI cannot touch it. But that could/would remove a lot of boilerplate, and if it is declarative, that might still be something to do. Maybe we could introduce a new annotation for injection in "private": `@requiredPrivated` ? Commits ------- cad7fbb [DI] Autowire public typed properties
Hi, I'm sorry for providing this very late feedback, I saw this new feature through the tweet https://twitter.com/Pehapkari/status/1205145395845840896 and, following multiple other twitter users, I am quite afraid of the consequences of this feature. This feature has obvious benefits in term of avoiding writing boilerplate code and, as stated by @dunglas, can quite nicely avoid the use for setter injections. What I am afraid of is that this feature looks like a step forward into "magic" behaviors happening in Symfony. I think you can guess what I mean: by writing a php comment, writing some "text" then some php functions will automatically called by Symfony to make things happen. The disadvantages of these behaviors are:
I am aware these arguments are the classic arguments against "magic" behaviors, and could be raised against Serializer annotations, Doctrine annotations, services autowiring ... Have a nice day |
I would say that this is quite big feature, really quickly (5 days...) being accepted & merged, while it's impact is way bigger than it looks like IMHO. Good read why not using such approach was written few years ago already: http://olivergierke.de/2013/11/why-field-injection-is-evil/ I'm not totally against this, neither approve it... but it looks like the review process was kinda too quick to allow community to see it... |
An additional piece of read provided to me by @toutantic https://www.petrikainulainen.net/software-development/design/why-i-changed-my-mind-about-field-injection/ that explains a lot better than me the possible negative outcomes of this approach |
Thanks for the comments but this PR didn't introduce property injection. Actually this kind of injection exists since before Symfony 2.0.0 was tagged. |
Are you referring to Property Injection ? Then you're right, but this is only a mismatch of vocabulary. If I am not wrong, Symfony already had "Property Injection" (as referenced above) and this PR introduces "Autowired Property Injection". The 2 blog articles (which refer to Java it seems) we mentioned talk about "Property Injection" but if you read them, what they refer as "Property Injection" is actually what Symfony refers as "Autowired Property Injection". So Autowired Property Injection is the mechanism that we are worried about. |
I'm not sure at all that others talk specifically about what this PR provides. Looking at the Twitter thread, ppl rant about property injection itself. They don't need to change their mind, this PR is about something else. About the article you linked now that I read it, there is a critical difference with the feature provided here: the code examples all use private properties for field injection. This is just plain wrong. To @Plopix that asks in the PR description above about The DI container of Symfony is about automating the creation of objects saving us from writing the wiring code by hand. There is a decisive implicit requirement in this statement: you should be able to write the wiring by hand using plain PHP code. That excludes using protected or private properties or methods. So, nothing to see here to me: the authors write their classes, then, afterward, we wire them together. The DI container is only about that. PHP 7.4 provides type annotations on properties. The DI container has to autowire them now that it has all the required information. It's a matter of feature completeness, nothing else. |
This PR was merged into the 5.1 branch. Discussion ---------- Add public typed properties autowiring https://symfony.com/blog/new-in-symfony-5-1-autowire-public-typed-properties and implemented in symfony/symfony#34769 Commits ------- 3451964 Add public typed properties autowiring
Description
This PR adds the Autowiring of public typed properties in PHP 7.4.
It is only on "public" properties.
It could let people think that services are better injected in "public".
I don't know what to think about it, you?
How about "private" properties - further thinkingEven if I think that it would be awesome to be able to inject in "private" properties, we discussed it with @nicolas-grekas, and I agree Symfony should not break any standard logic. If the property is private then it is private the DI cannot touch it.But that could/would remove a lot of boilerplate, and if it is declarative, that might still be something to do.Maybe we could introduce a new annotation for injection in "private":@requiredPrivated
?