note 52153 deleted from function.var-export by aidan

From: Date: Sun, 01 May 2005 04:59:29 +0000
Subject: note 52153 deleted from function.var-export by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-88811@lists.php.net to get a copy of this message
Note Submitter: james at colossalus dot com ---- 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

« previous php.notes (#88811) next »