[英]use service invoke function in symfony
Is possible to call invoke function from a service in symfony?是否可以从 symfony 中的服务调用调用 function?
this is the controller这是 controller
class FooController extends AbstractController
{
private $fooService;
public function __construct(FooService $fooService)
{
$this->fooService = $fooService;
}
#[Route('/foo', name: 'app_foo')]
public function index(): Response
{
return new Response($this->fooService->__invoke());
//is not possible to do
//return new Response($this->fooService());
}
}
and the service和服务
namespace App\Service;
class FooService
{
public function __invoke()
{
return 'hello';
}
}
I have to call __invoke
function explicitly instead to make $ this->fooService()
is not possible to do it?我必须明确调用
__invoke
function 来使 $ this->fooService()
不可能做到吗?
In PHP the method call has higher priority than property access so you need to use parentheses.在 PHP 中,方法调用的优先级高于属性访问,因此您需要使用括号。
($this->fooService)() ($this->fooService)()
To access the property and call it.访问该属性并调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.