0

Is possible to call invoke function from a service in symfony?

this is the 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?

1 Answer 1

2

In PHP the method call has higher priority than property access so you need to use parentheses.

($this->fooService)()

To access the property and call it.

Sign up to request clarification or add additional context in comments.

1 Comment

Just take a look at What is voting up?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.