This is an old revision of the document!
Straw poll: Interest in configurable callback to dump results of expressions in `php -a`
- Version: 0.1
- Date: 2021-01-05
- Author: Tyson Andre, tandre@php.net
- Status: Draft
- First Published at: https://wiki.php.net/rfc/readline_interactive_shell_result_function_straw_poll
Introduction
Many REPLs (Read-Eval-Print Loops) for other programming languages that I'm familiar with print a (possibly truncated) representation of the result of expressions, but PHP doesn't.
It would be useful to allow users to extend the functionality of the default interactive php shell (php -a
), possibly with auto_prepend_file
or through use of command wrappers/aliases, or after loading the shell.
Prior to this RFC, there was no way to extend the interactive php shell in this way.
(I've seen https://github.com/bobthecow/psysh mentioned as an alternative for php -a
while investigating this, but that's a shell written from scratch, and doesn't have some functionality from php -a
such as tolerance of fatal errors)
Proposal
This straw poll is mentioned to decide on what form https://wiki.php.net/rfc/readline_interactive_shell_result_function should take, and if it should continue to be worked on.
Proposed default result expression dumping implementation
One of the comments was that it would make more sense if it was added at the same time as a default implementation for rendering result expressions.
For the sake of discussion, the following default implementation is proposed. var_dump() is proposed for arrays and objects because it properly represents references and cyclic data structures.
The proposed snippet would be to behave as if this snippet were evaluated before auto_prepend_file
was run and any input was entered.
readline_interactive_shell_result_function( function(string $code, $result) { if (!isset($result)) { return; } if (is_scalar($result)) { echo "=> " . var_export($result, true) . "\n"; } else { echo "=> "; var_dump($result); }});
With this default implementation, an example interactive php -a
session would have the following output.
Interactive shell php > 1+1; => 2 php > 0.5 * 2; => 1.0 php > namespace\MyClass::class; => 'MyClass' php > fn()=>true; => object(Closure)#2 (0) { } php > $x = ["foo", "bar"]; => array(2) { [0]=> string(3) "foo" [1]=> string(3) "bar" } php > asort($x); => true php > $x; => array(2) { [1]=> string(3) "bar" [0]=> string(3) "foo" } php > json_encode($x); => '{"1":"bar","0":"foo"}' php > unset($x); php > function function_returning_void() { echo "in function_returning_void()\n"; } php > function_returning_void(); in function_returning_void() php > json_decode('{"key": "value"}'); => object(stdClass)#2 (1) { ["key"]=> string(5) "value" } php > throw new RuntimeException("test"); Warning: Uncaught RuntimeException: test in php shell code:1 Stack trace: #0 {main} thrown in php shell code on line 1 php > readline_interactive_shell_result_function(null); php > 1+1; // no longer dumped php >
Proposed Voting Choices
This is a multiple choice poll, fill out any acceptable options. The proposed default uses var_export for scalars, nothing for
What tools do you currently use?