rfc:var-export-array-syntax

This is an old revision of the document!


PHP RFC: Change var_export() array syntax to use shorthand arrays

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

Instead of

array(1, 2, 3)

var_export() would produce

[1, 2, 3]

This will effect things like stdClass and set_state as well since they are cast to objects from array literals and they use the long-form array syntax above.

So the following changes are also in effect:

$obj = new stdClass;
$obj->foo = "bar";
$obj->baz = "quix";
 
var_export($obj);
 
/*
Gives us:
(object) array(
   'foo' => 'bar',
   'baz' => 'quix',
)
 
With the new change it would be
 
(object) [
   'foo' => 'bar',
   'baz' => 'quix',
]
*/

The same happens for classes:

class Foo {
    public $bar = "baz";
}
 
var_export(new Foo);
 
/*
Gives us:
Foo::__set_state(array(
   'bar' => 'baz',
))
 
With the changes it would be:
Foo::__set_state([
   'bar' => 'baz',
])
*/

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.

However, there are some tests that test the output of var_export, though I view this as a broken test more than I do a BC change. I liken this to a test which uses the value of a constant like E_ALL rather than just using the constant itself in testing that the constant works. Why not just test that var_export gives you back an array rather than test its output? It's all valid PHP code that we care about not the output itself. The output value, like the constant value, is subject to change. That's why we use it in the first place. To prevent change from effecting the test (constant).

I hope that's clear.

In the event that this RFC is voted through I will, however, be updating these tests.

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

Rejected Features

rfc/var-export-array-syntax.1585584140.txt.gz · Last modified: 2020/03/30 16:02 by googleguy