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
rfc:var-export-array-syntax [2020/04/09 21:56] googleguyrfc:var-export-array-syntax [2020/04/10 09:24] (current) – typos, examples guilliamxavier
Line 12: Line 12:
  
 ===== Proposal ===== ===== Proposal =====
-This change proposes 3 new bit-wise optional flags for ''var_export()'' and adding a third optional argument as follows:+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_SHORT_ARRAY
Line 18: Line 18:
   - VAR_EXPORT_COMPACT   - VAR_EXPORT_COMPACT
  
-''VAR_EXPORT_SHORT_ARRAY'' triggers the short-hand syntax for arrays which affects all 3 cases (arrays, object castingstdclass 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.+''VAR_EXPORT_SHORT_ARRAY'' triggers the short-hand syntax for arrays which affects all 3 cases (arrays, stdClass objectsother 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).
  
-Instead of+ 
 +For example, <php>var_export([1, 2, 3]);</php> produces
  
 <code php> <code php>
Line 31: Line 33:
 </code> </code>
  
-''var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY)'' would now produce+and the new <php>var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY);</php> would produce
  
 <code php> <code php>
 [ [
   0 => 1,   0 => 1,
-  => 2, +  => 2, 
-  => 3,+  => 3,
 ] ]
 </code> </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.+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 are also in effect:+So the following changes would also be in effect:
  
 <code php> <code php>
Line 51: Line 53:
  
 var_export($obj); var_export($obj);
- 
 /* /*
-Gives us: 
 (object) array( (object) array(
    'foo' => 'bar',    'foo' => 'bar',
Line 59: Line 59:
 ) )
 */ */
- 
-//With the new change it would be 
  
 var_export($obj, false, VAR_EXPORT_SHORT_ARRAY); var_export($obj, false, VAR_EXPORT_SHORT_ARRAY);
- 
 /* /*
 (object) [ (object) [
Line 72: Line 69:
 </code> </code>
  
-The same happens for classes:+Same for other classes:
  
 <code php> <code php>
Line 80: Line 77:
  
 var_export(new Foo); var_export(new Foo);
- 
 /* /*
-Gives us: 
 Foo::__set_state(array( Foo::__set_state(array(
    'bar' => 'baz',    'bar' => 'baz',
 )) ))
 */ */
- 
- 
-//With the changes it would be: 
  
 var_export(new Foo, false, VAR_EXPORT_SHORT_ARRAY); var_export(new Foo, false, VAR_EXPORT_SHORT_ARRAY);
- 
 /* /*
 Foo::__set_state([ Foo::__set_state([
Line 100: Line 91:
 </code> </code>
  
-Using the other bitwise flags you can also do things like...+Using the other bitwise flags you could also do things like...
  
 <code php> <code php>
-var_export([1,2,3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_NO_INDEX | VAR_EXPORT_COMPACT);+var_export([1, 2, 3], false, VAR_EXPORT_NO_INDEX); 
 +/* 
 +array ( 
 +  1, 
 +  2, 
 +  3, 
 +
 +*/
  
 +var_export([1, 2, 3], false, VAR_EXPORT_COMPACT);
 /* /*
-  outputs: +array (0 => 1, 1 => 2, 2 => 3)
-   +
-  [1, 2, 3]+
 */ */
 +</code>
  
-var_export([1,2,3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_COMPACT);+and combine them...
  
 +<code php>
 +var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_NO_INDEX);
 /* /*
-  outputs: +[ 
-   +  1, 
-  [0 => 1, 1 => 2, 2 => 3]+  2, 
 +  3
 +]
 */ */
  
-var_export([1,2,3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_NO_INDEX);+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);
 /* /*
-  outputs: +array (1, 2, 3)
-   +
-  [ +
-    1, +
-    2, +
-    3 +
-  ]+
 */ */
  
 +var_export([1, 2, 3], false, VAR_EXPORT_SHORT_ARRAY | VAR_EXPORT_NO_INDEX | VAR_EXPORT_COMPACT); 
 +/* 
 +[1, 2, 3] 
 +*/
 </code> </code>
  
Line 169: Line 172:
  
 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.1586469401.txt.gz · Last modified: 2020/04/09 21:56 by googleguy