note 109146 added to function.debug-zval-dump
| From: | theriault | Date: | Sat, 23 Jun 2012 08:43:27 +0000 |
| Subject: | note 109146 added to function.debug-zval-dump | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188765@lists.php.net to get a copy of this message | ||
You can return a consistent refcount value for any variable (and it will work in PHP 5.3+ without
problems) using the following function. 0 is returned if the variable passed has no references
outside itself or the variable doesn't exist (use isset in conjunction with this function if
you need to distinguish between the two).
<?php
/**
* Return the reference count of a variable.
* Returns 0 if a variable has no reference other than itself or doesn't exist.
*/
function refcount(&$var) {
ob_start();
debug_zval_dump(array(&$var));
return preg_replace("/^.+?refcount\((\d+)\).+$/ms", '$1',
substr(ob_get_clean(), 24), 1) - 4;
}
# Example
$a = 34;
refcount($a) == 0;
$b = &$a;
refcount($a) == 1;
?>
Using the above function as a dependency, you can determine if two variables reference the same
space in memory using another small function:
<?php
/**
* Return whether a and b reference the same memory
*/
function reference(&$a, &$b) {
$d = refcount($b);
$e = &$a;
return refcount($b) != $d;
}
# Example
$a = 3;
$b = 5;
$c = &$a;
$d = &$c;
reference($a, $b) == false;
reference($a, $c) == true;
reference($a, $d) == true;
?>
----
Server IP: 192.41.42.26
Probable Submitter: 76.25.105.79
----
Manual Page -- http://www.php.net/manual/en/function.debug-zval-dump.php
Edit -- https://master.php.net/note/edit/109146
Del: integrated -- https://master.php.net/note/delete/109146/integrated
Del: useless -- https://master.php.net/note/delete/109146/useless
Del: bad code -- https://master.php.net/note/delete/109146/bad+code
Del: spam -- https://master.php.net/note/delete/109146/spam
Del: non-english -- https://master.php.net/note/delete/109146/non-english
Del: in docs -- https://master.php.net/note/delete/109146/in+docs
Del: other reasons-- https://master.php.net/note/delete/109146
Reject -- https://master.php.net/note/reject/109146
Search -- https://master.php.net/manage/user-notes.php