note 75353 added to language.references.spot

From: Date: Fri, 25 May 2007 07:20:29 +0000
Subject: note 75353 added to language.references.spot
Groups: php.notes 
Request: Send a blank email to php-notes+get-126885@lists.php.net to get a copy of this message
Someone said that BenBe's function is_ref() was broken for testing objects. I didn't test it but I rewrote it and I know it works for objects. Here is the new version : <?php function is_ref(&$a, &$b){ if(gettype($a) !== gettype($b)) return false; $same = false; if(is_array($a)){ //Look for an unused index in $a $key = uniqid("is_ref_", true); while(isset($a[$key]))$key = uniqid("is_ref_", true); //The two variables differ in content ... They can't be the same if(isset($b[$key])) return false; //The arrays point to the same data if changes are reflected in $b $data = uniqid("is_ref_data_", true); $a[$key] =& $data; //There seems to be a modification ... $same = ((isset($b[$key])) and ($b[$key] === $data)); //Undo our changes ... unset($a[$key]); }elseif(is_object($a)){ //The same objects are required to have equal class names ;-) if(get_class($a) !== get_class($b)) return false; //Look for an unused property in $a $key = uniqid("is_ref_", true); while(isset($a->$key))$key = uniqid("is_ref_", true); //The two variables differ in content ... They can't be the same if(isset($b->$key)) return false; //The arrays point to the same data if changes are reflected in $b $data = uniqid("is_ref_data_", true); $a->$key =& $data; //There seems to be a modification ... $same = ((isset($b->$key)) and ($b->$key === $data)); //Undo our changes ... unset($a->$key); }elseif(is_resource($a)){ if(get_resource_type($a) !== get_resource_type($b))return false; $same = ((string) $var1) === ((string) $var2); }else{ if($a !== $b) return false; //To check for a reference of a variable with simple type //simply store its old value and check against modifications of the second variable ;-) $data = uniqid("is_ref_", true); while($data === $a) $data = uniqid("is_ref_", true); $save = $a; //WE NEED A COPY HERE!!! $a = $data; //Set a to the value of $data (copy) $same = ($a === $b); //Check if $var2 was modified too ... $a = $save; //Undo our changes ... } return $same; } ?> ---- Server IP: 195.221.21.36 Probable Submitter: 194.206.50.185 ---- Manual Page -- http://www.php.net/manual/en/language.references.spot.php Edit -- https://master.php.net/note/edit/75353 Del: integrated -- https://master.php.net/note/delete/75353/integrated Del: useless -- https://master.php.net/note/delete/75353/useless Del: bad code -- https://master.php.net/note/delete/75353/bad+code Del: spam -- https://master.php.net/note/delete/75353/spam Del: non-english -- https://master.php.net/note/delete/75353/non-english Del: in docs -- https://master.php.net/note/delete/75353/in+docs Del: other reasons-- https://master.php.net/note/delete/75353 Reject -- https://master.php.net/note/reject/75353 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126885) next »