title: How Bad Are Singletons?
body: <p>So ....</p>
  
  <p>There are obviously many questions that all have been asked about Singletons, Global State Variables, and all that great stuff. My question is, </p>
  
  <p><strong>If Singletons and Globals are So Bad, Why are they used so often?</strong></p>
  
  <p>The following examples are simply ones off the top of my head that I believe are used by a good chunk of people.</p>
  
  <p>I give you a function from CodeIgniter that uses a psuedo-singleton function:</p>
  
  <p>(system\codeigniter\Common.php Line 89)</p>
  
  <pre><code>/**
  * Class registry
  *
  * This function acts as a singleton.  If the requested class does not
  * exist it is instantiated and set to a static variable.  If it has
  * previously been instantiated the variable is returned.
  *
  * ......
  */
  function &amp;load_class($class, $instantiate = TRUE)
  {
      static $objects = array();
  
      // Does the class exist?  If so, we're done...
      if (isset($objects[$class]))
      {
          return $objects[$class];
      }
    .......
  }
  </code></pre>
  
  <p>By placing every object into a single registry, you can't use their load_class function to create multiple instances of anything. This is especially inconvenient when you want to use classes as data structures. </p>
  
  <p>Also, because there is only one instance of all those classes, it leads into the argument against Global State. Which leads me to .....</p>
  
  <p>The entire Wordpress System, which runs mainly on global variables. All of the data for looping through posts is strewn about in various globals.</p>
  
  <p>(wp-includes\query.php Line 2644)</p>
  
  <pre><code>/**
   * Setup global post data.
   *
   *....
   */
  function setup_postdata($post) {
      global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  
      $id = (int) $post-&gt;ID;
  
      $authordata = get_userdata($post-&gt;post_author);
      ....
  }
  </code></pre>
  
  <p>These are just two main examples of Frameworks that use Singleton/Globals as the basis for their entire system!</p>
  
  <p>So .. is it just because these systems haven't caught up to OOP methodology? It just doesn't make sense when you have so many people telling you not to use Global Variables or Singletons, to make your entire system based on said practices.</p>
  
  <p>There of course is the argument about backwards-compatibility with PHP4. I still think that there were ways to do OOP programming in PHP4, as classes were still available. </p>