php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #29995 Want to be able to morph objects with $this = &$new_obj;
Submitted: 2004-09-06 09:54 UTC Modified: 2004-09-06 20:29 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: tuxie at dekadance dot se Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.0.1 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tuxie at dekadance dot se
New email:
PHP Version: OS:

 

 [2004-09-06 09:54 UTC] tuxie at dekadance dot se
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);



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-09-06 20:29 UTC] helly@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

Use a Factory
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 12:01:32 2024 UTC