'TYPO3 EventDispatcherInterface

I try to inject the $eventDispatcher in my Repository

private EventDispatcherInterface $eventDispatcher;

public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher): void
    {
        $this->eventDispatcher = $eventDispatcher;
    }

But since PHP 7.4 you have to be initialized. But I cant initialize the EventDispatcherInterface and get this error : Typed property $eventDispatcher must not be accessed before initialization

How can I initialize the eventDispatcher? Thanks.



Solution 1:[1]

Maybe, switching to Constructor Injection can solve this:

public function __construct(EventDispatcherInterface $eventDispatcher)
{
    $this->eventDispatcher = $eventDispatcher;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Julian Hofmann