Skip to content

Instantly share code, notes, and snippets.

@pjdietz
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjdietz/0f71a3aab9b37e589500 to your computer and use it in GitHub Desktop.
Save pjdietz/0f71a3aab9b37e589500 to your computer and use it in GitHub Desktop.
Return the number of references for a variable's zval
<?php
/**
* Return the number of references for a variable's zval
*
* @param mixed $var Variable to inspect
* @return int Number of references according to debug_zval_dump
*/
function refCount($var)
{
ob_start();
debug_zval_dump($var);
$dump = ob_get_clean();
if (preg_match('~refcount\(([\d]+)\)~', $dump, $matches)) {
$refcount = $matches[1];
return --$refcount; // Don't count the reference to this function
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment