Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- interface ISingleton {
- public static function getInstance(): ISingleton;
- }
- abstract class Singleton implements ISingleton {
- private static $_instances = array();
- private function __construct () {}
- private function __clone() {}
- public static function getInstance() : ISingleton {
- $className = get_called_class();
- self::$_instances[$className] = self::$_instances[$className] ?? new static();
- return self::$_instances[$className];
- }
- }
- class Person extends Singleton {
- public $age = 0;
- }
- class Man extends Person {
- }
- class Woman extends Person {
- }
- $person1 = Person::getInstance();
- $person1->age = 1;
- $man1 = Man::getInstance();
- $man1->age = 2;
- $woman1 = Woman::getInstance();
- $woman1->age = 3;
- var_dump($person1, $man1, $woman1, Person::getInstance(), Man::getInstance(), Woman::getInstance());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement