Created
April 25, 2016 16:21
-
-
Save abiusx/ec92a00e4839decdbc8963822f737611 to your computer and use it in GitHub Desktop.
Provides a unique identifier for each zval. Same zvals should have same identifier.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a hacky zval_id implementation. | |
* It tries to be as fast as possible, | |
* and can only detect unique arrays, leaving them | |
* modified | |
* When in doubt, assumes it's a new zval! | |
* @param mixed &$zval | |
* @return integer | |
*/ | |
function _zval_id_light(&$zval) | |
{ | |
static $id=0; | |
if (is_array($zval)) | |
{ | |
if (isset($zval['___VISITED___'])) | |
return $zval['___VISITED___']; | |
else | |
return $zval['___VISITED___']=$id++; | |
} | |
else | |
return $id++; | |
} | |
/** | |
* This is a more accurate zval_id | |
* implementation. for each new zval, | |
* it compares it to all previously checked | |
* zval and if they are the same, same id is returned. | |
* @param mixed &$zval | |
* @return integer | |
*/ | |
function _zval_id_accurate(&$zval) | |
{ | |
static $zvals=[]; | |
static $ids=0; | |
$backup=$zval; | |
$zval="___VISITED___"; | |
foreach ($zvals as $k=>$v) | |
if ($v==='___VISITED___') | |
{ | |
$id=$k; | |
break; | |
} | |
if (!isset($id)) | |
$id=$ids++; | |
$zval=$backup; | |
$zvals[$id]=&$zval; | |
return $id; | |
} | |
function _zval_id(&$zval) { | |
return _zval_id_accurate($zval); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment