rfc:var-export-array-syntax

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:var-export-array-syntax [2020/03/30 02:46] googleguyrfc:var-export-array-syntax [2020/04/09 22:32] googleguy
Line 12: Line 12:
  
 ===== Proposal ===== ===== Proposal =====
-Instead of <code php>array(1, 2, 3)</code> ''var_export()'' would produce <code php>[1, 2, 3]</code>+This change proposes new bit-wise optional flags for ''var_export()'' and adding a third optional argument as follows:
  
-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.+  - 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, object casting, stdclass casting). ''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. The last two options can be used without ''VAR_EXPORT_SHORT_ARRAY'' so they will have an affect on the output even without short hand array syntax. 
 + 
 + 
 +Instead of 
 + 
 +<code php> 
 +array ( 
 +  0 => 1, 
 +  1 => 2, 
 +  2 => 3, 
 +
 +</code> 
 + 
 +''var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY)'' would now produce 
 + 
 +<code php> 
 +
 +  0 => 1, 
 +  0 => 2, 
 +  3 => 3, 
 +
 +</code> 
 + 
 +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: So the following changes are also in effect:
Line 31: Line 58:
    'baz' => 'quix',    'baz' => 'quix',
 ) )
 +*/
  
-With the new change it would be+//With the new change it would be
  
 +var_export($obj, false, VAR_EXPORT_SHORT_ARRAY);
 +
 +/*
 (object) [ (object) [
    'foo' => 'bar',    'foo' => 'bar',
Line 55: Line 86:
    'bar' => 'baz',    'bar' => 'baz',
 )) ))
 +*/
  
-With the changes it would be:+ 
 +//With the changes it would be: 
 + 
 +var_export(new Foo, false, VAR_EXPORT_SHORT_ARRAY); 
 + 
 +/*
 Foo::__set_state([ Foo::__set_state([
    'bar' => 'baz',    'bar' => 'baz',
 ]) ])
 */ */
 +</code>
 +
 +Using the other bitwise flags you can also do things like...
 +
 +<code php>
 +var_export([1,2,3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_NO_INDEX | VAR_EXPORT_COMPACT);
 +
 +/*
 +  outputs:
 +  
 +  [1, 2, 3]
 +*/
 +
 +var_export([1,2,3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_COMPACT);
 +
 +/*
 +  outputs:
 +  
 +  [0 => 1, 1 => 2, 2 => 3]
 +*/
 +
 +var_export([1,2,3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_NO_INDEX);
 +
 +/*
 +  outputs:
 +  
 +  [
 +    1,
 +    2,
 +    3
 +  ]
 +*/
 +
 +
 </code> </code>
  
Line 88: Line 159:
  
 ===== Patches and Tests ===== ===== 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.+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 ===== ===== Implementation =====
Line 94: Line 167:
 ===== References ===== ===== References =====
 https://heap.space/xref/php-src/ext/standard/var.c?r=a9398056#530-540 https://heap.space/xref/php-src/ext/standard/var.c?r=a9398056#530-540
 +
 https://news-web.php.net/php.internals/109415 https://news-web.php.net/php.internals/109415
 +
 +https://externals.io/message/109415#109415
  
 ===== Rejected Features ===== ===== Rejected Features =====
  
rfc/var-export-array-syntax.txt · Last modified: 2020/04/10 09:24 by guilliamxavier