Created
March 4, 2010 10:02
-
-
Save v6ak/321601 to your computer and use it in GitHub Desktop.
PHP zval experiments
This file contains 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 | |
final class v6_Variable{ | |
private $val; | |
public function set($val){ | |
$this->val = $val; | |
} | |
public function get(){ | |
return $this->val; | |
} | |
} | |
final class v6_Zval{ | |
private function __construct(){} | |
public static function getZvalDump($a){ | |
$sc = new v6_Variable(); | |
ob_start(array($sc, 'set')); | |
debug_zval_dump($a); | |
ob_end_flush(); | |
return $sc->get(); | |
} | |
public static function isIdentical($a, $b){ | |
$bd1 = self::getZvalDump($b); | |
$aref = $a; | |
$bd2 = self::getZvalDump($b); | |
return $bd1 !== $bd2; | |
} | |
} | |
function testIdentity($a, $b, $expected, $comment=''){ | |
echo $comment.' should be '.var_export($expected, true).': '; | |
if(v6_Zval::isIdentical($a, $b) === $expected){ | |
echo 'OK'; | |
}else{ | |
echo 'ERROR!'; | |
} | |
} | |
$a = 1; | |
$b = 1; | |
testIdentity($a, $b, false, ''); | |
testIdentity($b, $a, false, ''); | |
$a = $b; | |
testIdentity($a, $b, true, ''); | |
testIdentity($b, $a, true, ''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment