10

I've been using phpsh for a while now, and it's worked great in the past. But its namespace support still isn't very good and this can be pretty frustrating.

Things like \Somespace\Someclass::someStaticFunction() don't work without disabling the check whether or not a method exists, which leads to frequent fatal errors on typos that reset your environment.

There are multiple PHP REPLs out there, including the PHP built-in shell (php -a), which is horrible to use.

Does anyone know of an alternative or perhaps a phpsh-fork with proper namespace support? Or perhaps an easy configuration fix I've overlooked...


an example:

This testfile:

<?
namespace testing;

function echoSome(){
        echo 'Something';
}

\testing\echoSome();

produces this output in phpsh (as expected)

php> include '/path/test.php';
Something
php>

But trying the same call again does not work:

php> \testing\echoSome();
Not executing input: Possible call to undefined function echoSome()
See /etc/phpsh/config.sample to disable UndefinedFunctionCheck.

without namespaces the function is still available:

<?
function echoSome(){
        echo 'Something';
}

echoSome();

in phpsh:

php> include '/path/test.php';
Something

and the call still works:

php> echoSome();
Something
Nanne
  • 64,065
  • 16
  • 119
  • 163
Marlies
  • 927
  • 1
  • 5
  • 18
  • Could you describe in a little more detail what you find lacking in phpsh's namespace support? – Doa Jul 11 '12 at 09:16
  • I'll add a snippet of what I find confusing, but maybe @michiel can clarify some too? – Nanne Jul 13 '12 at 11:42
  • Yes, this is a perfect example of what I meant. Aside from \someNameSpace\someFunction(); someNamespace\SomeClass::someFunction() also does not work. Iirc there are also issues with anonymous functions (another 5.3 feature), but my caffeine-deprived brain can't remember right now. – Marlies Jul 13 '12 at 14:00
  • Now there is PsySH and Boris. I don't know if they do what you want, but at the time of this comment, they both have very recent new releases on github. – still_dreaming_1 Mar 13 '15 at 01:17

2 Answers2

3

I found that using eval worked as a good workaround:

php> = eval('return \testing\echoSome();')

Yea, it's a hack but a handy one. :)

Eric Anderson
  • 3,692
  • 4
  • 31
  • 34
2

There are a couple of alternatives.

First of all, you might want to try the CLI for PHP 5.4. The interactive console has been greatly improved for 5.4, allegedly. They probably agreed with you that they built-in shell was horrible to use :) All I know is that it has been "refurbished".

There are a few alternatives, such as phpa, which seems quite outdated, and running the latest git version of phpsh. They are aware of namespace issues, judging from their "Issues" page, so they might try to improve that. Since it's open source, you could get someone to fix it for you, or fix it yourself ;-)

I think, overall you are faced with the choice between either the normal PHP CLI, or phpsh. There are no alternatives that are mature enough to do what either can do, and most alternatives are even more outdated (i.e., there is php_repl, which has been updated 3 years ago, as opposed to phpsh's 2 years).

Good luck

Doa
  • 1,965
  • 4
  • 19
  • 35
  • But looking for an interactive shell for 5.3 will not end with the console from 5.4 I guess? The latest git version is quite old, I'm not sure what that would accomplish? – Nanne Jul 11 '12 at 09:03
  • I didn't notice how outdated the git version is, sorry about that. Still more recent than any alternatives besides the normal php-cli. I am afraid that you would end up with the console from 5.4, unless there is some secret fork of phpsh that I am unaware of. – Doa Jul 11 '12 at 09:10
  • 2
    Right, turns out I hadn't properly tested the git version. As it seems, it supports "\namespace\foo::bar()" just fine, it just doesn't accept "namespace\foo::bar()" which PHP itself *does* accept. That's just a minor inconvenience so it seems I dismissed the git version too quickly. I'm marking this as the answer :) – Marlies Jul 13 '12 at 19:10
  • Then I'll award the bounty :) – Nanne Jul 16 '12 at 12:48