title: PHP Getting a reference through a constructor?
body: <p>I am wondering if it is possible while using the keyword 'new' to return a reference to an existing object instead of a new object. And if not, what would be the best way to avoid co-existence of objects with the same parameters?  </p>
  
  <p>I have a class with arguments on the constructor. If I am using arguments similar to the ones of an existing instance, I'd like my new object to be a reference to the existing one. Is it possible? I understand it is different than a singleton as in this case we can have several instances of the same class, but not several instances using the same parameters.</p>
  
  <pre><code>class myClass{
   public $signature; /* Unique signature for the connection */
   public static $instances = array(); /* Array of references to connection */
   private static function singleton($cfg,$inst){
     $signature = sha1(serialize($cfg));
     foreach ( self::$instances as $obj )
     {
       if ( $obj-&gt;signature == $signature )
         return $obj;
     }
     array_push(self::$instances,$inst);
     return true;
   }
   function __construct($myParam){
     if ( is_object(self::singleton($myParam,$this) )
       // Here I'd like $this to become the ref of my existing object
     else
     {
       $this-&gt;signature = $myParam;
       // I construct my new object
     }
   }
  }
  $e1 = new myClass(1);
  $e2 = new myClass(2);
  $e3 = new myClass(1); // $e3 should be a reference to $e1
  </code></pre>