Skip to content

[2.1] Method Injection #881

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
wants to merge 9 commits into from
Closed

Conversation

schmittjoh
Copy link
Contributor

@schmittjoh schmittjoh commented May 10, 2011

Implements #835

// enable method injection
$passes = $passConfig->getRemovingPasses();
array_unshift($passes, new GenerateLookupMethodClassesPass($this->getCacheDir().'/lookup_method_classes'));
$passConfig->setRemovingPasses($passes);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why add the pass here instead of in the container?

@kriswallsmith
Copy link
Contributor

I've read the blog article from 2004 and still disagree with this feature (as I've articulated in #835). The idea of native support for lazy injection is intriguing, but that would be better implemented by wrapping the dependency in a proxy object. I don't see any case when abstract lookup methods would be a best practice, unless you're designing a class for this particular feature. I would rather we design the DIC to work with classes designed without any knowledge of the container than provide features that are only useful if you design a class to be used with the container, thus binding your class to the DIC.

@lsmith77
Copy link
Contributor

Well I think we can all agree that injecting the entire container is an ugly practice, especially for Controllers since they are obviously quite central to a given request. Its ugly because it is a super tight coupling with the DIC and specific services. However to keep things simple we decided to default to using this approach.

Now the question is if we want to add even more "questionable" tools to make this practice a bit less ugly or if we instead try to encourage users to move towards Controllers as services if they care about reducing the coupling (I agree with @schmittjoh that this approach actually reduces the coupling rather than increases the coupling if you are already injecting the DIC).

We could also at least define that adding a getter for a service is a best practice to follow when injecting the container. Which is essentially what this PR tries to encourage by reducing the amount of code needed.

BTW: I think phpStorm 2.1 (and maybe some other IDE's) also support auto completion for @return phpDoc comments, so it might make sense to do the annotation on the function rather than force the use of a property.

Anyway, to me this eases the pain we have with a problem we shouldn't have to begin with so I am still not very enthusiastic about this. The problem is solved more elegantly by the ContainerWrapperBundle albeit with potentially more overhead in performance of course.

@fabpot
Copy link
Member

fabpot commented May 11, 2011

Let's schedule the discussion for 2.1.

@schmittjoh schmittjoh closed this Jun 16, 2011
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.

4 participants