Advertisement
P22DX

Singleton.php

Aug 21st, 2020
2,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.78 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Class: Singleton
  5.  *
  6.  */
  7. class Singleton
  8. {
  9.     /**
  10.      * instance
  11.      *
  12.      * @var mixed
  13.      */
  14.     private static $instance;
  15.  
  16.     /**
  17.      * name
  18.      *
  19.      * @var mixed
  20.      */
  21.     protected $name;
  22.  
  23.     /**
  24.      * __construct
  25.      *
  26.      * @param mixed $name
  27.      */
  28.     private function __construct($name)
  29.     {
  30.         $this->name = $name;
  31.     }
  32.  
  33.     /**
  34.      * getInstance
  35.      *
  36.      * @param mixed $name
  37.      */
  38.     public static function getInstance($name) : Self
  39.     {
  40.         if (!isset(self::$instance)) {
  41.             self::$instance = new Self($name);
  42.         }
  43.         return self::$instance;
  44.     }
  45.  
  46.     /**
  47.      * getName
  48.      *
  49.      */
  50.     public function getName()
  51.     {
  52.         return $this->name;
  53.     }
  54. }
  55.  
  56. echo Singleton::getInstance('Cvar1984')->getName(), PHP_EOL;
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement