[php-src] Issue #12790: var_export() with support for new array syntax as of 5.4+
| From: | timint | Date: | Mon, 27 Nov 2023 01:13:58 +0000 |
| Subject: | [php-src] Issue #12790: var_export() with support for new array syntax as of 5.4+ | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245899@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12790
Author: timint
### Description
I would like to open this topic for discussing improvements to var_export() as I had two things come
to mind today.
1. Add support for short array syntax [] as of PHP 5.4+.
2. Add ability to choose indentation type (tab, 2-spaces, 4-spaces) or no indentation/minified.
PHP offers several ways to serialize or dump variable contents. But I feel none of them honours the
often promoted use of how we type PHP variables in the modern day.
Let's take a look at our options today.
```
$vars = [
new stdclass(),
['foo' => 'bar'],
'foo',
true,
];
echo 'print_r:' . PHP_EOL;
print_r($vars);
echo PHP_EOL;
echo 'var_dump:' . PHP_EOL;
var_dump($vars);
echo PHP_EOL;
echo 'var_export:' . PHP_EOL;
var_export($vars);
echo PHP_EOL . PHP_EOL;
echo 'serialize:' . PHP_EOL;
echo serialize($vars) . PHP_EOL . PHP_EOL;
echo 'json_encode:' . PHP_EOL;
echo json_encode($vars, JSON_PRETTY_PRINT) . PHP_EOL . PHP_EOL;
```
What they output:
```
print_r:
Array
(
[0] => stdClass Object
(
)
[1] => Array
(
[foo] => bar
)
[2] => foo
[3] => 1
)
var_dump:
array(4) {
[0]=>
object(stdClass)#1 (0) {
}
[1]=>
array(1) {
["foo"]=>
string(3) "bar"
}
[2]=>
string(3) "foo"
[3]=>
bool(true)
}
var_dump:
array (
0 =>
(object) array(
),
1 =>
array (
'foo' => 'bar',
),
2 => 'foo',
3 => true,
)
serialize:
a:4:{i:0;O:8:"stdClass":0:{}i:1;a:1:{s:3:"foo";s:3:"bar";}i:2;s:3:"foo";i:3;b:1;}
serialize:
[
{},
{
"foo": "bar"
},
"foo",
true
]
```
We are missing maybe the most common variant?:
```
[
[],
[
'foo' => 'bar',
],
'foo',
true
]
```
Like this topic if you find this to be a good idea.