CSC/ECE 517 Summer 2008/wiki2 1 mf

From Expertiza_Wiki
Revision as of 19:22, 4 July 2008 by Mdfrisch (talk | contribs)
Jump to navigation Jump to search

Rails vs PHP

While Ruby is fast growing in popularity, there are still more PHP Web applications communicating with MySQL databases. 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 is an unfair comparison

I feel that I must start off with a cliche comparision: comparing Rails to PHP is like comparing apples to oranges. With straight quotes from the front pages of their respective web sites I will demonstrate what I mean:

  • Rails - Ruby on Rails is an open source web framework...
  • Ruby - Ruby is a dynamic, open source programming language...
  • PHP - PHP is a widely-used general-purpose scripting language...

What needs to be stressed is that Rails is a framework and PHP is a language. Because of this discrepancy, many of the comparisons made in this document are not Rails to PHP comparisons, they are Ruby to PHP comparisons. Luckily this doesn't derail us too much. And now we can proceed with the topic at hand.

Ruby on Rails Advantages for the Developer

Rails is easy to develop

One common comment about Ruby on Rails is that it is a very easy language to pick up and develop in for a beginner. With the Instant Rails package a developer can go from nothing to something in a matter of minutes. Ruby also provides very nice and organized documentation for any level of developer.

The Rails framework is much more organized than base PHP

Rails is based on an Model, View, Controller (MVC) architecture. The basic organization of MVC is as follows:

  • Models handle database interaction
  • Controllers handle processing of input stimulus
  • Views provide the interface

Rapid prototyping

To create a new Rails based web application and set up the basic layout of the MVC architecture, Rails only need be given a single command.

rails <app_name>

Creating new controllers, models, and views is no more difficult than the creation of the base architecture. All of these operations can be done with a single command and Rails will create the basis for the developer to add to. This makes prototyping an idea very quick.

Many operations in Ruby and 'nicer' than in PHP

Many operations that take multiple instructions in PHP can be done with one in Ruby. Example 1: Iteration

some_array.each { |x| puts x }

Example 2:

$array_len = count($some_array); // Put the array length into another variable for speed
for( i = 0; i < count($array_len); i++ )
  print $some_array[i] . "\n";

Rails makes the switching databases very easy

Rails can be configured to work with virtually any relational database on the market today. Switching databases is as simple as changing one configuration file (database.yml) and executing one command (rake db:migrate). This can be done because Rails uses migrations. A migration allows you to specify the tables, their contents, and their relationships to be created/modified/destroyed in a database.

Ruby is the epitome of OO

    • Everything in Ruby is an object, everything.
    • Ruby was specifically built around this principle.
    • This doesn't seem too big a deal unless it is also pointed out than many PHP sites still employ PHP4 which is very outdated and its OO support is shoddy at best.

Creation of tests is much easier

Whenever you create a new Rails web application, the base testing framework is included in the layout. Whenever you add a model, view, or controller via a command an empty test is created for the component you just added. Since Rails has migrations to do its databases, the creation of a testing database is very quickly done. This way mock database objects do not have to be created and you do not need to test against a live or development database.

Rails can make you a better programmer

While using Rails does not guarantee that you will become a better programmer, it does introduce some techniques and concepts that are useful for developers to learn, to know, and to use. The testing ability of Rails is one of the best things for a developer to take away from the framework. Many developers neglect the importance of testing and many times create errors that are uncaught because of this neglect. The other most useful takeaway from Rails is the MVC architecture itself. The architecture is very good for organizing concepts logically instead of throwing code in random places to simply get a program to work.

Ruby on Rails Advantages for the Finished Application

Rails is easy to maintain

The framework helps a lot because you know exactly where to look for the functionality that you need to add to or revise. And since Rails is very readable and easy to pick up, you can eyeball code and know what it does.

PHP Advantages for the Developer

PHP has much larger community support

The single biggest argument for PHP over Rails has to be PHP's availability of code snippets, assistance, and documentation. PHP's documentation on their site is extensive, descriptive, and community supported. Though not a scientific comparison, Google Trends paints a very clean picture of usage of PHP over Rails and Ruby.

PHP can have the MVC architecture too

Because of the huge user base many people have derived their own frameworks like CakePHP and Zend Framework that provide you what you think you may miss by straying away from Rails. Rails has criticized CakePHP for mimicking the exact architecture of Rails. This is not a bad thing though. Rails has a good thing in its MVC architecture and this criticism does nothing if not reiterate how well the MVC architecture can be done with PHP.

With PHP the developer has more control

With PHP you are not stuck with the MVC architecture. There are not many arguments against the MVC architecture as it does teach good practices, but there is one very big one. MVC architecture may be very overwhelming for very small and simple sites. In the case of a simple site, MVC adds a bunch of unnecessary bloat.

PHP looks familiar for many developers

Many developers do not get their start in Ruby or PHP, they get their start in a language like C, C++, or Java because that is what is taught in school. PHP's syntax is very similar to all three of these languages whereas Ruby's is much different looking. C++ Class:

class person
{
private:
  int age;
public:
  int GetAge() { return age; }
  void SetAge(int i) { age = i; }
};

PHP Class:

class person
{
  private age;
  public int GetAge() { return this->age; }
  public void SetAge(i) { age = i; }
}

Ruby Class:

class person
  def GetAge
    @age
  end
  def SetAge(i)
    @age = i
  end
end

PHP has a enormous function set (and *nix users already know a lot of it)

PHP has an absolutely enormous function set. Many of these commands are taken directly from the *nix command set and brought in to PHP. For developers this means that a lot of functionality is already done for you, don't reinvent the wheel. For developers who are also *nix users this means that you probably already know what function you are looking for without digging for it. Examples of *nix instructions in PHP: mkdir, md5, copy, date, and the list goes on.

More customizable

You can build your software a la cart. Take a piece of this code, take a piece from that code, mix, viola! New application!

PHP Advantages for the Finished Application

PHP scales more easily

Both PHP and Rails can scale for large web applications. PHP is often said to scale better than Rails when in reality PHP just scales more easily. Commonly the scaling bottleneck is the database connections. PHP can more easily connect to multiple databases than Rails, lending to the fact that PHP application can be brought to a large scale with more ease.

PHP is faster

PHP on its own is already faster than Rails. In addition to that PHP has many tools available to take care of caching and code optimization for you like Zend Optimizer, PHP Accelerator, and many more.

PHP is much less expensive to deploy

There are a wealth of PHP hosting sites available. Combine this fact with the fact that PHP is less resource intensive and you have a very good reason as to why PHP hosting is much less expensive than Rails hosting; you can host more PHP sites on less servers and there is a higher supply of servers with PHP support. This much higher availability (high supply) leads to much competition. Any economist will tell you that when there is a lot of competition, the consumer of that resource benefits. This is the case with PHP hosting.

PHP is easier to move from development to full-time host

You use phpMyAdmin to import you database schema and upload your code via FTP. The site then just works, there is no additional setup via SSH like there is with Rails. Many hosts do not even give you SSH access to your web site without a request because it is simply unnecessary in most cases.

Conclusion

References