rfc:readable_var_representation

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:readable_var_representation [2021/01/23 20:29] tandrerfc:readable_var_representation [2021/02/19 15:19] (current) – vote declined tandre
Line 1: Line 1:
 ====== PHP RFC: var_representation() : readable alternative to var_export() ====== ====== PHP RFC: var_representation() : readable alternative to var_export() ======
-  * Version: 0.2+  * Version: 0.3
   * Date: 2021-01-22   * Date: 2021-01-22
   * Author: Tyson Andre, tandre@php.net   * Author: Tyson Andre, tandre@php.net
-  * Status: Under Discussion+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/readable_var_representation   * First Published at: http://wiki.php.net/rfc/readable_var_representation
   * Implementation: https://github.com/php/php-src/pull/6619 (currently using another name)   * Implementation: https://github.com/php/php-src/pull/6619 (currently using another name)
Line 20: Line 20:
   - Unconditionally return a string instead of printing to standard output.   - Unconditionally return a string instead of printing to standard output.
   - Use ''null'' instead of ''NULL'' - the former is recommended by more coding guidelines [[https://www.php-fig.org/psr/psr-2/|such as PSR-2]].   - Use ''null'' instead of ''NULL'' - the former is recommended by more coding guidelines [[https://www.php-fig.org/psr/psr-2/|such as PSR-2]].
 +  - Escape control characters including tabs, newlines, etc., unlike var_export()/var_dump(). See the appendix [[#comparison_of_string_encoding_with_other_languages|Comparison of string encoding with other languages]] to learn more.
   - Change the way indentation is done for arrays/objects. Always add 2 spaces for every level of arrays, never 3 in objects, and put the array start on the same line as the key for arrays and objects)   - Change the way indentation is done for arrays/objects. Always add 2 spaces for every level of arrays, never 3 in objects, and put the array start on the same line as the key for arrays and objects)
   - Render lists as ''"['item1']"'' rather than ''"array(\n  0 => 'item1',\n)"''.   - Render lists as ''"['item1']"'' rather than ''"array(\n  0 => 'item1',\n)"''.
   - Always render empty lists on a single line instead of two lines.   - Always render empty lists on a single line instead of two lines.
   - Prepend ''\'' to class names so that generated code snippets can be used in namespaces without any issues.   - Prepend ''\'' to class names so that generated code snippets can be used in namespaces without any issues.
-  - Support the bit flag ''VAR_REPRESENTATION_SINGLE_LINE=1'' in a new optional parameter ''int $flags = 0'' accepting a bitmask. If the value of $flags includes this flags, ''var_representation()'' will return a single-line representation for arrays/objects, though strings with embedded newlines will still cause newlines in the output.+  - Support the bit flag ''VAR_REPRESENTATION_SINGLE_LINE=1'' in a new optional parameter ''int $flags = 0'' accepting a bitmask. If the value of $flags includes this flags, ''var_representation()'' will return a single-line representation for arrays/objects.
  
 <code php> <code php>
Line 48: Line 49:
 php > echo var_representation([]);  // always print zero-element arrays without a newline php > echo var_representation([]);  // always print zero-element arrays without a newline
 [] []
 +// lines are indented by a multiple of 2, similar to var_export but not exactly the same
 +php > echo var_representation([(object) ['key' => (object) ['inner' => [1.0], 'other' => new ArrayObject([2])], 'other' => false]]);
 +[
 +  (object) [
 +    'key' => (object) [
 +      'inner' => [
 +        1.0,
 +      ],
 +      'other' => \ArrayObject::__set_state([
 +        2,
 +      ]),
 +    ],
 +    'other' => false,
 +  ],
 +]
 </code> </code>
  
Line 61: Line 77:
   'x' => null,   'x' => null,
 ] ]
 +
 +
 </code> </code>
  
