php shell

February 16, 2007

The more I work with other languages like python and ruby I like their way how they work on problems. While PHP is very forgiving on errors, it is weak on the debugging side. It was missing a simple to use interactive shell for years. Python and Ruby have their ipython and iruby shell which give you a direct way to interact with the objects. No need to write a script and execute it afterwards.

Features

  • tab-completion (if readline() support is compiled into php)
  • handles FATAL errors (like calling a undefined function)
  • inline help
  • autoload() is enabled by default
  • works on all php platforms (shell wrappers for Unix and Windows)

Requirements

  • PHP 5 or later
  • ext/tokenizer has to be enabled (this is the default)
  • ext/pcre has to enabled (this is the default)

Installation

  • download PHP_Shell-0.3.1.tgz

  • install the package with pear install

    ## you might have to be root to install $ pear install http://jan.kneschke.de/assets/projects/php-shell/PHP_Shell-0.3.1.tgz

Usage

To check now a how a class is a behaving you first have to understand it.

Do you know the ArrayObject class which is a Class around the array() functions ?

>> $a = new ArrayObject( )
ArrayObject::__set_state(array(
))
>> $a->
__construct(
append(
count(
exchangeArray(
getArrayCopy(
getFlags(
getIterator(
getIteratorClass(
offsetExists(
offsetGet(
offsetSet(
offsetUnset(
setFlags(
setIteratorClass(
>> $a->append( 'a' )

>> $a->count( )
1
>> $a->offsetGet( 0 )
'a'

Tab-Completion

Tab-Completion works on

  • new Classname()
  • Classname::Methodname()
  • Classname::Constantname
  • $variablename
  • functionname()
  • $object->methodname()
  • $object->constantname()

To use it type the first characters and press TAB. If there is only one possible match it will complete the name, otherwise pressing TAB again will show you a list of choices.

To get a list of all declared classes you can use get_declared_classes() from the PHP core functions:

>> get_<TAB>
>> get_<TAB><TAB>
get_browser(
...
get_declared_classes(
...
get_resource_type(
>> get_dec<TAB>
>> get_declared_<TAB><TAB>
get_declared_classes(
get_declared_interfaces(
get_defined_constants(
get_defined_functions(
get_defined_vars(
>> get_declared_classes( )

Discovery

The shell supports a shortcut to display DocComments for Classes, Methods and functions:

>> get_declared_classes()
array (
  0 => 'stdClass',
  1 => 'Exception',
  2 => 'ErrorException',
  ...
  89 => 'PHP_Shell',
)

>> ? PHP_Shell
'/**
* PHP_Shell
*
* a interactive PHP Shell with tab-completion and history
* it can catch FATAL errors before executing the code
*
...
*/'

>> PHP_Shell::<tab><tab>
PHP_Shell::__construct(
PHP_Shell::appendCode(
PHP_Shell::getCode(
PHP_Shell::getHelp(
PHP_Shell::getVerbose(
PHP_Shell::getVersion(
PHP_Shell::hasReadline(
PHP_Shell::input(
PHP_Shell::parse(
PHP_Shell::readline(
PHP_Shell::resetCode(

>> ? PHP_Shell::parse()
'/**
    * parse the PHP code
    *
    * we parse before we eval() the code to
    * - fetch fatal errors before they come up
    * - know about where we have to wait for closing braces
    *
    */'
>>

Changes

2006-05-29 - Version 0.2.7
  * added :set autoload and :set background
  * added ? $obj->method()
  * added help for internal PHP functions like ? fopen()
  * moved shell-wrapper to <a href="php-shell-cmd.php.html">php-shell-cmd.php</a>
  * removed die()
  * removed use of SHELL and SHELL_COLOUR
  * added <a href="PHP_Prototypes.php.html">PHP_Prototypes.php</a> to extract
    the prototypes from the PHP-5.x source-code
2006-05-15 - Version 0.2.5
  * use PHPDoc instead of doxygen for the documentation
  * moved shell-commands like 'quit' and '?' into cmdHelp() and cmdQuit()
  * added registerCommand() to add user-supplied Commands
  * handle fopen("stdin")-failure in input() instead of a die() in the __construct()
  * <a href="http://pear.php.net/pepr/pepr-proposal-show.php?id=397">PEAR PEPr</a>
2006-05-15 - Version 0.2.3
  * disabled COLOURs by default
  * use PHP/Shell.php instead of PHP_Shell.php to follow PEARs naming conventions
2006-05-15 - Version 0.2.2
  * added handling of dynamic method calls as in $object->$method() or Class::$method()
2006-05-15 - Version 0.2.1
  * migrated the PEAR package.xml 2.0
  * added a 'php-shell.sh' for Unix systems and 'php-shell.bat' for Windows
  * disabled max-execution-time
2006-05-14 - Version 0.2.0
  * built a PEAR package
  * added PHPUnit testcases
2006-05-13
  * added more known PHP-tokens
  * added support for object[...]->method()
  * moved shell to the class-file
2006-05-08
  * added readline support
  * added tab-completion
2006-05-05
  * first release

Usage

Since PHP_Shell 0.2.1 a shell script is added to start the shell

$ php-shell.sh

Documentation

Alternatives

the php-shell is not the first attempt to give PHP a interactive shell:

php php-shell

Comments

Enable javascript to load comments.