PHP RFC: Change var_export() array syntax to use shorthand arrays
- Version: 1.0
- Date: 2020-03-29
- Author: Sherif Ramadan, googleguy@php.net
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/var-export-array-syntax
Introduction
This RFC proposes to change var_export()'s array syntax to use the new short-hand arrays first introduced in PHP 5.4. The old array() construct is a kludge and is unappealing. The shorter syntax may be easier to read and takes up less space and is also more ubiquitous with JSON notation.
Proposal
This change proposes adding a third optional argument for var_export()
and 3 new bit-wise flags as follows:
- VAR_EXPORT_SHORT_ARRAY
- VAR_EXPORT_NO_INDEX
- VAR_EXPORT_COMPACT
VAR_EXPORT_SHORT_ARRAY
triggers the short-hand syntax for arrays which affects all 3 cases (arrays, stdClass objects, other classes objects). VAR_EXPORT_NO_INDEX
will discard sequential numbered indexes starting from 0, which is currently the default behavior to include them. VAR_EXPORT_COMPACT
will compact the output to one line rather than adding the additional new line characters at each stage.
Each option can be used alone, and can also be combined with other(s).
For example, var_export([1, 2, 3]);
produces
array ( 0 => 1, 1 => 2, 2 => 3, )
and the new var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY);
would produce
[ 0 => 1, 1 => 2, 2 => 3, ]
This would affect stdClass
and other classes objects as well since they are exported using array literals (for (object)
casting or __set_state()
call) and they use the long-form array syntax above.
So the following changes would also be in effect:
$obj = new stdClass; $obj->foo = "bar"; $obj->baz = "quix"; var_export($obj); /* (object) array( 'foo' => 'bar', 'baz' => 'quix', ) */ var_export($obj, false, VAR_EXPORT_SHORT_ARRAY); /* (object) [ 'foo' => 'bar', 'baz' => 'quix', ] */
Same for other classes:
class Foo { public $bar = "baz"; } var_export(new Foo); /* Foo::__set_state(array( 'bar' => 'baz', )) */ var_export(new Foo, false, VAR_EXPORT_SHORT_ARRAY); /* Foo::__set_state([ 'bar' => 'baz', ]) */
Using the other bitwise flags you could also do things like...
var_export([1, 2, 3], false, VAR_EXPORT_NO_INDEX); /* array ( 1, 2, 3, ) */ var_export([1, 2, 3], false, VAR_EXPORT_COMPACT); /* array (0 => 1, 1 => 2, 2 => 3) */
and combine them...
var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_NO_INDEX); /* [ 1, 2, 3, ] */ var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_COMPACT); /* [0 => 1, 1 => 2, 2 => 3] */ var_export([1, 2, 3], false, VAR_EXPORT_NO_INDEX | VAR_EXPORT_COMPACT); /* array (1, 2, 3) */ var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_NO_INDEX | VAR_EXPORT_COMPACT); /* [1, 2, 3] */
Backward Incompatible Changes
There shouldn't be any backwards incompatible changes as var_export()
will continue to produce valid PHP code such that var_export()
to PHP and PHP back to var_export()
will continue to work as expected. The syntax changes are all forwards compatible as of PHP 5.4 so we shouldn't see any issues here.
Proposed PHP Version(s)
PHP 8.0
RFC Impact
To SAPIs
None.
To Existing Extensions
None.
To Opcache
None.
Future Scope
None.
Proposed Voting Choices
To vote will be to either change var_export()
's array syntax to use the new short hand syntax or do not implement the change at all. I am not including an option to keep the old behavior, because I don't think it necessary. The change is forwards compatible and the old behavior should have zero effect on the outcome of the function's intent to produce valid PHP code. It is merely for aesthetics at that point.
Patches and Tests
The change only requires changing two lines in ext/standard/var.c
(lines 530 and 540) to replace array (
and )
with [
and ]
.
See https://heap.space/xref/php-src/ext/standard/var.c?r=a9398056#530-540 for reference.
Implementation
References
https://heap.space/xref/php-src/ext/standard/var.c?r=a9398056#530-540