The problem: I create a ViewHelper, which calls a service (calcService) and in this service I want to use another service, named AuthenticationService. I got the following error message:
Fatal error: Call to a member function getUser() on a non-object
The problem is that I tried to call the functions statically instead of inject or via objectManager. When I tried the inject in my CalcService, I got the errormessage above and the reason is, that I called the service in my viewhelper in the wrong way.
WRONG:
use Ophi\OphiSomething\Service\CalcService;
class CalcSomethingViewHelper extends AbstractViewHelper {
public function render($str) {
return CalcService::doSomething($str);
}
}
CORRECT:
use Ophi\OphiSomething\Service\CalcService;
class CalcSomethingViewHelper extends AbstractViewHelper {
/**
* @var \Ophi\OphiSomething\Service\CalcService
* @inject
*/
private $calcService;
public function render($str) {
return $this->calcService($str);
}
}
…good to know.