From:             tuxie at dekadance dot se
Operating system: 
PHP version:      5.0.1
PHP Bug Type:     Feature/Change Request
Bug description:  Want to be able to morph objects with $this = &$new_obj;

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);



-- 
Edit bug report at http://bugs.php.net/?id=29995&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=29995&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=29995&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=29995&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=29995&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=29995&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=29995&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=29995&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=29995&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=29995&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=29995&r=notwrong
Not enough info:             http://bugs.php.net/fix.php?id=29995&r=notenoughinfo
Submitted twice:             http://bugs.php.net/fix.php?id=29995&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=29995&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=29995&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=29995&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=29995&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=29995&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=29995&r=float

Reply via email to