Friday, January 10, 2014

5 PHP development improvements rediscovered in 2013

Love it or hate it, the fact is 80% of the web is PHP and its usage has been in an increasing trend since 2010. Be informed that PHP is much better than you think as per Fabien Potencier. As a team, we have worked a lot on PHP this year, specifically on Symfony 2 mainly for our back-end APIs. It has really strengthen our multi-application SOA platform and enabled us to build responsive front ends on top of it.

In this process of having a robust back-end API, we have rediscovered and utilized some technologies inline with PHP development and improved on them in the past year. Here is a summary of these PHP related technologies/methods/best practices that will help all PHP developers:


PHP Specification Request - PSR

PSR (0-3) PHP Specification Request is a set of standards devised by the PHP Framework Interoperability Group (FIG) to standardize auto-loading, coding standard, coding style and logger interface. PHP-FIG members include big names like Zend, Symfony, Drupal to name some. We follow a slightly modified version of PSR-2 as our coding standards.

Writing clean code is always helpful and if you follow a widely accepted coding standard it makes it easier to open source the code. To make the process better you can use PHP code sniffer and integrate it with your IDE for code evaluation while you type.


Composer and Packagist

Composer is dependency management tool in PHP which lets you define libraries that your project is dependent on. Composer uses simple JSON declaration of dependent project in composer.json files and downloads the related libraries in the vendor folder. Getting started in easy with compser and all the opensource packages are listed in packagist, where you can search for packages you need.

Lets say if you want to work on Paypal and think someone might have submitted a wrapper library for Paypal Rest API, you can search and find the library that suits your need. You can even submit your library/package to be open source and used by others. Composer really makes working with dependency and managing version of the dependent library an easy task.


Virtual Development Environment - Vagrant

Vagrant is a virtual development environment which works as a virtual machine on your main machine with all needed software stack as config. Basically it creates a reproducible and portable development machine which is shared by all the developers, that in turn eliminates "Works on my machine" syndrome.

Another thing it greatly helps is the time to set up a machine and get running. In our case, it came down from around 2 days to install all the software stack and configure all environments to mere 5 hours including the download of all needed software. If you want your developer to start coding on the first day, this is your best bet as s/he does not need to install the correct version of PHP, MYSQL, X-Debug and all other software.


Debugging with X-Debug

Debugging in PHP used to be that elusive creature that you see sometimes and it just hides when you want to see it. On the contrary if you setup X-Debug correctly on your machine (or your vagrant) you can run the debugger and inspect how your code executes line by line. Beware that its a correct mix of X-Debug, a client on your browser like easy Xdebug for firefox and a good IDE that supports debugging like NetBeans or PHPStorm. Know well how to set it up.

In our case, X-Debug is installed on the vagrant machine so each developer does not need to manually install it. Just set up the preferred client on the browser and run the debugger on the IDE, then you can get new insights on how your code executes.

Automated Testing with PHPUnit

When your code is tested and all tests pass (they are green! yehh) you are confident that things will not break on the production environment. Writing test verify what the code is supposed do and it serves as animated documentation of the code. Not only does it help save the development time on the long run, you are already aware of the related things that break due the the changes you made when you coded a feature/fix. We use PHPUnit to test on our PHP code so that we know all our features are performing how it should.

I would not call our way of testing Test Driven Development (TDD) or core Unit Testing of each class. Rather its our own way of ensuring that the feature works as intended. We code the feature first and then write the test to verify if it meets the requirements. We generally ensure that before sending a pull request on git hub all the test pass.



All in all 2013 has been a great year with lots of things to learn and explore, we experimented with many things as we raised our technical know how to a different level in terms of the tools we use. We went from a cron that runs every minute to Rabbit MQ messaging queue that can be a different blog post :).

Now we follow continuous integration with Travis CI which runs the test we wrote with PHPUnit taking the code from Git Hub. I would like to thank our VP - Technology Alex (@_Odino_) for his vision and implementation of  above mentioned technologies. Lets hope we see more advancement in 2014.

5 comments :

  1. well written, i was not aware of vagrant. i will use it in my team.

    ReplyDelete
  2. Good Article. Composer and Vagrant are comparative new. XDebug and PHP-FIG are not new but hit news recently so we can call rediscovered but I can't understand why PHPUnit is categorized as rediscovered. It is old and commonly in use since long.

    ReplyDelete
  3. @Kapil thanks for the comment. PHP Unit is not very new but I rediscovered new ways of using it like data provider etc. Anyways its rediscovered not discovered :).

    ReplyDelete
  4. Behat + PHPSpec to do BDD in PHP is mostimportant thing in 2013.

    ReplyDelete

Comments will be moderated, so they will not appear as soon as you post them.