Friday, 11 May 2012

Yii - changing my perceptions of PHP

For a recent project we were required to write an application in PHP that communicates with RESTful web services.  This meant that the site itself did not require a database.  I had limited experience of PHP, and did not have the best impression of it as a language.

We looked into PHP frameworks and decided to use Yii.  This is primarily due to the following reasons:

* MVC design pattern.
* Testability (both functional testing and unit testing) is considered from the start (with phpunit).
* Although Yii does ORM, and magic like "generating classes from your database tables" it does not REQUIRE using a database the way some frameworks do.
* Widely used and relatively well established.

The result is a very fast application, with thin controllers and a service layer (services as Yii ApplicationComponents).  We have very high unit test coverage in the service layer, and we have a suite of functional tests.

Using Yii has resulted in a well-structured application that benefits from the framework, rather than the framework getting in the way.  When looking up how to do things "the Yii way" I always reacted with "that makes sense".

I feel this experience says something about PHP as a language.  It is generally-considered that the language is not as elegant as others, and it is a scripting language at heart.  The syntax often feels clunky and functions are named and used inconsistently.  Yii encourages you to write PHP in a way that felt like I was writing Java or Groovy.

This is similar to my experience with javascript.  Historically used just for some UI magic, and there are a lot of gotchas to the language, yet when used correctly it can result in well-written non-trivial applications.

1 comment:

Ray Stoeckicht said...

Nice article on Yii. It is great to see someone publishing quality content on the framework. We have been working on an open source CRM application that is written in PHP utilizing JQuery, Yii, and RedBeanPHP and relies heavily on test driven development.

Zurmo might be one of the most complex projects on Yii to date. Right now, we have 1000+ unit tests running across eight server configurations. We utilize selenium as well for a nice set of functional tests too. It would be incredibly helpful to get your technical feedback and recommendations so that we can improve the application. Take a look and let me know what you think: http://zurmo.org