Advertisement
Guest User

php singleton registry

a guest
Sep 27th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. <?php
  2.  
  3. class Registry
  4. {      
  5.     /**
  6.      * Static instance
  7.      *
  8.      * @var Registry
  9.      */
  10.     private static $_instance;
  11.    
  12.     /**
  13.      * Object hash map
  14.      *
  15.      * @var array
  16.      */
  17.     private $_map;
  18.    
  19.     /**
  20.      * Private constructor
  21.      *
  22.      */
  23.     private function __construct()
  24.     {}
  25.    
  26.     /**
  27.      * Get the single instance
  28.      *
  29.      * @return Registry
  30.      */
  31.     public static function getInstance()
  32.     {
  33.             if(self::$_instance === null)
  34.             {
  35.                     //First and only construction.
  36.                     self::$_instance = new self();
  37.             }
  38.             return self::$_instance;
  39.     }
  40.    
  41.     /**
  42.      * Get an object by key
  43.      *
  44.      * @param string|int $key
  45.      * @return object
  46.      */
  47.     public function get($key)
  48.     {
  49.             return $this->_map[$key];
  50.     }
  51.    
  52.     /**
  53.      * Set an object by key
  54.      *
  55.      * @param string|int $key
  56.      */
  57.     public function set($key, $object)
  58.     {
  59.             return $this->_map[$key] = $object;
  60.     }
  61.    
  62.     /**
  63.      * Disallow cloning
  64.      *
  65.      */
  66.     private function __clone()
  67.     {}
  68. }
  69.  
  70. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement