Good afternoon, I'm used to doing this in Symfony2 as follows In service.yml

api.service.filter: class: ApiBundle\Service\FilterRule arguments: - "%some.parametr%" 

In the service

  * @param $some */ public function __construct($some) { $this->some = $some; } 

and then in any service method, this parameter is received via

 $this->some 

And then I ran into the fact that I have such a code

  public static function fromParamFetcher(ParamFetcherInterface $fetcher) { $input = new static(); $input->code = $fetcher->get('code', $strict = true); $input->dateFrom = $fetcher->get('dateFrom', $strict = true); $input->dateTo = $fetcher->get('dateTo', $strict = true); } 

And here this-> some can not pass. How can you do it right?

  • Of course, you can pass this parameter in the controller when I pull the service, but in my opinion it will not be very good - Nikita Rassamahin

2 answers 2

So this is a factory
http://symfony.com/doc/current/components/dependency_injection/factories.html

 services: api.service.filter: class: ApiBundle\Service\FilterRule factory: ["ApiBundle\Service\FilterRule", fromParamFetcher] arguments: - '@fetcher' 

But only at the end of the method you need to add return $input; .

    The static method is common to all instances of classes and can be called without creating objects.

     ClassName::fromParamFetcher($fetcher); 

    Therefore, we cannot use inside it the $this link to the current object - it simply does not exist. You need to either create an object inside such a static class, or pass the desired value to it via an additional parameter, or declare a static variable with a predetermined value in the class.