views:

368

answers:

4

Are there any howtos for using the php command line interactively? I found a lot about running sripts that are in text-files, but not really about the shell with the prompt where I type in commands:

$ php -a
Interactive shell

php > echo "hello world";
hello world
php > $a = 1;
php > echo $a;
1
php > exit;
$

When I go to the linux shell and run php -a I get the php shell. Can I load classes that live in files? What are the rules here?

+2  A: 

The rules aren't any different to a normal PHP script - just think of it like reading from a very slow disk... The only real difference is that it can't read ahead, so you have to define functions before you use them.

You can use include or require as normal to load classes.

Greg
+1  A: 

I believe you can use include. You can include files relative to the location you called the command.

KahWee Teng
+1  A: 

There is another minor difference that could be problematic if you rely on the class-autoloading-behavior of PHP:

Note: Autoloading is not available if using PHP in CLI interactive mode.

Source: Using PHP from the command line and Autoloading Objects

Stefan Gehrig
+1  A: 

The interactive mode for php is somewhat limited. You may find phpsh more useful.

troelskn