wp-cli / shell-command

Opens an interactive PHP console for running and testing PHP code.
MIT License
20 stars 16 forks source link

PHP Parse Error on namespace #30

Closed dustinpaluch closed 2 years ago

dustinpaluch commented 6 years ago
wp> namespace dustinpaluch { }
PHP Parse error:  syntax error, unexpected 'dustinpaluch' (T_STRING), expecting \\ (T_NS_SEPARATOR) in phar:///usr/local/bin/wp/vendor/wp-cli/shell-command/src/WP_CLI/REPL.php(37) : eval()'d code on line 1

I'm trying to reproduce a bug involving namespaces, wp actions, and anonymous functions, and was hoping I'd be able to use the wp shell to make a minimal test case.

schlessera commented 6 years ago

The default REPL uses PHP eval() to execute the code, and eval() does not work properly with namespaces, a limitation in PHP itself.

If you want to use namespaces in wp shell, you install the additional PsySH REPL, which provides many improvements, one of them being support for namespaces.

One easy way to do so is through the package manager:

wp package install schlessera/wp-cli-psysh
dustinpaluch commented 6 years ago

Shouldn't this fail then, from php --interactive?

php > eval('namespace dustinpaluch { function foobar() { echo __FUNCTION__; } foobar(); }');
dustinpaluch\foobar

Looks like my code doesn't work in 5.2.x, but starts working in 5.3.x.

screen shot 2018-08-29 at 10 33 07 am

screen shot 2018-08-29 at 10 33 20 am