i have easy 1 (i think).
i have silex application...i created service services.yml file, services arguments. of coruse, arguments can instance of classes:
services: service: class: app\services\xxxxxservice arguments: - app\lib\parser\jsonparser - xxxxxx
so, in init application, have piece of code:
$services = $this['config']['services']; foreach ($services $name => $service) { $classname = $service['class']; $args = array_map(function ($arg) { if(class_exists($arg)){ return new $arg; } else { return $arg; } }, $service['arguments']); $args = implode(',', $args); $this[$name] = new $classname($this, $args); }
this code gives me error:
catchable fatal error: object of class app\lib\parser\jsonparser not converted string in /app/src/application.php on line 252
my goal have $this[$name] = new $classname($this, $args[0], $args[1] ....) , cant use implode function.
any ideas???
thank in advance!!
m.
i suggest use reflectionclass instantiate $classname
after collect $args
not use implode
method can not pass correctly args class constructor.
so have $args
array
array_unshift($args, $this); # prepend $this in args $refl = new reflectionclass($classname); $this[$name] = $refl->newinstanceargs($args); #instatiate $classname appropriate args.
Comments
Post a Comment