CSC/ECE 517 Summer 2008/wiki2 1 n1

From Expertiza_Wiki
Jump to navigation Jump to search

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 seeks to offer developers some insight on what Ruby on Rails and PHP offer, what are the advantages of each, and 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 will be done between Ruby and PHP. Ruby is truly object oriented programming language. PHP also supports object oriented programming with Class and Object, but the following code examples of 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| puts "#{index}. #{item}" }
%>

Notice, the loop expression is called using an object in Ruby, whereas in PHP the loop is done through a construct. One can argue the two code segments accomplish the same goal; in this case it is purely personal preference. Ruby treats everything as object. Developers coming from Java or Perl 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 via mixin, but does not support abstract or interface. PHP does not support multiple inheritance, but supports multiple interfaces like Java. Multiple inheritance is a powerful feature, but those used to develop in Java should get by with multiple interfaces just fine.

Other preferential differences worth mentioning:

  1. In Ruby, semicolon is not required after statements.
  2. Ruby supports the unless statement.
  3. Ruby block can be defined with the begin and end keywords.

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 - built-in support for Model-View-Controller setup. Rails supports and enforces MVC by having the application structure with root folder \app, and four folders underneath, specifically \controllers, \helpers, \models, \views. Ruby uses common conventions to facilitate communications between them.
  • ORM - support for object-record mapper. Rails' implementation of ORM is ActiveRecord.
  • Templates - support for template engine. Rails supports templating through render :partial, and yield statements.
  • 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 support AJAX using Prototype Javascript Library, and is available in ActionView::Helpers::PrototypeHelper module.

There are over 40 PHP frameworks; not every PHP framework supports every feature above; please refer to PHP Framework Comparison Chart for comparison on popular PHP frameworks. 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. However, 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 the 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. Similar to Rails, PHP supports include() as basic templating mechanism, but developers can also use third-party template engines, such as Smarty

Conclusion

PHP and Rails have their advantages. PHP offers flexible project structures and familiar coding style. Developers can opt to use third-party extensions or a PHP framework that further simplify development. Because developers can tailor PHP to their needs, PHP is often the more scalable solution for those who take their time to fine tune low level code and the use of third party extensions. Rails relies on standards and conventions to provide uniform development and to hide complexity, which in turn allow new standards and packages to be adapted easily by Rails community. Rails increases developer productivity at cost of VM speed and visibility. It also allow more rapid prototyping than PHP in general because there is less or no time spent on setting up add-ons, but projects that do not fit its strict conventions require extra effort from developers to make work.

Reference