In a tiny Symfony 5.4 project, I would like to initialize a class (logger) at application's boot. This class will build a singleton object and will give me the possibility to call the logger like this maClass::logger->error().
I red a bunch of articles speaking about autowiring. I understand the concept, but in some others classes, I can't use the autowiring, that's why I would like to do that.
Here my new class:
<?php
namespace App\Core\Service;
use Psr\Log\LoggerInterface;
class CoreService
{
private LoggerInterface $logger;
private static LoggerInterface $loggerSingleton;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
$this->initStatic();
}
public function initStatic()
{
self::$loggerSingleton = $this->logger;
}
public static function logger(): LoggerInterface
{
return self::$loggerSingleton;
}
}
Is there a way to do that?
Thanks.