title: Creating a good registry class in PHP for important classes
body: <p>I have a webapplication where I use a registry class. The registry class holds important classes that I need troughout my application.</p>
  
  <p>I have made the registry class a Singleton class and this class is static.</p>
  
  <p>The content of the registry class is here:</p>
  
  <pre><code>&lt;?php
  class registry
  {
      private static $objects = array();
      private static $instance;
  
      private function __construct(){}
      private function __clone(){}
  
      public static function singleton()
      {
          if( !isset( self::$instance ) )
          {
              self::$instance = new registry();
          }
          return self::$instance;
      }
  
      public function storeObjects()
      {
          $Objects = array_merge($GLOBALS['EXTRA_LIBS'],array('data','page','session','uri','loader','modules'));
  
          foreach($Objects as $name)
          {
              $this-&gt;$name  = $name;
          }
      }
  
      public function __set( $object, $key )
      {
          require_once(__LIBRARIES . DS . $object . '.class.php');
          self::$objects[ $key ] = new $object( self::$instance );
      }
  
      public function __get( $key )
      {
          if( is_object ( self::$objects[ $key ] ) )
          {
              return self::$objects[ $key ];
          }
      }
  
      public function returnAllObjects()
      {
          return self::$objects;
      }
  }
  ?&gt;
  </code></pre>
  
  <p>Now whenever I want to use one of the classes here in my application I do:</p>
  
  <pre><code>registry::singleton()-&gt;data-&gt;adddata('somedata');
  </code></pre>
  
  <p>I use the classes a lot in my application, minimum of 1 time in every method.</p>
  
  <p>Now my question is what is the best thing to do:</p>
  
  <p>1) call the whole thing everytime</p>
  
  <p>2) Class <code>$registry = registry::singleton();</code> one time in every method and then just use the local variable. (I do not know if this will work)</p>
  
  <p>Or is there a more elegant method to tackle this problem.</p>