|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-10-11 10:26 UTC] manu at chasqui dot cu
Description:
------------
var_export fails to reproduce valid code when dumping several nested objects.
Reproduce code:
---------------
<?php
class VE_Test
{
var $child;
}
$ve_child = new VE_Test();
$ve_child->child = NULL;
$ve_parent = new VE_Test();
$ve_parent->child = &$ve_child;
var_export($ve_parent);
?>
Expected result:
----------------
Actually I can't make a proposal without requiring more from PHP:
with (new VE_Test()){
child =
with (new VE_Test()){
child = NULL;
}
}
This is just a proposal, there can be other better ways.
Actual result:
--------------
class ve_test {
var $child =
class ve_test {
var $child = NULL;
};
}
Which fails to compile:
Parse error: parse error, unexpected T_CLASS in d:\inetpub\tests\tests\php\vd.php on line 4
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 21:00:01 2025 UTC |
As I promised *long ago*, here is a small function that implements the idea. Notice, I left circular reference loops unchecked, though it is not hard to add this check. I did this in part, because any way I wasn't changing the very var_export function :) Again, notice I do not expect the class definition. Test code: <?php function obj_var_export($obj) { // TODO: Maintain a global list of references and fix-ups // to avoid forever-loops when circular references // are found. if (is_object($obj)) { $classn = get_class($obj); $result = "with (new $classn())\n{\n"; $vars = get_object_vars($obj); foreach($vars as $n => $v) $result .= "\$$n = ".obj_var_export($v)."\n"; $result .= "}\n"; return $result; } else // var_export would be modified in order that any component // of obj is an object (e.g an array containing objects) return var_export($obj, true).";"; } class VE_Test { var $child; var $str = "hi!"; } $ve_child = new VE_Test(); $ve_child->child = NULL; $ve_parent = new VE_Test(); $ve_parent->child = &$ve_child; echo obj_var_export($ve_parent); echo "<br/><br/>"; echo var_export($ve_parent); ?>