CSC/ECE 517 Summer 2008/wiki2 1 mf: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Ruby vs PHP =
= Rails vs PHP =


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?
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?  


== An unfair comparison? ==
== 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. Heard that one too many times? Well it definitely applies here. With straight quotes from the front pages of their respective web sites I will demonstrate what I mean:
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:
* [http://www.rubyonrails.org/ Rails] - ''Ruby on Rails is an open source web framework...''
* [http://www.rubyonrails.org/ Rails] - ''Ruby on Rails is an open source web framework...''
* [http://www.ruby-lang.org/en/ Ruby] - ''Ruby is a dynamic, open source programming language...''
* [http://www.ruby-lang.org/en/ Ruby] - ''Ruby is a dynamic, open source programming language...''
* [http://php.net/ PHP] - ''PHP  is a widely-used general-purpose scripting language...''
* [http://php.net/ 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. (horrible pun intended) And now we can proceed with the topic at hand.
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 Advantages for the Developer ==
=== Ruby on Rails ===
=== Rails is easy to develop ===
* 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 [http://instantrails.rubyforge.org/wiki/wiki.pl Instant Rails] package a developer can go from nothing to something in a matter of minutes. Ruby also provides very nice and organized [http://www.ruby-lang.org/en/documentation/ documentation] for any level of developer.
** Ruby has a very logical language set making Ruby on Rails is one of the easiest languages to pick up out of the blue and rapidly develop.
=== The Rails framework is much more organized than base PHP ===
** Rapid prototyping. Rails includes scripts to create basic functionality extremely fast.
Rails is based on an [http://en.wikipedia.org/wiki/Model-view-controller Model, View, Controller (MVC)] architecture. The basic organization of MVC is as follows:
* Ruby code is nice and concise
* Models handle database interaction
** Many operations that take multiple instructions in PHP can be done with one in Ruby. Best example: iteration.
* Controllers handle processing of input stimulus
* Changing databases is easy
* Views provide the interface
** Change a configuration file, run a script, done. This is much more involved with PHP.
=== Rapid prototyping ===
* The Rails framework is much more organized
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.
** Model handles database interaction, check.  
rails <app_name>
** Controller handles processing of input stimulus, check.
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.
** View provides the interface, check.
=== Many operations in Ruby and 'nicer' than in PHP ===
** It is incredibly plainly set forth how to build a Rails web application.
Many operations that take multiple instructions in PHP can be done with one in Ruby.
* 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.
** Rails comes with a testing framework there.
** If you use the scripts to generate you basic code, the basis for a test is already there.
** With the ease of creating a test database, testing database interaction is much easier than trying to use mock objects.
* Rails can make you a better programmer
** Because of the rules that Rails enforces you are almost forced to learn proper technique when developing with Rails.
** Rails makes testing easier and a good developer should test.


=== PHP ===
Example Iteration:
* Availability of code snippets, assistance, and documentation
** PHP is a much more popular and widely used language than Ruby on Rails.
** There is huge advantage of a large developer base: near limitless help and support for a problem that some else has much more than likely already experienced.
* Like MVC?
** PHP can have that too. Because of the huge user base many people have derived their own frameworks like [http://www.cakephp.org/ CakePHP] and [http://www.zend.com/en/community/framework Zend Framework] that provide you what you think you may miss by straying away from Rails.
* More control
** With PHP you are not stuck trying to make your application fit a framework. You can make you application do what you want however you want to do it.
** If you are good at what you are doing you can still have beautiful code without a strict framework trying to get you to design/develop properly.
* PHP looks familiar
** Know C, C++, or Java like many coders do? The syntax is very familiar in PHP which makes the switch very easy.
** Know *nix commands? A lot of them are functions in PHP that do what you'd think that they do.
** This makes ramp-up time in PHP very quick for an already seasoned programmer and/or *nix user.
* 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 has a wealth of built-in functions


== Advantages for the finished application ==
Ruby:
=== Ruby on Rails ===
<pre>
* Rails is easy to maintain
some_array.each { |x| puts x }
** The framework helps a lot because you know exactly where to look for the functionality that you need to add to or revise.
</pre>
** Rails is very readable and easy to pick up. You can eyeball code and know what it does.
PHP:
** Changing databases is easy.
<pre>
$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";
}
</pre>
=== 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 [http://api.rubyonrails.org/classes/ActiveRecord/Migration.html migrations]. A migration allows you to specify the tables, their contents, and their relationships to be created/modified/destroyed in a database.
=== 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.


=== PHP ===
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 scales much more easily
 
** PHP is less CPU and Memory intensive requiring less hardware to accomplish the same task
== Ruby on Rails Advantages for the Finished Application ==
* PHP is faster
=== Rails is easy to maintain ===
** PHP has many tools available to take care of caching and code optimization for you (Examples: [http://www.zend.com/en/products/guard/optimizer/ Zend Optimizer], [http://www.php-accelerator.co.uk/ PHP Accelerator],  
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 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 Advantages for the Developer ==
** PHP hosting a very inexpensive comparatively.
=== PHP has much larger community support ===
** You only need FTP access, no need for SSH access.
The single biggest argument for PHP over Rails has to be PHP's availability of code snippets, assistance, and documentation. [http://www.php.net/manual/en/ PHP's documentation] on their site is extensive, descriptive, and community supported. Though not a scientific comparison, [http://google.com/trends?q=php%2C+rails%2C+ruby&ctab=0&geo=all&date=all&sort=0 Google Trends] paints a very clean picture of usage of PHP over Rails and Ruby.
** Rails hosting? What is that? Finally found it! It costs how much?!?!?! I can't just use FTP?!?!?! :(
=== PHP can have the MVC architecture too ===
Because of the huge user base many people have derived their own frameworks like [http://www.cakephp.org/ CakePHP] and [http://www.zend.com/en/community/framework Zend Framework] that provide you what you think you may miss by straying away from Rails. [http://www.railsenvy.com/2007/8/24/rails-vs-php 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:
<pre>
class person
{
private:
  int age;
public:
  int GetAge() { return age; }
  void SetAge(int i) { age = i; }
};
</pre>
PHP Class:
<pre>
class person
{
  private age;
  public int GetAge() { return this->age; }
  public void SetAge(i) { age = i; }
}
</pre>
Ruby Class:
<pre>
class person
  def GetAge
    @age
  end
  def SetAge(i)
    @age = i
  end
end
</pre>
=== PHP has a enormous function set (and *nix users already know a lot of it) ===
PHP has an absolutely enormous [http://us2.php.net/quickref.php 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.
 
== 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 [http://www.zend.com/en/products/guard/optimizer/ Zend Optimizer], [http://www.php-accelerator.co.uk/ 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 [http://www.phpmyadmin.net/home_page/index.php 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 =
Rails is the hands-down choice between Rails and PHP if you have a very rapid development cycle because of it's ability to do rapid prototyping and development.  
 
PHP, however, will most likely save and money in the long run for most any sized company. PHP's long term benefits and community support make it the ideal choice for very small projects and large projects alike. The only caveat with PHP is that with the large selection of frameworks floating around it is harder to decide exactly which to use for your project's requirements.


= References =
= References =
* [http://www.rubyonrails.org/ Rails]
* [http://www.ruby-lang.org/en/ Ruby]
* [http://php.net/ PHP]
* [http://www.zend.com PHP Development & Production Software - PHP Tutorials Training & Certification - Learn PHP]
* [http://www.cakephp.org/ CakePHP: the rapid development php framework.]
* [http://manuals.rubyonrails.com/read/chapter/28 A Guide to Testing the Rails]
* [http://tomayko.com/writings/rails-multiple-connections Rails and Scaling with Multiple Databases]
* [http://api.rubyonrails.org/classes/ActiveRecord/Migration.html Class ActiveRecord::Migration]
* [http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=1 ONLamp.com -- Rolling with Ruby on Rails]
* [http://www.railsenvy.com/2007/8/24/rails-vs-php Rails Envy: Ruby on Rails vs. PHP]
* [http://www.oreillynet.com/ruby/blog/2007/01/php_vs_ruby_on_rails_an_evolu.html PHP vs. Ruby on Rails. An evolutionary story of a Web Developer and his tools.]
* [http://www.cmswire.com/cms/industry-news/php-vs-java-vs-ruby-000887.php PHP vs Java vs Ruby]
* [http://www.oreillynet.com/onlamp/blog/2006/04/digg_phps_scalability_and_perf.html Digg PHP's Scalability and Performance]
* [http://www.refreshinglyblue.com/2007/8/20/php-vs-ruby-practical-language-differences/ Refreshingly Blue » Blog Archives » PHP vs Ruby - Practical Language Differences]
* [http://www.refreshinglyblue.com/2007/8/20/php-vs-ruby-practical-language-differences/ Refreshingly Blue » Blog Archives » PHP vs Ruby - Practical Language Differences]

Latest revision as of 22:41, 4 July 2008

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 Iteration:

Ruby:

some_array.each { |x| puts x }

PHP:

$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.

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.

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

Rails is the hands-down choice between Rails and PHP if you have a very rapid development cycle because of it's ability to do rapid prototyping and development.

PHP, however, will most likely save and money in the long run for most any sized company. PHP's long term benefits and community support make it the ideal choice for very small projects and large projects alike. The only caveat with PHP is that with the large selection of frameworks floating around it is harder to decide exactly which to use for your project's requirements.

References