|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-09-06 20:29 UTC] helly@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 09:00:01 2025 UTC |
Description: ------------ I make extensive use of Memcache in my application. A simplified version of many classes I have: class Test { private $id; private $data = array(); private $cachekey; public function __construct($id) { global $cache; $this->id = $id; $this->cachekey = 'Test:'. $id; $this->data = /* Fill it from the database */; // Cache myself: $cache->set($this->cacheid, $this, 0, 0); } public function __get($var) { return $this->data[$var]: } public function __set($var,$val) { global $cache; if( $this->data[$var] == $val) return; /* update database here*/ // Update myself in the cache: $this->data[$var] = $val; $cache->set($this->cachekey, $this, 0, 0); } } On every page I want to use the object I now have to do: if( false === $testObj = $cache->get('Test:'.$id) ) { $testObj = new Test($id); } Now here is my feature request: If I could assign $this in the constructor my code would be much more readable and the caching details would be completly transparent to the user of the class. Then the constructor could look like this: public function __construct($id) { global $cache; if( $cachedObj = $cache->get('Test:'.$id) ) $this = &$cachedObj; else { $this->id = $id; $this->cachekey = 'Test:'. $id; $this->data = /* Fill it from the database */; // Cache myself: $cache->set($this->cacheid, $this, 0, 0); } } Using the class would now be simple and standard: $testObj = new Test($id);