symfony - Symfony2 - Instantiation of arguments in Services -
i'm having bit of issue , don't know if i'm being stupid, on imaginative, or if didn't quite grasp of services.
say have service defined followed :
awesome: class: "\\my\\bundle\\services\\awesome" arguments: fantabulous: @fanta.bulous #which refers \\another\\bundle\\services\\fantabulous
say have controller testcontroller in have following action
public function someaction() { $awesome = $this->get('awesome'); $awesome->dosomething(); }
say fantabulous class looks :
class fantabulous { protected $number; public function __construct($anynumber) { $this->number = $anynumber; } public function printnumber() { return $this->number; } }
say awesome service looks :
class awesome { protected $fantabulous; public function __construct(fantabulous $fantabulous) { $this->fantabulous = $fantabulous; } public function dosomething() { echo $this->fantabulous->printnumber(); } }
my question : how pass number (e.g. 14) initialize fantabulous, testcontroller? i've been searching high , low answer couldn't find one, seems me never instatiate in services.
thanks.
you can't! not in case. cannot call service service container , pass arguments @ same time. however, can follow these approaches:
- add
setter
method fantabulous (maybe fantabulous::setnumber()) , turn optional $anynumber argument (on construct); - create objects on hand rather call container;
- create factory service in order create "awesome" or others services;
what want has been discussed once project repository, might want take , find out why suggestion not approved. helpful links:
Comments
Post a Comment