Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In my application, I do not implemented everything inside the controller.

The controller calls methods on another class, which calls another class, but THAT class wishes to use a service like $this->get('service').

As an example:

public function controllerAction()
{
    $obj = new SubClass1();
    $obj->method();
}

class SubClass1
{
    public function method()
    {
        $obj = new SubClass2();
        $obj->method();
    }
}

class SubClass2
{
    public function method()
    {
        //Here I wish to use a Symfony Service.
    }
}

Since the Service Container isn't global, both SubClass1 and SubClass2 must be declared as services, so Symfony will inject the services to them, although those classes are not really services in my application (They're being used once).

Do you have another solution to this? Am I holding a design mistake?

Thank you.

share|improve this question
1  
Can you show some code? –  sebbo Aug 30 at 14:41
    
I have added a code example. –  Taru Stolovich Aug 30 at 15:15
1  
Thank you. Alright - seems like you need to learn a bit more about the Symfony2 dependency injection mechanisms. Please check symfony.com/doc/current/components/dependency_injection/… –  sebbo Aug 30 at 15:45
1  
Basically, instead of using the new operator you would inject the dependency. This will avoid passing the container around which is generally considered to be a bad thing. You might be better off with presenting a specific use case. –  Cerad Aug 30 at 17:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.