title: Building a Singleton Trait with PHP 5.4
body: <p>We recently had a discussion if it was possible to build a <code>trait Singleton</code> <a href="http://www.php.net/manual/en/language.oop5.traits.php" rel="nofollow noreferrer"><strong>PHP Traits</strong></a> and we played around with it a possible Implementation but ran into issues with building one.</p>
  
  <p><strong>This is an academic exercise.</strong> I know that <a href="https://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323"><strong><code>Singletons have very little - if not to say no - use in PHP</code></strong></a> and that <a href="http://gooh.posterous.com/singletons-in-php" rel="nofollow noreferrer"><strong><code>one should 'just create one'</code></strong></a> but just for exploring the possibilities of traits:</p>
  
  <pre><code>&lt;?php
  trait Singleton
  {
      protected static $instance;
      final public static function getInstance()
      {
          return isset(static::$instance)
              ? static::$instance
              : static::$instance = new static;
      }
      final private function __construct() {
          static::init();
      }
      protected function init() {}
      final private function __wakeup() {}
      final private function __clone() {}    
  }
  
  class A  {
      use Singleton;
      public function __construct() {
          echo "Doesn't work out!";
      }
  }
  
  $a = new A(); // Works fine
  </code></pre>
  
  <p>reproduce: <a href="http://codepad.viper-7.com/NmP0nZ" rel="nofollow noreferrer">http://codepad.viper-7.com/NmP0nZ</a></p>
  
  <p>The question is: It is possible to create a Singleton Trait in PHP? </p>