1 | <?php |
---|
2 | // +----------------------------------------------------------------------+ |
---|
3 | // | PHP Version 4 | |
---|
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 | // +----------------------------------------------------------------------+ |
---|
17 | // |
---|
18 | // $Id$ |
---|
19 | |
---|
20 | |
---|
21 | /** |
---|
22 | * Replace var_export() |
---|
23 | * |
---|
24 | * @category PHP |
---|
25 | * @package PHP_Compat |
---|
26 | * @link http://php.net/function.var_export |
---|
27 | * @author Aidan Lister <aidan@php.net> |
---|
28 | * @version $Revision: 1.15 $ |
---|
29 | * @since PHP 4.2.0 |
---|
30 | * @require PHP 4.0.0 (user_error) |
---|
31 | */ |
---|
32 | if (!function_exists('var_export')) { |
---|
33 | function var_export($var, $return = false, $level = 0) |
---|
34 | { |
---|
35 | // Init |
---|
36 | $indent = ' '; |
---|
37 | $doublearrow = ' => '; |
---|
38 | $lineend = ",\n"; |
---|
39 | $stringdelim = '\''; |
---|
40 | $newline = "\n"; |
---|
41 | $find = array(null, '\\', '\''); |
---|
42 | $replace = array('NULL', '\\\\', '\\\''); |
---|
43 | $out = ''; |
---|
44 | |
---|
45 | // Indent |
---|
46 | $level++; |
---|
47 | for ($i = 1, $previndent = ''; $i < $level; $i++) { |
---|
48 | $previndent .= $indent; |
---|
49 | } |
---|
50 | |
---|
51 | // Handle each type |
---|
52 | switch (gettype($var)) { |
---|
53 | // Array |
---|
54 | case 'array': |
---|
55 | $out = 'array (' . $newline; |
---|
56 | foreach ($var as $key => $value) { |
---|
57 | // Key |
---|
58 | if (is_string($key)) { |
---|
59 | // Make key safe |
---|
60 | for ($i = 0, $c = count($find); $i < $c; $i++) { |
---|
61 | $var = str_replace($find[$i], $replace[$i], $var); |
---|
62 | } |
---|
63 | $key = $stringdelim . $key . $stringdelim; |
---|
64 | } |
---|
65 | |
---|
66 | // Value |
---|
67 | if (is_array($value)) { |
---|
68 | $export = var_export($value, true, $level); |
---|
69 | $value = $newline . $previndent . $indent . $export; |
---|
70 | } else { |
---|
71 | $value = var_export($value, true, $level); |
---|
72 | } |
---|
73 | |
---|
74 | // Piece line together |
---|
75 | $out .= $previndent . $indent . $key . $doublearrow . $value . $lineend; |
---|
76 | } |
---|
77 | |
---|
78 | // End string |
---|
79 | $out .= $previndent . ')'; |
---|
80 | break; |
---|
81 | |
---|
82 | // String |
---|
83 | case 'string': |
---|
84 | // Make the string safe |
---|
85 | for ($i = 0, $c = count($find); $i < $c; $i++) { |
---|
86 | $var = str_replace($find[$i], $replace[$i], $var); |
---|
87 | } |
---|
88 | $out = $stringdelim . $var . $stringdelim; |
---|
89 | break; |
---|
90 | |
---|
91 | // Number |
---|
92 | case 'integer': |
---|
93 | case 'double': |
---|
94 | $out = (string) $var; |
---|
95 | break; |
---|
96 | |
---|
97 | // Boolean |
---|
98 | case 'boolean': |
---|
99 | $out = $var ? 'true' : 'false'; |
---|
100 | break; |
---|
101 | |
---|
102 | // NULLs |
---|
103 | case 'NULL': |
---|
104 | case 'resource': |
---|
105 | $out = 'NULL'; |
---|
106 | break; |
---|
107 | |
---|
108 | // Objects |
---|
109 | case 'object': |
---|
110 | // Start the object export |
---|
111 | $out = $newline . $previndent . 'class ' . get_class($var) . ' {' . $newline; |
---|
112 | |
---|
113 | // Export the object vars |
---|
114 | foreach (get_object_vars($var) as $key => $val) { |
---|
115 | $out .= $previndent . ' var $' . $key . ' = '; |
---|
116 | if (is_array($val)) { |
---|
117 | $export = var_export($val, true, $level); |
---|
118 | $out .= $newline . $previndent . $indent . $export . ';' . $newline; |
---|
119 | } else { |
---|
120 | $out .= var_export($val, true, $level) . ';' . $newline; |
---|
121 | } |
---|
122 | } |
---|
123 | $out .= $previndent . '}'; |
---|
124 | break; |
---|
125 | } |
---|
126 | |
---|
127 | // Method of output |
---|
128 | if ($return === true) { |
---|
129 | return $out; |
---|
130 | } else { |
---|
131 | echo $out; |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | ?> |
---|