M
M
magary42016-08-05 15:04:28
symfony
magary4, 2016-08-05 15:04:28

How to extract an object from the Service Container depending on the argument?

How to do this

class Product {
   private $container;
   private $data;
   public function __constructor( ServiceContainer  $container ) {
       $this->container = $container;
        $this->data = $this->container->get('someotherservice')->getDataById( $ID );
  } 
}

// controller code

$ID = 123;
$product = $this->get('service.product');  //  как передать туда ID?

i.e. pass 123 and the ServiceContainer should return an object instance created based on this ID

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2016-08-05
@magary4

You can't do that.
Or rather, it is possible, but this is a complete bad manners and an architectural mistake.
Inject someotherservice into the controller and call its getDataById method .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question