Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing dynamic variables to a service constructor

Tags:

I have a service in Symfony2 that looks like:

services:
    MyCustomService:
        class:     MyClass
        arguments: //Arguments aren't static, but dynamic based on application logic.

Is it possible to pass dynamic variables to a service's constructor?

There doesn't seem to be any extra parameters within a controller's $this->get('MyCustomService');

Is there something I'm missing?

like image 525
Tek Avatar asked May 22 '14 15:05

Tek


1 Answers

To me, it sounds like, you do not understand what a word "service" really means. What you are trying to achieve, would not be a service anymore.

You can still define a setter method inside your "MyClass" for any custom arguments, while defining some default ones, which you basically override when you use setter method.

You would use something like this:

$this->get('MyCustomService')->setSomething($something);
like image 179
tomazahlin Avatar answered Oct 28 '22 17:10

tomazahlin