Re: Re: DataObjects - staticGet not working as expected
| From: | Jeroen Houben | Date: | Wed, 10 Sep 2003 10:47:31 +0000 |
| Subject: | Re: Re: DataObjects - staticGet not working as expected | ||
| References: | 1 2 3 4 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-7685@lists.php.net to get a copy of this message | ||
Alan Knowles wrote:
Jeroen Houben wrote:I've added this code to my PA_Contacts class, which extends DataObjects: function &singleton($contact_id=null) {Hi, staticGet is not working the way I expect it to. In have a class "Contacts" generated by DataObjects. I have added a propery called x. Consider the following code: $contact_id = 73; $contact =& PA_Contacts::staticGet($contact_id);$_DB_DATAOBJECT['CACHE'][$lclass][$key] = $obj; return $_DB_DATAOBJECT['CACHE'][$lclass][$key]; I thought this should be: $_DB_DATAOBJECT['CACHE'][$lclass][$key] =& $obj; return $_DB_DATAOBJECT['CACHE'][$lclass][$key];the cache is there to prevent/reduce get requests.. - I didnt think being able to play around with the cache was a particularly good idea.. (actually I didnt expect anyone to try...) Acutally looking through the code I suspect getting rid of the alias before function &staticGet is probably a good idea... ... may even speed things up... Hi Alan,
// 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);
}
now this code:
$contact =& PA_Contacts::singleton($contact_id);
$contact->x = 'test';
echo "<p>this is property x of the contact: $contact->x</p>";
$contact =& PA_Contacts::singleton($contact_id);
echo "<p>this is property x of the contact: $contact->x</p>";
exit;
now prints this:
=== start output ===
creating instance
GET contact_id 73 a:1:{i:0;s:10:"contact_id";}
__find 1
sql QUERYSELECT * FROM contacts WHERE contacts.contact_id = 73query DONE QUERY __find CHECK autofetchd 1 __find ABOUT TO AUTOFETCH fetchrow contacts DONE __find DONE this is property x of the contact: test the object for this record exists, returning it this is property x of the contact: test === end output === Do you see any problems with doing it this way? Would it be worthwhile added something like this to the base class? Jeroen