Allow injection of any container object as factory parameter via type hinting #333
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.
With this PR it's possible to inject any container object into a factory via type hinting (see #197).
This means that instead of fetching the needed objects from the container inside your factory like this
you can now let the container inject your dependencies as arguments to your factory directly
This works with multiple arguments, too, of course. And it's backwards compatible, so if you don't type hint the first parameter, an instance of the
ContainerInterfacewill be injected (but that's not recommended anyway).There was a discussion about "angular style DI" in #197 and #239 but as I wasn't sure what's the best way to define this type of dependencies this feature was left out here.
I did a quick benchmark (my first one using blackfire) with the provided
blackfire run --samples=100 php factory.phpwhich showed a performance decrease of about 10% but I'm not sure how reliable this is for real world situations. Maybe you have better ways for benchmarking this?Todo: