Skip to content

Instantly share code, notes, and snippets.

@v6ak
Created March 4, 2010 10:02
Show Gist options
  • Save v6ak/321601 to your computer and use it in GitHub Desktop.
Save v6ak/321601 to your computer and use it in GitHub Desktop.
PHP zval experiments
<?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