Answer the question
In order to leave comments, you need to log in
How to use singleton?
Just started learning Symfony after Larael and Yii but can't figure out how to use singleton within Symfony.
In general, it is supposed to create a Setting class:
<?php
namespace App\Service;
class Setting
{
private static $instance;
private array $settings;
protected function __construct() {}
public function get($key)
{
if (empty($settings)) {
$this->settings = // тут каким-то образом сделать запрос к базе
}
return $this->settings[$key] ?? null;
}
public static function getInstance(): self
{
if (null === static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question