Now that grad school is over with, I’ve already felt the need to find a new challenge to tackle. As you might guess, I get bored very quickly. I’ve chosen to learn Ruby and Rails. I’ve attempted to tackle this challenge in the past but I’ve always ended up frustrated enough to quit. To celebrate the release of Rails 2.0, I figured I would try once again.
I’ve been working with Ruby and Rails 2.0 for the last two days, and I am already a convert. I’ve ported the existing development of my company’s Intranet application from PHP / CakePHP to Ruby / Rails in less than one day. That one day was the first day I worked with RoR, if that tells you anything.
There is currently a holy war surrounding PHP / CakePHP and Ruby on Rails. Having used PHP for eight years and CakePHP for one year I was basically in a mental bind. PHP is great and definitely does the job at hand, but at times it shows its hacked-together nature. One of the main differences between PHP and Ruby is that Ruby is truly an object oriented programming language while PHP is a scripting language. What does this mean? Basically, Ruby handles dealing with logical code elements in a much more refined, repeatable, and logical way.
For example, in CakePHP, an Object (say ‘User’) only lasts as long as it takes for the framework to conjure it from the database. From then on, you’re dealing with an elaborate array of specific information:
// Load User from the database
$user = $this->User->findById(121);
// Set User's last login time
$user['User']['last_login'] = date('Y-m-d g:i:s');
// Save User back to the database
$this->User->save($user);
In contrast, doing the same operation in RoR on a Ruby Object looks like this:
- Load User from the database
user = User.find 121
- Set User's last login time
user.last_login = Time.now
- Save User back to the database
user.save
Which language looks more friendly and logical to you? Exactly.
CakePHP is a godsend to the PHP community, and anyone stuck on/with PHP should be using it for every single project they work on. It’s a miracle. That being said, it is a poor clone of Rails in many ways.
Most of CakePHP’s shortcomings are due to PHP itself. PHP is not object oriented, so the CakePHP developers are coding with their hands tied from the start. Another major detraction from the framework is the severe lack of documentation. CakePHP 1.1 (the recommended stable version) does have a fair amount of documentation and an API available, but the latest and greatest version 1.2 does not have any official ‘manual’.
To be fair, most of 1.1’s documentation can be extrapolated to 1.2; however the features that make 1.2 desirable are scarcely documented. Developers close to the project post blogs that are helpful but this is far from a ideal situation. And yes, I do understand that 1.2 is in heavy development. This point is also won by RoR for its wonderful built-in documentation system that makes generating documentation effortless.
RoR is more mature feeling with a robust set of tools and documentation. Ruby itself is amazingly powerful and has a huge plugin library available. Rails has built-in support for the Prototype and script.aculo.us libraries so designing towards ‘Web 2.0’ standards is a cinch. After failing many times to do anything meaningful with Ajax in CakePHP I have already created a CRUD UI for an important Object in my Intranet application. Keep in mind, again, that I started with RoR yesterday and more specifically with Ajax this morning.
There are many more points in which RoR beats Cake/PHP; a few that spring to my mind:
1. Unit testing is built-in (no external libraries are needed)
2. Creating a new application is as easy as running ‘rails project_name’
3. Built-in web server
4. Much more robust debugging and query tracing (great for finding bottlenecks!)
5. Plugin installation is a breeze
The bottom line? If you’re stuck with PHP for you projects, use CakePHP. It’s a miracle for PHP. If you are not stuck with PHP, give Ruby on Rails a whirl. It makes coding fun again and you’ll be amazed on how quickly you’ll pick it up. I’ll be using it for all of my upcoming projects.