This question of PHP vs Rails comes up more than enough, and it
therefore
needs to be seriously addressed.
I used to work for a company specializing in PHP web development.
Towards
the end of my employment I was advocating the development of a PHP
framework
based off of Rails and Cake (the open source PHP framework inspired by
Rails). I spend a little over 2 months putting together a prototype of
a
PHP like Rails. The endeavor has illuminated many fine points of this
distinction. In an attempt to answer the question of “Why would anyone
prefer PHP over Rails or visa versa?”, I present the following with an
emphasis on the implementation.
- Ruby over PHP
a) the PHP parser is too inflexible.
- The biggest caveat of PHP is that methods cannot be added on the fly
to
classes without compiling in a special library called runkit, located at
http://php.net/manual/en/ref.runkit.php. In other words: classes are
closed
upon definition. If one wants to create an active_record like ORM then
one
is determined to include runkit when compiling the php parser or write
class
definition files to the hard drive on the fly.
b) PHP references are kludged in PHP4. It is common to pass by
reference in
C and C++ in order to save memory and utilize return value optimization.
In
my implementation of a PHP version of Rails I ended up splitting an
“ActiveRecord” into two classes: a Factory and an Object class. Each
object
instance would have as a data-member a reference to its Factory in an
effort
to not duplicate the sql_generation methods. The Factory instance would
also contain all of the instances of its objects, hoping that each
object
would simply be a reference into the Factories container. As a bug,
when
the objects filled up memory so did the reference count… I belive
this is
fixed in PHP5.
c) is possibly two counts: an overbearing responsibility upon global
methods
in PHP, and the fact that PHP’s object.c class is designed to do
everything. Addressing the later first, I acknowledge that both PHP and
Ruby have base object classes. Their distinction is that, in Ruby the
object class is designed to provide methods to all inherited objects,
whereas in PHP the object class is designed to also contain data: for
example, there is a minimal distinction between PHP’s array data
structures
and PHP’s object data structures; the two are almost equivocal and it is
therefore tiresome to distinguish (in code) between objects, arrays and
hashes (what PHPers call associative arrays). As a result there is a
lot of
type checking in PHP. I suspect that this equivocation is the main
reason
for the plethora of global methods.
d) finally, I find that the simplicity of Ruby’s to_s style methods put
the
language above and beyond both Java and PHP. However, I fear that I am
not
expert enough to defend this adequately.
It may not be hidden that I loath PHP, and mainly because of the
presence of
(c). However I also feel that PHP has its place. Let me elaborate.
- PHP over Ruby
a) PHP’s learning curve is close to nothing. Switching mentalities
between
Java, C and C++ to PHP is admirably easy. Most CS graduate students
only
have experience in the three canons of development languages listed
above.
The better education includes experience in Scheme, or Lisp, for the
shear
breadth of syntactic understanding. Most of us do acknowledge the
importance of pseudo code and see that all implementations are just
divergent from that initial “pseudo” logic. However, as a precondition,
some development environments cannot cater to a heightened sense of
programesque. In other words, there is a need for non-CS people to
write
web based programs and PHP satisfies that need.
I do, in an elitist fashion, consent that Ruby is the superior language
over
PHP. However I also realize that PHP satisfies a niche that is
difficult
for Ruby to semantically attain (however it is certainly possible to
attain
a php (read global) like semantic).
- A little bit about Rails
Insofar as I can tell, PHP has another upper hand over Rails/Ruby, and
that
is the support PHP has within Windows Servers. One of the major
setbacks I
encountered in a PHP version of Rails was the esteem of the
dispatch.fcgi.
Windows servers are mainly processed based (as opposed to thread based),
and
therefore prefer to spawn another parser for each request rather than
handle
an error in a thread. PHP has done an awesome job making a fastcgi port
of
their parser. PHP’s fastcgi is oriented towards ever .php file rather
than
just one dispatcher. I would love to see Ruby duplicate this effort in
their own fcgi such that it can run different dispatch.fcgi’s.
Such a framework as Rails is possible in PHP, however because of the
implementation restrictions of PHP, developing a “Rails” for it should
be
tiresome, if not mind wracking. However, I do not think there should be
a
Rails equivalent developed for PHP solely because the language fits a
different niche; if I were to describe it, I would say that it is the
quick,
easy and dirty.
Regards,
Travis Michel.