note 109146 deleted from function.debug-zval-dump by cmb

From: Date: Mon, 06 Jun 2016 18:23:53 +0000
Subject: note 109146 deleted from function.debug-zval-dump by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-205674@lists.php.net to get a copy of this message
Note Submitter: theriault ---- 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; ?>

« previous php.notes (#205674) next »