Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. <?php
  2.  
  3. interface ISingleton {
  4.  
  5. public static function getInstance(): ISingleton;
  6. }
  7.  
  8. abstract class Singleton implements ISingleton {
  9.  
  10. private static $_instances = array();
  11.  
  12. private function __construct () {}
  13. private function __clone() {}
  14.  
  15. public static function getInstance() : ISingleton {
  16.  
  17. $className = get_called_class();
  18. self::$_instances[$className] = self::$_instances[$className] ?? new static();
  19. return self::$_instances[$className];
  20. }
  21.  
  22. }
  23.  
  24. class Person extends Singleton {
  25.  
  26. public $age = 0;
  27. }
  28.  
  29. class Man extends Person {
  30.  
  31. }
  32.  
  33. class Woman extends Person {
  34.  
  35. }
  36.  
  37. $person1 = Person::getInstance();
  38. $person1->age = 1;
  39. $man1 = Man::getInstance();
  40. $man1->age = 2;
  41. $woman1 = Woman::getInstance();
  42. $woman1->age = 3;
  43.  
  44. var_dump($person1, $man1, $woman1, Person::getInstance(), Man::getInstance(), Woman::getInstance());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement