CSC/ECE 517 Summer 2008/wiki2 1 mf
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.
Advantages for the developer
Ruby on Rails
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
Example 2:
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.
PHP
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!
Advantages for the finished application
Ruby on Rails
- 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.
- Rails is very readable and easy to pick up. You can eyeball code and know what it does.
- Changing databases is easy.
PHP
- PHP scales much more easily
- PHP is less CPU and Memory intensive requiring less hardware to accomplish the same task
- PHP is faster
- PHP has many tools available to take care of caching and code optimization for you (Examples: Zend Optimizer, PHP Accelerator,
- PHP is easier to deploy
- It is actually rather difficult to find a hosting service that does not support PHP. While some may only have PHP4 support, most hosts support PHP5. When the time comes it is also very easy to move from you development system to a host.
- PHP hosting a very inexpensive comparatively.
- You only need FTP access, no need for SSH access.
- Rails hosting? What is that? Finally found it! It costs how much?!?!?! I can't just use FTP?!?!?!
References
- Rails
- Ruby
- PHP
- PHP Development & Production Software - PHP Tutorials Training & Certification - Learn PHP
- CakePHP: the rapid development php framework.
- PHP vs. Ruby on Rails. An evolutionary story of a Web Developer and his tools.
- PHP vs Java vs Ruby
- Digg PHP's Scalability and Performance
- Refreshingly Blue » Blog Archives » PHP vs Ruby - Practical Language Differences