title: Library as Singleton in Codeigniter?
body: <p>I've created a custom library using singleton pattern. Why by this way? because I need to be able to call my functions without <strong>$this-></strong> reference, for example, I could execute the code below:</p>
  
  <pre><code>function foo() {
      MyLibrary::instance()-&gt;foo();        
  }
  </code></pre>
  
  <p>then I could call my functions in controllers like this:</p>
  
  <pre><code>function foo();
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>$this-&gt;mylibrary-&gt;foo();
  </code></pre>
  
  <p>I am in trouble because CodeIgniter try to instantiate my library when the best way to do is "read" the static instance.  </p>
  
  <p>Why I need to do this?</p>
  
  <p>My custom library must register "hooks" from external php files located in /plugins directory outside of application folder. Look:</p>
  
  <ul>
  <li>/application</li>
  <li>/plugins</li>
  <li>/plugins/helloworld.php</li>
  </ul>
  
  <p>This is my Loader library:</p>
  
  <pre><code>class Loader{
  
      public static $instance;
        protected $hooks;
  
      private function __construct($params = array()){
          $this-&gt;hooks= array();
          $this-&gt;includePlugins();
      }
  
      public static function instance()
          {
              if (!self::$instance)
              {
                  self::$instance = new Loader();
              }
  
              return self::$instance;
          }
  
      public function includePlugins()
      {
          include_once "/plugins/helloworld.php";  
      }
  
      public function do_action($hook= "after")
      {
          foreach ($this-&gt;actions[$hook] as $function)
          {
              call_user_func_array($function, array());
          }
  
      }
  
      public function add_action($hook, $function, $priority)
      {
          if ($hooks !== false)
          {
          $this-&gt;actions[$hook][] = $function;
          }
          else
          {
          echo "hook name is unavalaible";
          }
      }
  
  }
  
  
  
  /**
   * Functions using the Singleton instance !!!
  */
  
  function do_action($hook)
  {
      Loader::instance()-&gt;do_action($hook);
  }
  
  function add_action($hook, $function, $priority)
  {
      Loader::instance()-&gt;add_action($hook, $function, $priority);
  }
  </code></pre>
  
  <p>No, my helloworld.php (plugin) looks like this:</p>
  
  <pre><code>add_action('right_here', 'show_message', 11);
  function show_message()
  {
      echo "hello world!";
  }
  </code></pre>
  
  <p>Finally, I could call my the function do_action in my controller/view  in order to print the message, like this:</p>
  
  <pre><code>do_action('right_here');
  </code></pre>
  
  <p>Note that all my important functions are called by a global function that allow me to use the singleton instance. But I am experiencing two issues:</p>
  
  <ol>
  <li>Codeigniter needs that my contruct be public in order to
  instantiate. Here I need to take my singleton.</li>
  <li>If I let it public, several instances will be created and the library
  will no longer works as expected.</li>
  </ol>