Closed
Description
Description
It would be nice if we had @Autowired
annotation like Spring. Rather than injecting services via the constructor, we could use @Autowired
annotation to make it even simpler. Check out the examples below:
Example
Currently we're injecting services via the constructor.
namespace App\Manager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
class TrafficManager
{
private UrlMatcherInterface $urlMatcher;
private LoggerInterface $logger;
public function __construct(UrlMatcherInterface $urlMatcher, LoggerInterface $logger)
{
$this->urlMatcher = $urlMatcher;
$this->logger = $logger;
}
public function match(string $url)
{
try {
$this->urlMatcher->match($url);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage());
}
}
}
If we had @Autowired
annotation, we could make this simpler eliminating the need of creating a constructor.
namespace App\Manager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Comonent\DependencyInjection\Annotations\Autowired;
class TrafficManager
{
#[Autowired]
private UrlMatcherInterface $urlMatcher;
#[Autowired]
private LoggerInterface $logger;
public function match(string $url)
{
try {
$this->urlMatcher->match($url);
} catch (\Throwable $e) {
$this->logger->error($e->getMessage());
}
}
}