CSC/ECE 517 Summer 2008/wiki2 1 n1
While Ruby is fast growing in popularity, there are still more PHP Web applications communicating with MySQL dataases. Compare Rails with support for Web applications in PHP. Which are the advantages of each for the developer? For the finished application?
Rails vs. PHP
This document does not seek to favor one framework over another, but rather to offer developers some insight on what Ruby on Rails and PHP can offer, how they may be suitable for your next project. Note that PHP in this document refers specifically to PHP 5.
Language Comparison
As Ruby on Rails is built on Ruby, the comparison should be done between Ruby and PHP. Ruby is truely object oriented. While PHP supports OOD with notion of Class, the following codes on simple loop expression illustrates the different mindset required of developers for each language:
PHP
<?php $array = array('item1', 'item2', 'item3'); foreach ($array as $index => $item) { echo "$index. $item"; } ?>
Ruby
<% array = ['item1', 'item2', 'item3'] array.each_with_index { |item, index| p "#{index}. #{item}" } %>
Notice, the loop expression is called using an object in Ruby, whereas in PHP a construct. One can argue they accomplish the same goal, and it is true. Ultimately it is down to personal preference. Developers coming from Java or C++ will feel right at home with PHP, but may also find Ruby object oriented thinking refreshing and intuitive.
Another major difference is Ruby supports multiple inheritance. PHP does not support multiple inheritance, but supports multiple interfaces like Java. Multiple inheritance is a blessing, but those used to develop in Java should get by with multiple interfaces just fine.
Framework Comparison
Quote from the preface of PHP manual:
PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.
indicates that PHP should not be compared directly to Ruby on Rails as a framework. Instead comparison should be drawn between Ruby on Rails and various PHP frameworks, such as Zend Framework, CakePHP, etc.
Below are common features found in a well rounded web framework:
- MVC - inbuilt support for Model-View-Controller setup. Rails supports MVC by having the application structure with root folder \app, and four folders underneath, specifically \controllers, \helpers, \models, \views, and using a common convention to communicate between them.
- ORM - support for object-record mapper. For Rails, it is ActiveRecord.
- Templates - support for template engine. Rails supports templating through render :partial, and yield.
- Caching - Perhaps the most commonly used caching mechanism in Rails is caches_page method.
- Validation - built-in validation components. ActiveRecord::Validations module in Rails offers useful validation methods.
- AJAX - Rails inbuilt support for AJAX using Prototype Javascript Library is available in ActionView::Helpers::PrototypeHelper module.
There are many PHP frameworks; not every PHP framework supports every one of these features above; please refer to PHP Framework Comparison Chart. It is important to note that, Ruby on Rails gains popularity fast partially because it is a unified full stack framework for web development, and additional packages are supported through RubyGems. Thus developers have the comfort of knowing their projects on Rails are well supported. While several PHP frameworks have well-rounded set of features, it is more difficult for developers to determine which framework to use in the long run.
Advantage of PHP is that developers can opt not to use any PHP frameworks at all. In fact, PHP by itself provides a large set of core library and gives developers flexibility to use third-party modules or develop their own. For instance, interaction with database can be done directly using low level API (see PHP Database Extensions) or high level API commonly found in PHP frameworks.