My Experience with PHP Frameworks

June 14, 2009
The Yii Book If you like my writing on the Yii framework, you'll love "The Yii Book"!

A couple of months ago I first heard about, and quickly started using the Yii framework for PHP. It’s the third framework I’ve ever used, after Ruby on Rails and Zend (the latter is also for PHP), and so far I’m really pleased with it. In this, my first post on Yii, I’ll share my thoughts on frameworks and introduce Yii. In later posts, I’ll discuss some tips and tricks I discovered with respect to Yii specifically.In order to contextualize my thoughts on frameworks, it helps to know a bit about me and my background. First, here are what I consider to be the benefits of frameworks:

  • Rapid development
  • Theoretically more secure
  • Easier to maintain

Frameworks are great if you do a lot of Web sites or if you work in a team, where different people might work on different aspects. On the other hand, frameworks:

  • Take a while to learn and master
  • Are much harder to customize
  • Can perform more poorly

I primarily work by myself and while I’ve done a lot of Web sites in the past ten years, I don’t do a lot in any one year. What I primarily do are very custom, dynamic sites, trying to give the client (and their end-user) exactly what they want without any compromise. For these reasons, I didn’t even consider using a framework until Ruby on Rails, in 2005.

By now most people in the industry have heard of Ruby on Rails and I will say that it does live up to the hype. It’s ridiculously easy to rapidly put together fully-functioning sites. Rails is just fantastic. Yes, there may be performance issues, but I don’t think that’s truly a concern for most sites. If you’re creating a top-tier site, like an ESPN or Amazon, you probably don’t want the overhead of a framework. On the other hand, with such sites, you also have the resources to throw tons of hardware at the performance problem. The main downside of Rails—for some people—is that you really have to know Ruby in order to use it. For those out there that only know PHP, that’s an issue…

When it comes to PHP frameworks, there are lots out there, with different features and backgrounds. I’ve heard very good things about CodeIgniter, Cake, and Symphony, but I choose to learn the Zend Framework when I started looking into this. Mainly I started with Zend because it’s well documented and supported. Plus, I wanted a PHP 5-specific framework, as the object model in PHP 5 is so significantly improved from PHP 4, that I believe any framework that runs on PHP 4 is just not taking full advantage of what’s possible.

So I went through the Zend tutorials, making a couple of quick sample sites. And I read a book on the Zend Framework and used a lot of its documentation. After a month, maybe two, I had had enough. I feel like I never quite “got” the Zend Framework, perhaps because it’s too complicated, perhaps because the perspective being used in the book I read and the online documentation were too different. In any case, Zend felt like a whole lot of work: everything just took more effort than I felt it should. For just the most petty example, all of the view files goes within a views directory, then organized by controller in there. That makes sense. But in each controller subdirectory, the view files have to go within a scripts subdirectory. Sure you can change this configuration, but it just seems like an unnecessary extra step. I felt like I was spinning my wheels a lot and getting nowhere fast, so when I heard about Yii, I decided to check it out…

The Yii framework is really new, but is written for PHP 5 and developed by a person behind the popular Prado framework. Yii does not have much in the way of documentation, but what it does have makes a lot of sense to me. In the end, the fact that I was able to easily understand and use Yii sold me on it, but at first what I appreciated was:

  • It uses jQuery by default for its JavaScript (Zend uses Dojo by default, but jQuery is possible through other extensions).
  • It easily supports third-party extensions (for example, the documentation shows how you can use the Zend Framework Lucene search component in Yii).
  • ActiveRecord is a common basis for the models (this may or may not mean much to those new to frameworks but I value it).
  • It creates the code for you!

With this last point, Yii behaves much like Ruby on Rails. Using a command-line tool, you can immediately create models and CRUD—Create, Read, Update, and Delete—functionality. For example, you create a model using the command model User. Then create CRUD functionality using crud User. At that point you’ll already have a Web site where you can add, select, update, and delete a type of information called “User” (likely this would correspond to a User table in a database). The site will paginate records, validate data, provide detailed error messages, etc. From there you just need to modify templates, tweak forms, set permissions, etc. The default site template also creates basic login functionality, too.

I’m about 85% finished on my first Yii site, which is rather complex (23 database tables, three user types, multiple layouts, etc.), and am very pleased with my decision to use Yii. In fact, I quickly had to whip out CRUD capabilities for another site so that the database could be populated, and I was able to get that ready for the client in 3 hours! In subsequent posts I’ll talk about the MVC (Model-View-Controller) paradigm in some detail, as it’s the foundation for most frameworks, then get into using Yii in detail.