-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] always call the parent class' constructor #25762
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.
This is a risky change: let's say the parent is customized: what if it has no constructor? or any args (less likely?)
Could happen, but right now we already call the parent constructor if the container is not compiled. Not doing this for compiled containers leads to inconsistencies and to issues as described in the linked issue. |
On the other hand, uncompiled containers are not a thing, so their code path is mostly dead (we even removed it in 4.0.) |
Hm indeed, you are right about that. Should we do some reflection then and try to find out if there is a constructor that could be called instead? |
Looks great yes. We should ensure that there is a constructor, and that it has zero mandatory args. |
On 3.4 only? I'd prefer not change any behavior on 2.* |
ae89452
to
e18186b
Compare
@@ -970,9 +976,20 @@ public function __construct() | ||
} | ||
|
||
if ($this->container->isCompiled()) { | ||
try { | ||
$r = new \ReflectionClass($baseClassWithNamespace); |
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.
$container->getReflectionClass()? (then no need to try/catch)
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.
done
try { | ||
$r = new \ReflectionClass($baseClassWithNamespace); | ||
|
||
if ((null !== $constructor = $r->getConstructor()) && 0 === $constructor->getNumberOfRequiredParameters()) { |
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.
&& $baseClassWithNamespace !== Container::class
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 (($constructor = $r->getConstructor()) && !$constructor->getNumberOfRequiredParameters() && $baseClassWithNamespace !== Container::class) {
?
34d463a
to
ac4b2f6
Compare
Thank you @xabbuh. |
…uctor (xabbuh) This PR was merged into the 3.4 branch. Discussion ---------- [DependencyInjection] always call the parent class' constructor | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25343 | License | MIT | Doc PR | Commits ------- a1b1484 always call the parent class' constructor
Uh oh!
There was an error while loading. Please reload this page.