Re: Re: DataObjects - staticGet not working as expected

From: Date: Wed, 10 Sep 2003 11:02:22 +0000
Subject: Re: Re: DataObjects - staticGet not working as expected
References: 1 2 3 4 5  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-7686@lists.php.net to get a copy of this message
Jeroen Houben wrote:
Hi Alan, I've added this code to my PA_Contacts class, which extends DataObjects: function &singleton($contact_id=null) {
       // Declare a static variable to hold the object instance
       static $instance;
         // If the instance is not there, create one
       if (!isset($instance)) {
           echo 'creating instance';
           $instance = new PA_Contacts;
           if ($contact_id) {
               $instance->get($contact_id);
           }
       } else {
           echo 'the object for this record exists, returning it';
       }
       return($instance);
} Actually this code will work better I think:
function &singleton($contact_id=null) {
       // Declare a static variable to hold the object instance
       static $instances = array();
         // If the instance is not there, create one
       if (!isset($instances[$contact_id])) {
           echo 'creating instance <br><br>';
           $instances[$contact_id] = new PA_Contacts;
           if ($contact_id) {
               $instances[$contact_id]->get($contact_id);
           }
           $instances[$contact_id]->loadQuickForm();
       } else {
           echo 'the object for this record exists, returning it';
       }
       return($instances[$contact_id]);
}

« previous php.pear.general (#7686) next »