Глава 37: рефакторинг кода паттерна Singleton

This commit is contained in:
Igor Simdyanov
2022-07-09 08:17:46 +03:00
parent 44b13a35d8
commit 27faf549a7
2 changed files with 7 additions and 9 deletions

View File

@ -5,3 +5,4 @@ use Singleton\Settings;
Settings::get()->items_per_page = 20;
echo Settings::get()->items_per_page; // 20

View File

@ -3,8 +3,8 @@ namespace Singleton;
final class Settings
{
private static $_object = null;
private $_settings;
private static ?Settings $_object;
private ?array $_settings;
private function __construct()
{
@ -13,16 +13,13 @@ final class Settings
private function __clone() {}
public static function get()
public static function get() : Settings
{
if (is_null(self::$_object)) {
self::$_object = new self();
}
self::$_object ??= new self();
return self::$_object;
}
public function __get($key)
public function __get($key) : mixed
{
if(array_key_exists($key, $this->_settings)) {
return $this->_settings[$key];
@ -31,7 +28,7 @@ final class Settings
}
}
public function __set($key, $value)
public function __set($key, $value) : void
{
$this->_settings[$key] = $value;
}