Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.6.0a3

Request #7414 making var_export() generating correct PHP code with nested objects
Submitted: 2006-04-17 20:44 UTC
From: kenashkov at gmail dot com Assigned: arpad
Status: Closed Package: PHP_Compat (version 1.5.0)
PHP Version: 4.4.1 OS: GNU/Linux (fedora Core 4)
Roadmaps: 1.6.0a1    
Subscription  


 [2006-04-17 20:44 UTC] kenashkov at gmail dot com (Vesselin Kenashkov)
Description: ------------ Backporting the fix for this http://bugs.php.net/bug.php?id=29361 (changing the generated code for objects - replacing the class definition - the class::__set_state call). Here is a modification of the original code found in PHP/Compat/Function/var_export.php Not tested to be identical to php5.1 function ! Just tested to be working and follow the representation from the examples. ----------------------------------------------- if (!function_exists('var_export')) { function var_export($var, $return = false, $level = 0) { // Init $indent = ' '; $doublearrow = ' => '; $lineend = ",\n"; $stringdelim = '\''; $newline = "\n"; $find = array(null, '\\', '\''); $replace = array('NULL', '\\\\', '\\\''); $out = ''; // Indent $level++; for ($i = 1, $previndent = ''; $i < $level; $i++) { $previndent .= $indent; } // Handle each type switch (gettype($var)) { // Array case 'array': $out = 'array (' . $newline; foreach ($var as $key => $value) { // Key if (is_string($key)) { // Make key safe for ($i = 0, $c = count($find); $i < $c; $i++) { $var = str_replace($find[$i], $replace[$i], $var); } $key = $stringdelim . $key . $stringdelim; } // Value if (is_array($value)) { $export = var_export($value, true, $level); $value = $newline . $previndent . $indent . $export; } else { $value = var_export($value, true, $level); } // Piece line together $out .= $previndent . $indent . $key . $doublearrow . $value . $lineend; } // End string $out .= $previndent . ')'; break; // String case 'string': // Make the string safe for ($i = 0, $c = count($find); $i < $c; $i++) { $var = str_replace($find[$i], $replace[$i], $var); } $out = $stringdelim . $var . $stringdelim; break; // Number case 'integer': case 'double': $out = (string) $var; break; // Boolean case 'boolean': $out = $var ? 'true' : 'false'; break; // NULLs case 'NULL': case 'resource': $out = 'NULL'; break; // Objects case 'object': // Start the object export $out = get_class($var) . '::__set_state(array('.$newline; foreach(get_object_vars($var) as $key=>$value) { $out .= $indent . $previndent . $stringdelim . $key . $stringdelim . $doublearrow; if(is_array($value) || is_object($value)) { $export = var_export($value, true, $level); $out .= $export . $lineend; } else { $out .= var_export($value, true, $level) . $lineend; } } $out .= $previndent . '))'; break; } // Method of output if ($return === true) { return $out; } else { echo $out; } } } ----------------------------------------------- Test script: --------------- class aa{ function __set_state($vars=array()) { $obj =& new aa(); foreach($vars as $key=>$value) $obj->$key = $value; return $obj; } } $obj = new aa; $obj->nested_obj = new aa; var_export(obj); Expected result: ---------------- aa::__set_state(array( 'nested_obj' => aa::__set_state(array( )), )) Actual result: -------------- class aa { var $nested_obj = class aa { }; }

Comments

 [2006-05-22 00:18 UTC] arpad (Arpad Ray)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.