Thursday, February 4, 2010

phpsh - Great tool for commandline PHP

Recently i've found great tool phpsh. It was developed by Facebook programmer Daniel <dancor> Corson. phpsh was written in Python for PHP (irony). It allow you to run PHP scripts in a shell. Yeah, you can say "Just install php5-cli and run php -a". When i have phpsh? No, thanks.

phpsh features i've been searching for:


  • php autocomplete of native and included functions and classes

  • php manual/description of native functions and classes

  • good exception handling (in php -a shell is exiting)

  • Color highlighting for output type

How to install phpsh on Ubuntu Karmic

First of all you need to have git and python installed:

sudo apt-get install git python python-setuptools


Next you need to fetch phpsh git repository on github:

git clone git://github.com/facebook/phpsh.git phpsh


Then build and install phpsh:

cd phpsh/
python2.6 setup.py build
sudo python2.6 setup.py install


During install procedure i've encountered a problem: setuptools was unable to get pysqlite, so i've installed those from Ubuntu repo:

sudo aptitude install python2.6-pysqlite2

and repeated step with install command

First look on phpsh

To see what i can do with phpsh i've used following simple code:

<?php
/**
 * Classs for testing phpsh
 * @author bumz
 *
 */
class Test
{
 /**
  * Variable holds all set foos
  * @var array
  */
 private $_foos = array();
 
 /**
  * General empty constructor
  * @return void
  */
 public function __construct()
 {
  
 }
 
 /**
  * General getter
  *
  * @param string $fooName
  * @return mixed
  * @throws Exception if foo isn't set
  */
 public function __get($fooName)
 {
  if (isset($this->_foos[$fooName])) {
   return $this->_foos[$fooName];
  } else {
   throw new Exception('This foo isn\'t set');
  }
 }
 
 /**
  * General setter
  *
  * @param string $fooName
  * @param mixed $fooValue
  * @return void
  */
 public function __set($fooName, $fooValue)
 {
  $this->_foos[$fooName] = $fooValue;
 }
 
 /**
  * General magic isset function
  *
  * @param string $fooName
  * @return bool
  */
 public function __isset($fooName)
 {
  return isset($this->_foos[$fooName]);
 }
 
 /**
  * General magic unset function
  *
  * @param string $fooName
  * @return bool
  * @throws Exception every time as foo can't be unset
  */
 public function __unset($fooName)
 {
  throw new Exception('You can\'t unset foos');
 }
 
 /**
  * Test of autocomplete
  * @return void
  */
 public function autocomplete()
 {
  echo 'autocomplete works';
 }
}


first of all you need to load phpsh itself and load Test class:

phpsh
phpsh> r '/home/bumz/workspace/test/testsh.php'


Then i've done few simple operations:

php> $class = new Test();

php> $class->foo1 = 'I\'m foo 1';

php> $class->foo2 = 'Foo 2 here';

php> echo $class->foo2;
Foo 2 here
php> echo isset($class->foo2);
1
php> unset($class->foo2);
Uncaught exception: Exception: You can't unset foos

php> $class->autocom
autocommit autocomplete
php> $class->autocomplete();


Now i know i will use this tool.

No comments:

Post a Comment