Re: Re: DataObjects - staticGet not working as expected
| From: | Alan Knowles | Date: | Wed, 10 Sep 2003 14:13:56 +0000 |
| Subject: | Re: Re: DataObjects - staticGet not working as expected | ||
| References: | 1 2 3 4 5 6 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-7692@lists.php.net to get a copy of this message | ||
I'll have to think about this one...
my initial instinct is to depreciate staticGet() with cachedGet(), especially as I'd like to move away from the rather kludgy implementation of staticGet -(that means it has to be redefined in each child..) ** a similar issue exists with the singleton idea below..
something along the lines of...
$do = DB_DataObject::factory('contacts');
$x = &$do->cachedGet(123);
or using extra args of factory:
$do = DB_DataObject::factory('contacts',array(
'get'=>12,
'cached'=>true
));
or more advanced...
$do = DB_DataObject::factory('contacts',array(
'get'=> array(
'id'=>12
'age'=>30
),
'cached'=>true
));
Regards
Alan
Jeroen Houben wrote:
Jeroen Houben wrote:-- Can you help out? Need Consulting Services or Know of a Job? http://www.akbkhome.comHi Alan, I've added this code to my PA_Contacts class, which extends DataObjects: function &singleton($contact_id=null) {Actually this code will work better I think: 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);}// 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]);}