unique identifier for zvals in user-land?

From: Date: Sat, 02 Sep 2000 01:44:38 +0000
Subject: unique identifier for zvals in user-land?
Groups: php.dev 
Request: Send a blank email to php-dev+get-31632@lists.php.net to get a copy of this message
Problem: It´s not possible to identify all kinds of variables (in fact it´s only possible for resources (eg. resource id #4)) based on their zval. Task: Create method that caches returned values based on input as follows: Implementation (rough):
function cache_result($input)    {
 if (!isset($this->cache_result[$input]))    {
$this->cache_result[$input]=calculate_somthings($input); } return $this->cache_result[$input]; } That´s no problem if input(parameter) are strings, integers or floats. Storing them as an index in that array is quite reasonable but storing (array)s,(object)s and (resources) is a BIG problem. For resources there´s a very nice workaround, because 'echo $resource;' echo´s out 'resource id #2' etc. (and of course I don´t use the resources directly as indices, they won´t be freed then before the script ends) thus... ob_start(); print $resource; $array_index=ob_get_contents(); ob_end_clean(); ... is even better than storing (string)s, (integer)s or (double)s, because they of course (especially can be very large, then another workaround is to md5() strings), ok. many workarounds here, but the real problem persists for (object)s and (array)s, storing them as an array will clutter the object with this cache method used in more and more. Solution: Having a function that returns the "adress" of a particular zval or another unique identifier to identify that zval. Example:
function cache_result($input)    {
$cache_index=zval_ident($input);
 if (!isset($this->cache_result[$cache_index]))    {
$this->cache_result[$cache_index]=calculate_somthings($input); } return $this->cache_result[$cache_index]; } I doesn´t care if it returns the 'real' memory adress (0x0230230) or maybe some sort of an enumarated zval index (4,240 whatever). It´s currently not possible to realize such things on a reasonable basis and for objects and arrays not all without risking that the object gets messed up more and more. I´ve found this kind of caching quite useable and encountered the mentioned drawbacks, I think it shouldn´t be as hard to create a function that returns the adress (and nobody needs to be worried that people are starting weird things with this information, they just can´t use it beyond, because there´s no possibility to access adresses directly in PHP) andré

« previous php.dev (#31632) next »