Line 84: Line 102:
 === Encoding binary data === === Encoding binary data ===
 This does a better job at encoding binary data in a form that is easy to edit. This does a better job at encoding binary data in a form that is easy to edit.
-var_export() passes through everything except ''\\'', ''\''', and ''\0'',  +var_export() does not contain any bytes except for ''\\'', ''\''', and ''\0'',  
-even control characters such as tabs, vertical tabs, backspaces, carriage returns, etc.+not even control characters such as tabs, vertical tabs, backspaces, carriage returns, newlines, etc.
  
 <code php> <code php>
 php > echo var_representation("\x00\r\n\x00"); php > echo var_representation("\x00\r\n\x00");
 "\x00\r\n\x00" "\x00\r\n\x00"
 +// var_export gives no visual indication that there is a carriage return before that newline
 php > var_export("\x00\r\n\x00"); php > var_export("\x00\r\n\x00");
 '' . "\0" . ' '' . "\0" . '
 ' . "\0" . '' ' . "\0" . ''
 +// Attempting to print control characters to your terminal with var_export may cause unexpected side effects
 +// and unescaped control characters are unreadable
 +php > var_export(implode('', array_map('chr', range(0, 0x1f))));
 +'' . "\0" . '
 +
 +
 +hp > // (first character and closing ' was hidden by those control characters)
 +php > echo var_representation(implode('', array_map('chr', range(0, 0x1f))));
 +"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
 +
  
 // Bytes \x80 and above are passed through with no modification or encoding checks. // Bytes \x80 and above are passed through with no modification or encoding checks.
Line 185: Line 214:
   * You are generating a snippet of code to ''eval()'' in a situation where the snippet will occasionally or frequently be read by a human - see the section [[#supporting_namespaces|Supporting Namespaces]]. (If the output never needs to be read by a human, ''return unserialize(' . var_export(serialize($data), true) . ');'' can be used)   * You are generating a snippet of code to ''eval()'' in a situation where the snippet will occasionally or frequently be read by a human - see the section [[#supporting_namespaces|Supporting Namespaces]]. (If the output never needs to be read by a human, ''return unserialize(' . var_export(serialize($data), true) . ');'' can be used)
   * The output is occasionally or frequently read by humans (e.g. CLI or web app output, a REPL, unit test output, etc.).    * The output is occasionally or frequently read by humans (e.g. CLI or web app output, a REPL, unit test output, etc.). 
-  * The output contains control characters such as newlines, tabs, ''\r'' or ''\x00'' and may be viewed or edited by other users in a text editor/IDE. Many IDEs may convert from windows line endings (''\r\n'') to unix line endings(''\r\n'') automatically.+  * The output contains control characters such as newlines, tabs, ''\r'' or ''\x00'' and may be viewed or edited by other users in a text editor/IDE. Many IDEs may convert from windows line endings (''\r\n'') to unix line endings(''\n'') (or vice versa) automatically.
   * You want to unambiguously see control characters in the raw output regardless of how likely they are (e.g. dumping php ini settings, debugging mysterious test failures, etc)   * You want to unambiguously see control characters in the raw output regardless of how likely they are (e.g. dumping php ini settings, debugging mysterious test failures, etc)
   * You are writing unit tests for applications supporting PHP 8.1+ (or a var_representation polyfill) that test the exact string representation of the output (e.g. phpt tests of php-src and PECL extensions) - see the section [[#printing_to_stdout_by_default_or_configurably|Printing to stdout by default or configurably]].   * You are writing unit tests for applications supporting PHP 8.1+ (or a var_representation polyfill) that test the exact string representation of the output (e.g. phpt tests of php-src and PECL extensions) - see the section [[#printing_to_stdout_by_default_or_configurably|Printing to stdout by default or configurably]].
Line 194: Line 223:
 This flag may be useful when any of the following apply: This flag may be useful when any of the following apply:
  
-  * You are writing or modifying tests of exact variable representation and want to write the equivalent of ''assertSame("[NAN, 2]", $repr)'' instead of ''assertSame("[\n  NAN,\n  2,\n]", $repr)''+  * You are writing or modifying tests of exact variable representation and want to write the equivalent of 
 +    <code php> 
 +    $this->assertSame("[\\NS\\MyClass::__set_state(['prop' => true]), 2]", $repr) 
 +    // instead of the much longer and harder to type 
 +    $this->assertSame("[\n  \\NS\\MyClass::__set_state([\n    prop' => true,\n  ],\n  2,\n]", $repr) 
 +    </code>
   * You are generating human-readable output and expect the output to be a small object/array - see the section [[#printing_to_stdout_by_default_or_configurably|Printing to stdout by default or configurably]]   * You are generating human-readable output and expect the output to be a small object/array - see the section [[#printing_to_stdout_by_default_or_configurably|Printing to stdout by default or configurably]]
   * You want the output to be as short as possible while still being somewhat human readable, e.g. sending an extremely long array representation over the network, or are saving it to a file/cache/external service, or you're using var_representation($mixed) as an array key for thousands of objects.   * You want the output to be as short as possible while still being somewhat human readable, e.g. sending an extremely long array representation over the network, or are saving it to a file/cache/external service, or you're using var_representation($mixed) as an array key for thousands of objects.
Line 313: Line 347:
  
 Adding more flags here would increase the scope of the rfc and complexity of implementing the change and for reviewing/understanding the implementation. Adding more flags here would increase the scope of the rfc and complexity of implementing the change and for reviewing/understanding the implementation.
 +
 +=== Supporting an indent option ===
 +
 +This was left out since I felt it would increase the scope of the RFC too much.
 +
 +If  an ''indent'' option might be supported by also allowing ''var_representation(value: $value, flags: ['flags' => VAR_REPRESENTATION_SINGLE_LINE, 'indent' => "\t"])'' or by bitmask flags such as ''VAR_REPRESENTATION_INDENT_FOUR_SPACES''/''VAR_REPRESENTATION_INDENT_TABS''/''VAR_REPRESENTATION_INDENT_NONE''.
 +
 +The fact that embedded newlines are now no longer emitted as parts of strings makes it easier to efficiently convert the indentation to spaces or tabs using ''preg_replace'' or ''preg_replace_callback''
 +
 +<code php>
 +php > echo var_representation([[['key' => 'value  with  space']]]);
 +[
 +  [
 +    [
 +      'key' => 'value  with  space',
 +    ],
 +  ],
 +]
 +php > echo preg_replace('/^((  )+)/m', '\1\1', var_representation([[['key' => 'value  with  space']]]));
 +[
 +    [
 +        [
 +            'key' => 'value  with  space',
 +        ],
 +    ],
 +]
 +</code>
 +````
  
 ==== Adding magic methods such as __toRepresentation() to PHP ==== ==== Adding magic methods such as __toRepresentation() to PHP ====
Line 330: Line 392:
 It may be useful to override this string representation through additional flags, callbacks, or other mechanisms. It may be useful to override this string representation through additional flags, callbacks, or other mechanisms.
 However, I don't know if there's widespread interest in that, and this would increase the scope of this RFC. However, I don't know if there's widespread interest in that, and this would increase the scope of this RFC.
 +
 +==== Emitting code comments in result about references/types/recursion ====
 +
 +Adding a comment such as ''/* resource(2) of type (stream) */ null'' to the var_representation output with an opt-in flag (e.g. ''VAR_REPRESENTATION_ADD_TYPE_COMMENTS'') to add this information may be useful to explore in follow-up work (to meet more use cases of ''var_dump'').
 +
 +(Or ''/* RECURSION  */ NULL'', or ''[/* reference */ 123, /* reference */ 123]'', etc.)
  
 ===== Discussion ===== ===== Discussion =====
Line 402: Line 470:
 The last time ''var_export()'' changed was from ''<nowiki>stdClass::__set_state(array())</nowiki>'' to ''(object) []'' in PHP 7.3.0, but that was something that had a clearer reason to fix - ''<nowiki>stdClass::__set_state</nowiki>'' is an undeclared function and many users were inconvenienced by being unable to generate code for stdClass instances. The last time ''var_export()'' changed was from ''<nowiki>stdClass::__set_state(array())</nowiki>'' to ''(object) []'' in PHP 7.3.0, but that was something that had a clearer reason to fix - ''<nowiki>stdClass::__set_state</nowiki>'' is an undeclared function and many users were inconvenienced by being unable to generate code for stdClass instances.
  
-===== Proposed Voting Choices ===== +===== Vote ===== 
-Yes/No, requiring 2/3 majority.+This is a Yes/No vote, requiring 2/3 majority. Voting started on 2021-02-05 and ended 2021-02-19. 
 + 
 +<doodle title="Add var_representation($value, int $flags=0): string to php?" auth="tandre" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== References ===== ===== References =====
Line 423: Line 496:
 </blockquote> </blockquote>
  
-Proposed PHP ''var_representation()'':+If there are any control characters (in the ranges \x00-\x1f and \x7f), ''var_representation()'' uses double quotes instead of single quotes. 
 +If there are no control characters, strings are represented the way ''var_export()'' currently represents them.
  
 <code php> <code php>
Line 559: Line 633:
   - This may be much slower and end users may not expect that - a lot of small stream writes with dynamic C function calls would be something I'd expect to take much longer than converting to a string then writing to the stream.  (e.g. I assume a lot of small echo $str; is much faster than ''\fwrite(\STDOUT, $str);'' in the internal C implementation) (if we call ''->serialize()'' first, then there's less of a reason to expose ''->serializeFile()'' and ''->serializeStream()'')   - This may be much slower and end users may not expect that - a lot of small stream writes with dynamic C function calls would be something I'd expect to take much longer than converting to a string then writing to the stream.  (e.g. I assume a lot of small echo $str; is much faster than ''\fwrite(\STDOUT, $str);'' in the internal C implementation) (if we call ''->serialize()'' first, then there's less of a reason to expose ''->serializeFile()'' and ''->serializeStream()'')
   - Adding even more ways to dump to a stream/file. Should that include stream wrappers such as http://?  For something like XML/YAML/CSV, being able to write to a file makes sense because those are formats many other applications/languages can consume, which isn't the case for var_export.   - Adding even more ways to dump to a stream/file. Should that include stream wrappers such as http://?  For something like XML/YAML/CSV, being able to write to a file makes sense because those are formats many other applications/languages can consume, which isn't the case for var_export.
 + 
 +==== Changing var_dump ====
 +
 +var_dump is a function which I consider to have goals that are incompatible ways.
 +If an exact representation of reference cycles, identical objects, and circular object data is needed, the code snippet ''unserialize("....")'' can be generated using ''var_representation(serialize($value))'' (or var_export).
 +
 +In particular, var_dump() dumps object ids, indicates objects that are identical to each other, shows recursion, and shows the presence of references. It also redundantly annotates values with their types, and generates output for types that cannot be evaluated (e.g. ''resource(2) of type (stream)'').
 +
 +Adding a comment such as ''/* resource(2) of type (stream) */ null'' to the var_representation output with an opt-in flag to add this information may be useful to explore in follow-up work.
 +
 +https://externals.io/message/112967#112970
  
 ===== Changelog ===== ===== Changelog =====
  
   * 0.2: Add the section "When would a user use var_representation?". Add a comparison with other languages.   * 0.2: Add the section "When would a user use var_representation?". Add a comparison with other languages.
 +  * 0.3: Add more examples, add discussion section on indent
rfc/readable_var_representation.1611433789.txt.gz · Last modified: 2021/01/23 20:29 by tandre