Friday, December 14, 2012

Make sure that your PHP code is compatible with the new version of PHP on your web server

Many times, this happens with almost every developer. You wrote code for PHP 4.2. Few years later, you decide to upgrade your Web Server. Lets say, you are installing PHP 5.3. Its a headache just thinking about how your code will perform or how many places the code will break where the functions used have been deprecated or removed from the new PHP version. Not anymore…. PHP CodeSniffer to the rescue.
Lets go ahead and do some work !!! We will need PEAR installed on the system for this to work. If you do not have PEAR, or need help installing it, google for “install php pear”.
For Linux users, you can install it from your command line:
apt-get install php-pear
Once you have pear installed, we will install CodeSniffer:
pear install PHP_CodeSniffer

We will now download a wonderful codesniffer standard by Win Godden (Credits to him for his excellent work).
git clone git:// PHPCompatibility

Now, copy the PHPCompatibility directory to  your_PEAR_install_path/PHP/CodeSniffer/Standards.

Now comes the fun part…Suppose i have a folder /var/www/project1, where all my PHP files are stored, i will run:
phpcs --standard=PHP53Compatibility /var/www/project1/

and it will show me all the files which have deprecated or removed function usage. We can now go ahead, and change the old functions to the new functions…

Hope folks enjoy it and its helpful to somebody out there. Enjoy…

No comments:

Post a Comment