rfc:var-export-array-syntax

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:var-export-array-syntax [2020/03/29 17:35] – created googleguyrfc:var-export-array-syntax [2020/04/10 09:24] (current) – typos, examples guilliamxavier
Line 3: Line 3:
   * Date: 2020-03-29   * Date: 2020-03-29
   * Author: Sherif Ramadan, googleguy@php.net   * Author: Sherif Ramadan, googleguy@php.net
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/var-export-array-syntax   * First Published at: http://wiki.php.net/rfc/var-export-array-syntax
  
Line 12: Line 12:
  
 ===== Proposal ===== ===== Proposal =====
-Instead of ''array(1, 2, 3)'' var_export() would produce ''[1, 2, 3]''+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, <php>var_export([1, 2, 3]);</php> produces 
 + 
 +<code php> 
 +array ( 
 +  0 => 1, 
 +  1 => 2, 
 +  2 => 3
 +) 
 +</code> 
 + 
 +and the new <php>var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY);</php> would produce 
 + 
 +<code php> 
 +[ 
 +  0 => 1, 
 +  1 => 2, 
 +  2 => 3
 +] 
 +</code> 
 + 
 +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: 
 + 
 +<code php> 
 +$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', 
 +
 +*/ 
 +</code> 
 + 
 +Same for other classes: 
 + 
 +<code php> 
 +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', 
 +]) 
 +*/ 
 +</code> 
 + 
 +Using the other bitwise flags you could also do things like... 
 + 
 +<code php> 
 +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) 
 +*/ 
 +</code> 
 + 
 +and combine them... 
 + 
 +<code php> 
 +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] 
 +*/ 
 +</code>
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-What breaks, and what is the justification for it?+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) ===== ===== Proposed PHP Version(s) =====
-List the proposed PHP versions that the feature will be included in.  Use relative versions such as "next PHP 7.x" or "next PHP 7.x.y".+PHP 8.0
  
 ===== RFC Impact ===== ===== RFC Impact =====
 ==== To SAPIs ==== ==== To SAPIs ====
-Describe the impact to CLI, Development web server, embedded PHP etc.+None.
  
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
-Will existing extensions be affected?+None.
  
 ==== To Opcache ==== ==== To Opcache ====
-It is necessary to develop RFC's with opcache in mind, since opcache is a core extension distributed with PHP.+None.
  
-Please explain how you have verified your RFC's compatibility with opcache. 
  
-==== New Constants ==== 
-Describe any new constants so they can be accurately and comprehensively explained in the PHP documentation. 
- 
-==== php.ini Defaults ==== 
-If there are any php.ini settings then list: 
-  * hardcoded default values 
-  * php.ini-development values 
-  * php.ini-production values 
- 
-===== Open Issues ===== 
-Make sure there are no open issues when the vote starts! 
- 
-===== Unaffected PHP Functionality ===== 
-List existing areas/features of PHP that will not be changed by the RFC. 
- 
-This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. 
  
 ===== Future Scope ===== ===== Future Scope =====
-This section details areas where the feature might be improved in future, but that are not currently proposed in this RFC.+None.
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
-Include these so readers know where you are heading and can discuss the proposed voting options.+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 ===== ===== Patches and Tests =====
-Links to any external patches and tests go here.+The change only requires changing two lines in ''ext/standard/var.c'' (lines 530 and 540) to replace ''array ('' and '')'' with ''['' and '']''.
  
-If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. +See https://heap.space/xref/php-src/ext/standard/var.c?r=a9398056#530-540 for reference.
- +
-Make it clear if the patch is intended to be the final patch, or is just a prototype. +
- +
-For changes affecting the core language, you should also provide a patch for the language specification.+
  
 ===== Implementation ===== ===== Implementation =====
-After the project is implemented, this section should contain  
-  - the version(s) it was merged into 
-  - a link to the git commit(s) 
-  - a link to the PHP manual entry for the feature 
-  - a link to the language specification section (if any) 
  
 ===== References ===== ===== References =====
-Links to external references, discussions or RFCs+https://heap.space/xref/php-src/ext/standard/var.c?r=a9398056#530-540 
 + 
 +https://news-web.php.net/php.internals/109415 
 + 
 +https://externals.io/message/109415#109415
  
 ===== Rejected Features ===== ===== Rejected Features =====
-Keep this updated with features that were discussed on the mail lists.+
rfc/var-export-array-syntax.1585503354.txt.gz · Last modified: 2020/03/29 17:35 by googleguy