2 // +----------------------------------------------------------------------+
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997-2004 The PHP Group |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 3.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/3_0.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+
15 // | Authors: Aidan Lister <aidan@php.net> |
16 // +----------------------------------------------------------------------+
18 // $Id: var_export.php,v 1.18 2006/05/22 00:09:42 arpad Exp $
22 * Replace var_export()
26 * @link http://php.net/function.var_export
27 * @author Aidan Lister <aidan@php.net>
28 * @version $Revision: 1.18 $
30 * @require PHP 4.0.0 (user_error)
32 function php_compat_var_export($var, $return = false, $level = 0, $inObject = false)
36 $doublearrow = ' => ';
40 $find = array(null, '\\', '\'');
41 $replace = array('NULL', '\\\\', '\\\'');
46 for ($i = 1, $previndent = ''; $i < $level; $i++
) {
47 $previndent .= $indent;
50 $varType = gettype($var);
52 // Handle object indentation oddity
53 if ($inObject && $varType != 'object') {
54 $previndent = substr($previndent, 0, -1);
63 $out .= $newline . $previndent;
65 $out .= 'array (' . $newline;
66 foreach ($var as $key => $value) {
68 if (is_string($key)) {
70 $key = str_replace($find, $replace, $key);
71 $key = $stringdelim . $key . $stringdelim;
75 if (is_array($value)) {
76 $export = php_compat_var_export($value, true, $level);
77 $value = $newline . $previndent . $indent . $export;
79 $value = php_compat_var_export($value, true, $level);
82 // Piece line together
83 $out .= $previndent . $indent . $key . $doublearrow . $value . $lineend;
87 $out .= $previndent . ')';
92 // Make the string safe
93 for ($i = 0, $c = count($find); $i < $c; $i++
) {
94 $var = str_replace($find[$i], $replace[$i], $var);
96 $out = $stringdelim . $var . $stringdelim;
102 $out = (string) $var;
107 $out = $var ?
'true' : 'false';
118 // Start the object export
119 $out = $newline . $previndent;
120 $out .= get_class($var) . '::__set_state(array(' . $newline;
121 // Export the object vars
122 foreach(get_object_vars($var) as $key => $value) {
123 $out .= $previndent . $indent . ' ' . $stringdelim . $key . $stringdelim . $doublearrow;
124 $out .= php_compat_var_export($value, true, $level, true) . $lineend;
126 $out .= $previndent . '))';
131 if ($return === true) {
140 if (!function_exists('var_export')) {
141 function var_export($var, $return = false)
143 return php_compat_var_export($var, $return);