Hello I am using php dependency-injection in this script below .Everything works when the injected object or class has no constructor. But the issue here is that when the injected class get a constructor function along with parameter , injection fails .I would like to know how to deal with this case.
require 'vendor/autoload.php';
class useMe {
private $param ;
function __construct($param) {
$this->param = $param ;
}
public function go() {
return "See you later on other class with my parameter " . $this->param;
}
} // end of useMe Class
class needInjection {
private $objects ;
public function __construct(useMe $objects) {
$this->objects = $objects ;
}
public function apple() {
return $this->objects->go();
}
} // end of needInjection
/** Implementing now injection **/
$container = DI\ContainerBuilder::buildDevContainer();
// adding needInjection class dependency
$needInjection = $container->get('needInjection');
echo $needInjection->apple() ; // this fails due to parameter passed to the constructor function of useMe class
NOTE : This example has been simplified for understanding purpose
$aparamin theuseMeclass? Should be$this->param = $param;(no space either, before semicolon)