rfc:readable_var_representation
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:readable_var_representation [2021/01/23 00:08] – tandre | rfc: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.1 | + | * Version: 0.3 |
* Date: 2021-01-22 | * Date: 2021-01-22 | ||
* Author: Tyson Andre, tandre@php.net | * Author: Tyson Andre, tandre@php.net | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
* Implementation: | * Implementation: | ||
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 '' | - Use '' | ||
+ | - Escape control characters including tabs, newlines, etc., unlike var_export()/ | ||
- Change the way indentation is done for arrays/ | - Change the way indentation is done for arrays/ | ||
- Render lists as ''" | - Render lists as ''" | ||
- 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 '' | - Prepend '' | ||
- | - Support the bit flag '' | + | - Support the bit flag '' |
<code php> | <code php> | ||
Line 48: | Line 49: | ||
php > echo var_representation([]); | php > echo var_representation([]); | ||
[] | [] | ||
+ | // lines are indented by a multiple of 2, similar to var_export but not exactly the same | ||
+ | php > echo var_representation([(object) [' | ||
+ | [ | ||
+ | (object) [ | ||
+ | ' | ||
+ | ' | ||
+ | 1.0, | ||
+ | ], | ||
+ | ' | ||
+ | 2, | ||
+ | ]), | ||
+ | ], | ||
+ | ' | ||
+ | ], | ||
+ | ] | ||
</ | </ | ||
Line 61: | Line 77: | ||
' | ' | ||
] | ] | ||
+ | |||
+ | |||
</ | </ | ||
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() | + | var_export() |
- | 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(" | php > echo var_representation(" | ||
" | " | ||
+ | // var_export gives no visual indication that there is a carriage return before that newline | ||
php > var_export(" | php > var_export(" | ||
'' | '' | ||
' . " | ' . " | ||
+ | // 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('', | ||
+ | '' | ||
+ | |||
+ | |||
+ | hp > // (first character and closing ' was hidden by those control characters) | ||
+ | php > echo var_representation(implode('', | ||
+ | " | ||
+ | |||
- | // bytes above \x80 are passed through with no modification or encoding checks. | + | // Bytes \x80 and above are passed through with no modification or encoding checks. |
- | // PHP strings are internally just arrays of bytes. | + | // PHP strings are internally just arrays of bytes and |
+ | // different php applications use different encodings. | ||
+ | // E.g. for this interactive shell session in a terminal expecting output with the utf-8 encoding: | ||
php > echo var_representation(' | php > echo var_representation(' | ||
' | ' | ||
Line 157: | Line 188: | ||
</ | </ | ||
+ | ===== When would a user use var_representation? | ||
+ | |||
+ | https:// | ||
+ | |||
+ | < | ||
+ | My hesitation remains that this is just duplicating existing | ||
+ | functionality with only cosmetic differences. | ||
+ | |||
+ | As a user of PHP 8.1, how would I decide whether to use print_r, | ||
+ | var_dump, var_export, or var_representation? | ||
+ | |||
+ | And under what circumstances would I bother to write | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | An end user may wish to use these functions in the following situations: | ||
+ | (These are the personal opinions of the RFC's author) | ||
+ | |||
+ | ==== Use var_representation when: ==== | ||
+ | |||
+ | var_representation returns a parsable string representation of a variable that is easier to read than var_export. | ||
+ | |||
+ | It may be useful when any of the following apply: | ||
+ | |||
+ | * You are generating a snippet of code to '' | ||
+ | * 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, '' | ||
+ | * 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 [[# | ||
+ | * You need to copy the output into a codebase that's following a modern coding style guideline such as [[https:// | ||
+ | |||
+ | ==== Use VAR_REPRESENTATION_SINGLE_LINE when: ==== | ||
+ | |||
+ | 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 | ||
+ | <code php> | ||
+ | $this-> | ||
+ | // instead of the much longer and harder to type | ||
+ | $this-> | ||
+ | </ | ||
+ | * You are generating human-readable output and expect the output to be a small object/ | ||
+ | * 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/ | ||
+ | |||
+ | ==== Use var_export when: ==== | ||
+ | |||
+ | var_representation returns a parsable string representation of a variable (that has the limitations described in this RFC) | ||
+ | |||
+ | * You are writing an application or library that would benefit from behaving the exact same way in php 8.0 and 8.1, and polyfilling is impractical (e.g. for performance reasons or packaging requirements such as being distributed without additional dependencies (e.g. a single-file script)) | ||
+ | * You prefer string output escaped with single quotes mixed with newlines, tabs, and embedded control characters (especially if control characters are unlikely), or want to render NULL as an uppercase scalar. | ||
+ | * You need to generate output that can be '' | ||
+ | |||
+ | ==== Use var_dump when: ==== | ||
+ | |||
+ | * You do not need valid php code. | ||
+ | * You need to inspect a datastructure to determine if it contains references or recursive data structures such as recursive objects or recursive array references. | ||
+ | * You need a human-readable representation of data, don't need valid php syntax, and want to distinguish between | ||
+ | |||
+ | ==== Use debug_zval_dump when: ==== | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | * You do not need valid php code. | ||
+ | * You are looking into bugs in the internals of php (e.g. incorrect reference counting of php values leading to memory leaks or premature freeing) and need to know exact reference counts | ||
+ | * You are generating a test case for a PECL extension or php-src validating that a reference counting bug is fixed. | ||
+ | |||
+ | <code php> | ||
+ | php > $y = [new stdClass()]; | ||
+ | php > debug_zval_dump($y); | ||
+ | array(2) refcount(2){ | ||
+ | [0]=> | ||
+ | & | ||
+ | } | ||
+ | [1]=> | ||
+ | & | ||
+ | } | ||
+ | } | ||
+ | php > var_dump($y); | ||
+ | array(2) { | ||
+ | [0]=> | ||
+ | & | ||
+ | } | ||
+ | [1]=> | ||
+ | & | ||
+ | } | ||
+ | } | ||
+ | php > var_export($y); | ||
+ | array ( | ||
+ | 0 => | ||
+ | (object) array( | ||
+ | ), | ||
+ | 1 => | ||
+ | (object) array( | ||
+ | ), | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | ==== Use print_r when: ==== | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | * You do not need valid php code. | ||
+ | * You want human-readable output, string escaping isn't a concern for your use case, and differentiating between strings, integers, floats, booleans, and null doesn' | ||
+ | |||
+ | The below snippet is an example of where you may not want to use print_r(). | ||
+ | |||
+ | <code php> | ||
+ | php > print_r([[' | ||
+ | Array | ||
+ | ( | ||
+ | [0] => Array | ||
+ | ( | ||
+ | [key] => first | ||
+ | [other] => second | ||
+ | [third] => 1 | ||
+ | ) | ||
+ | |||
+ | [1] => 1 | ||
+ | [2] => 1 | ||
+ | [3] => 1 | ||
+ | [4] => 1 | ||
+ | [5] => | ||
+ | [6] => | ||
+ | [7] => | ||
+ | ) | ||
+ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 190: | Line 347: | ||
Adding more flags here would increase the scope of the rfc and complexity of implementing the change and for reviewing/ | Adding more flags here would increase the scope of the rfc and complexity of implementing the change and for reviewing/ | ||
+ | |||
+ | === Supporting an indent option === | ||
+ | |||
+ | This was left out since I felt it would increase the scope of the RFC too much. | ||
+ | |||
+ | If an '' | ||
+ | |||
+ | 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 '' | ||
+ | |||
+ | <code php> | ||
+ | php > echo var_representation([[[' | ||
+ | [ | ||
+ | [ | ||
+ | [ | ||
+ | ' | ||
+ | ], | ||
+ | ], | ||
+ | ] | ||
+ | php > echo preg_replace('/ | ||
+ | [ | ||
+ | [ | ||
+ | [ | ||
+ | ' | ||
+ | ], | ||
+ | ], | ||
+ | ] | ||
+ | </ | ||
+ | ```` | ||
==== Adding magic methods such as __toRepresentation() to PHP ==== | ==== Adding magic methods such as __toRepresentation() to PHP ==== | ||
Line 208: | Line 393: | ||
However, I don't know if there' | However, I don't know if there' | ||
- | ===== Proposed Voting Choices | + | ==== Emitting code comments in result about references/ |
- | Yes/No, requiring 2/3 majority. | + | |
+ | Adding a comment such as ''/ | ||
+ | |||
+ | (Or ''/ | ||
+ | |||
+ | ===== Discussion ===== | ||
+ | |||
+ | ==== PHP already has a lot of ways to dump variables ==== | ||
+ | |||
+ | https:// | ||
+ | |||
+ | < | ||
+ | While I agree that all the suggestions in this thread would improve | ||
+ | var_export, I worry that it is failing a "smell test" that I often apply: | ||
+ | |||
+ | "If you're struggling to come up with the appropriate name for something | ||
+ | that you're creating, maybe you're creating the wrong thing." | ||
+ | |||
+ | In this case, the reason it's difficult to name is that PHP already has | ||
+ | rather a lot of different ways to produce a human-readable string from a | ||
+ | variable. The synopses in the manual aren't particularly enlightening: | ||
+ | |||
+ | print_r — Prints human-readable information about a variable | ||
+ | var_dump — Dumps information about a variable | ||
+ | var_export — Outputs or returns a parsable string representation of a | ||
+ | variable | ||
+ | Then there' | ||
+ | once was) debug_zval_dump; | ||
+ | human-friendly like json_encode; | ||
+ | userland libraries that define their own " | ||
+ | weren' | ||
+ | |||
+ | The name of any new function in this crowded space needs to somehow tell | ||
+ | the user why they'd use this one over the others - and, indeed, when | ||
+ | they wouldn' | ||
+ | |||
+ | Should we be aiming for a single function that can take over from some | ||
+ | or all of the others, and deprecate them, rather than just adding to the | ||
+ | confusion? | ||
+ | </ | ||
+ | |||
+ | https:// | ||
+ | |||
+ | < | ||
+ | < | ||
+ | IMO print_r/ | ||
+ | readable outputs for human consumption. var_export() is about a machine | ||
+ | readable output for recreating initial state within a runtime. The | ||
+ | requirements presented are wholly different. | ||
+ | |||
+ | -Sara | ||
+ | </ | ||
+ | |||
+ | If the goal of var_export is only to have some machine-readable output, the following will do it: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | function my_var_export(mixed $x): string { | ||
+ | $serialized = \base64_encode(\serialize($x)); | ||
+ | return " | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | In reality, the output of var_export() is both machine-readable and human-readable. | ||
+ | |||
+ | —Claude | ||
+ | </ | ||
+ | |||
+ | I believe that the improvements of var_representation make adding a new function worth it. See the section [[# | ||
+ | |||
+ | As mentioned earlier, a lot of existing php code depends on the exact default output of var_export() (e.g. unit tests of php-src itself and otherwise), which was introduced in php 4.2 and predates namespaces and short arrays. | ||
+ | Changing it would result in a lot of work in php-src, PECL, and projects written in PHP to support both old and new syntaxes for var_export. | ||
+ | |||
+ | The last time '' | ||
+ | |||
+ | ===== Vote ===== | ||
+ | This is a Yes/ | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== References ===== | ===== References ===== | ||
- | Links to external references, discussions | + | |
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | |||
+ | ===== Appendix ===== | ||
+ | ==== Comparison of string encoding with other languages ==== | ||
+ | |||
+ | See https:// | ||
+ | < | ||
+ | ASCII is the American Standard Code for Information Interchange. | ||
+ | It is a 7-bit code (with 128 characters). | ||
+ | ASCII as their lower half. The international counterpart of | ||
+ | ASCII is known as ISO 646-IRV. | ||
+ | </ | ||
+ | |||
+ | If there are any control characters (in the ranges \x00-\x1f and \x7f), '' | ||
+ | If there are no control characters, strings are represented the way '' | ||
+ | |||
+ | <code php> | ||
+ | php > echo var_representation(implode('', | ||
+ | " | ||
+ | php > echo var_representation(implode('', | ||
+ | " !\"# | ||
+ | </ | ||
+ | |||
+ | Python appears to have the same inner representation with shorter representations only for '' | ||
+ | |||
+ | <code python> | ||
+ | # \x00-\x1f | ||
+ | print(repr('' | ||
+ | ' | ||
+ | # \x20-\x7f | ||
+ | print(repr('' | ||
+ | ' !"# | ||
+ | </ | ||
+ | |||
+ | |||
+ | JSON escapes a wider range of control characters, but the format does not require escaping backspaces(\x7f), | ||
+ | |||
+ | <code javascript> | ||
+ | > console.log(JSON.stringify(" | ||
+ | " | ||
+ | > console.log(JSON.stringify(" | ||
+ | " !\"# | ||
+ | </ | ||
+ | |||
+ | Ruby has additional shorter escapes for '' | ||
+ | |||
+ | <code ruby> | ||
+ | puts(" | ||
+ | " | ||
+ | puts(" !\"# | ||
+ | " !\" | ||
+ | </ | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- | ==== Printing to stdout by default/configurably ==== | + | ==== Printing to stdout by default |
Printing to stdout and creating a string representation are two distinct behaviors, which some would argue should not be combined into the same function. | Printing to stdout and creating a string representation are two distinct behaviors, which some would argue should not be combined into the same function. | ||
Line 311: | 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. | - 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. | ||
- Adding even more ways to dump to a stream/ | - Adding even more ways to dump to a stream/ | ||
+ | |||
+ | ==== 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 '' | ||
+ | |||
+ | 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. '' | ||
+ | |||
+ | Adding a comment such as ''/ | ||
+ | |||
+ | https:// | ||
+ | ===== Changelog ===== | ||
+ | * 0.2: Add the section "When would a user use var_representation?" | ||
+ | * 0.3: Add more examples, add discussion section on indent |
rfc/readable_var_representation.1611360492.txt.gz · Last modified: 2021/01/23 00:08 by tandre