If you use PHP on the command line you most probably know the -r parameter to execute one line of code. This feature is quite nice but it’s hard to keep the quoting right. Depending on which quotes (single or double) and shell you are using you might even need to keep attention on escaping variables. Writing the code directly to the STDIN of a php instance is quite annoying if you just want to change something after typing it. Always writing the code into a file (even so it’s just a single line + <?php) and running this file is quite circumstantial. To solve this problem Marcus and I were adding a new feature to PHP 5.1’s CLI SAPI whicht I’d like to introduce here: An interactive PHP console.
After compiling PHP using GNU readline (–with-readline) or BSD’s libedit (–with-libedit) you can invoke the console by starting PHP with the -a flag. This will give you a nice prompt:
$ php -a<br />Interactive mode enabled<br /><br />php >
Here you can just type some code and get it execcuted. Depending on the current context the prompt changes:
php > $a = 1;<br />php > $b = 2;<br />php > echo $a + $b;<br />3<br />php > function foo() {<br />php { echo "foo<br />php " bar";<br />php { }<br />php > foo();<br />foo<br />bar<br />php > <br />
But this isn’t all we have there. The über-cool feature is tab-completion. As used from bash or the mysql console you just type the first few letters and by pressing the tab key you get a completion or a list of possible completions. Currently we have completion for functions, constants, class names, variables, static method calls and class constants. Currently I’m working on completion for object methods or properties.
The whole completion stuff has some limitations, for example completion of variables or objects only works if it had been definied in a prevoius line already executed, not if a variable is used multiple times in the same line or inside a function. But in many cases it can save you typing unnecessary letters.
Feel free to test a current snapshot and to give us some feedback.
–johannes
Schreibe einen Kommentar