Singleton added
This commit is contained in:
48
code/singleton.php
Normal file
48
code/singleton.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 08:39
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Pattern\Creational\{Singleton, Sub\Single};
|
||||
|
||||
/**
|
||||
* The client code.
|
||||
*
|
||||
* @noinspection ForgottenDebugOutputInspection
|
||||
*/
|
||||
function clientCode(): void
|
||||
{
|
||||
$output = [
|
||||
'where' => trace(),
|
||||
's1' => Singleton::getInstance('s1'),
|
||||
's2' => Singleton::getInstance('s2'),
|
||||
];
|
||||
|
||||
if ($output['s1'] === $output['s2']) {
|
||||
$output['message'] = "Singleton works, both variables contain the same instance.";
|
||||
} else {
|
||||
$output['message'] = "Singleton failed, variables contain different instances.";
|
||||
}
|
||||
|
||||
$output['s1']->setValue('value set from s1@Singleton::class');
|
||||
dump($output['s1']->getValue());
|
||||
$output['s2']->setValue('value set from s2@Singleton::class');
|
||||
dump($output, $output['s1']->getValue(), $output['s2']->getValue());
|
||||
}
|
||||
|
||||
clientCode();
|
||||
|
||||
$single = Single::getInstance('from subclass');
|
||||
$single->setValue('value set from Single::class')->childish();
|
||||
|
||||
|
||||
$singleton = Singleton::getInstance('from Singleton');
|
||||
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump($single->getValue(), $singleton->getValue());
|
||||
Reference in New Issue
Block a user