note 52153 added to function.var-export

From: Date: Fri, 22 Apr 2005 13:25:24 +0000
Subject: note 52153 added to function.var-export
Groups: php.notes 
Request: Send a blank email to php-notes+get-88400@lists.php.net to get a copy of this message
Ok, here is a routine that will export any variable type you throw at it...and yes, that means objects also! It will build a class for any objects passed to it. Its usage is simply: <? $code = _var_export(mixed $var [, bool $return]); eval("\$var = $code;"); // assign the exported value/object to $var (notice the ';' required after '$code'). ?> The resulting exported code supports objects, and can simply be saved to a file or database and reloaded at another time when required by simply assigning the code via a call to eval(""). Code: <? function _var_export($var, $return=false) { $object_tree = array("\$this"); $object_index = 0; $class_code = ""; $init_code = ""; if (!function_exists("test")) { function test($a) {return $a;} } if (!function_exists("_var_export_")) { function _var_export_($var, &$object_tree, &$object_index, &$class_code, &$init_code, $indent="") { if (is_object($var)) { $class_name = get_class($var); if ($class_name == "stdClass") $class_name = "_{$class_name}_"; if ($class_code == "") { $class_code = "new $class_name();\r\nclass $class_name {\r\n"; foreach ($var as $key => $value) { $object_tree[++$object_index] = "->{$key}"; $class_code .= " var \$$key = "._var_export_($value, $object_tree, $object_index, $class_code, $init_code, " ").";\r\n"; $object_index--; } $output = rtrim($output, ";\r\n")."\r\n$indent)\r\n"; } else { $output = "null /*(object)$class_name*/"; foreach ($var as $key => $value) { $object_tree[++$object_index] = "->{$key}"; $init_code_tmp .= " ".implode(array_slice($object_tree,0,$object_index),"")."->$key = "._var_export_($value, $object_tree, $object_index, $class_code, $init_code, " ").";\r\n"; $object_index--; } $init_code = $init_code_tmp.$init_code; return $output; } if ($init_code != "") $class_code .= "\r\n function $class_name() {\r\n$init_code }\r\n"; $class = true; $class_code .= "}"; return $class_code; } elseif (is_array($var)) { if ($class_code == "") $class_code = " "; // so we dont start a class export $output = "array (\r\n"; foreach ($var as $key => $value) { $object_tree[++$object_index] = "[$key]"; $output .= "$indent '$key' => "._var_export_($value, $object_tree, $object_index, $class_code, $init_code, "$indent ").",\r\n"; $object_index--; } $output = rtrim($output, ",\r\n")."\r\n$indent)"; return $output; } elseif (is_string($var) || $var == "") { $output = "\"".addcslashes($var, "\0..\37\"@\@\177..\377")."\""; return $output; } elseif (is_bool($var)) { $output = ($var ? 'TRUE' : 'FALSE'); return $output; } else { return $var; } } } $output = _var_export_($var, $object_tree, $object_index, $class_code, $init_code); if ($return) return $output; echo $output; } ?> There may be a shorter way, but time is money for me right now. :) Any questions, feel free to email me. You can also send an email via my website form (which is the domain part of my email address). Oh, and my apologies for the lack of comments in the code. Regards, James ---- Manual Page -- http://www.php.net/manual/en/function.var-export.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52153 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52153&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52153&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52153&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52153&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52153&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52153&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88400) next »