Chris Shiflett posted some thoughts on PHP and Ruby on Rails which has generated some interesting discussion. Chris divides programming languages into two categories:
- Flexible and powerful: PHP, C, C++, and Perl.
- Structured and organized: Java, Ruby, and ASP.NET.
I don’t disagree with the cultural grouping. PHP’s cultural heritage is definitely in the Unix programming, C/Perl camp as I suggested in building a culture of objects in PHP and Why isn’t PHP the natural successor to Java?
I prefer to group languages in a different way.
Garbage Collection
To me, this is the single biggest language comparison issue. Managing memory is orthogonal to the task most programmers are trying to accomplish. Thats a fancy way of saying a waste of time. Additionally, memory management is easy to screw up. Any process that relies on humans repeatedly doing something correctly is destined for disappointment. Sure, there are tools that help avoid memory management bugs. The best of these is to take it off the programmers plate entirely with garbage collection and let the programmer concentrate on other things.
Sure, there can be resource usage and performance issues. However, for most cases computer hardware is cheaper than computer programmers.
We can divide our languages along this axis:
- Quickly runs: PHP, Java, Ruby, C#, Perl, Python, Smalltalk
- Runs quickly: C, C++, Objective C, Delphi
Interestingly, this corresponds fairly closely with native executable versus virtual machine.
Static or Dynamic Typing
The next most important axis of comparison is dynamic or static typing. In static typing type information is associated with the variable. In dynamic typing type information is associated with the value in the variable.
The benefit of dynamic typing is that you do not waste programmer time and attention with typecasting (”static types get in my way”). The benefit of static typing is that the compiler can catch certain errors (”The compiler finds my mistakes”).
- Risk tolerant: PHP, Ruby, Python, Smalltalk, Perl
- Risk averse: Java, C#
Interactive or Batch
Some might call this axis interpreted versus compiled. The issue is how long does it take to switch contexts from coding to running? Is there a lengthy build process or a deployment stage? The shorter the build process, the more productive development can be.
- Interactive: PHP, Ruby
- Batch: Java
A good example of this in the PHP world is to look at Smarty and Propel. They both generate code to execute. Smarty does it interactively, while Propel requires a build process. The build process is the primary reason I don’t use Propel.
Scope
One should not underestimate the importance of this factor. Why is Java considered so complicated? One reason is that it is designed to run on everything from servers to cell phones, from desktops to dishwashers. It’s scope is impossibly general. Of course, specialized tools such as Rails and PHP are more productive for their sweet spot applications.
- General: Java, .NET
- Specific: PHP, Rails
PHP and Ruby on Rails are two peas in a pod
Looking at these factors so far, PHP and Ruby on Rails are two peas in a pod. Lets start looking at some of the factors where Ruby and PHP begin to diverge.
Popularity
Popularity brings better tools, more available developers, better documentation, and better productivity. Programming on a discontinued or rarely used platform is more difficult.
- Popular: PHP, .NET, Java
- Rare: Python, Ruby, Smalltalk
Abstraction
Working at a higher level of abstraction allows the programmer to be more productive. The computer industry has continuously been heading up the abstraction scale without looking back. Professional programmers want to work with higher level constructs because they are more expressive. One can get more done with less code. The draw back is that higher level language features can require more programmer skill to use well.
- Simple: PHP, C, Java
- Powerful: Ruby, Smalltalk, Lisp
I’m talking about the language, not the libraries when I classify Java as simple. Dynamic versus static typing aside, I see the Java and PHP languages as similarly expressive.
I have alot of respect for Anders Hejlsberg and there is some serious innovation going on in the C# world on this front. Java can barely keep up.
The Rails folks are fond of saying that Rails couldn’t be written without the expressive features of the Ruby language. It would be interesting to get the opinions of some of the cake developers on this idea.
Library Organization
Not my favorite part of PHP.
- Function oriented: C, PHP
- Object oriented: Java, .Net, Python, Ruby
Stiffies?
I think Ruby is misclassified when placed with Java and C#. Ruby is closer to PHP than it is to either Java or C#. Rails and PHP share most of the same sources of productivity: garbage collection, dynamic typing, interactivity, and focus on web applications.
Dude you are totally wrong!
All over you are comparing .NET witch is a framework with JAVA and Rails (again a framework) with other programming languages such as PHP or Python.
Why don`t you make a new blog entry where you should compare
Rails vs. one (or all) +500 PHP frameworks vs. .NET .vs J2EE?
You have unfortunately missed to mention one of the most advanced, but sadly also the most misunderstood language known today: Javascript. Neglected for years, it’s luckily been gaining momentum lately both on the client side (due to the “ajax” fad) and the server/desktop side (thanks to projects like wxJS or Firecat).
Javascript has all the familiarity of C, flexibility of Ruby and power of Lisp. Let’s see where it would fall in your analysis:
Culture: Flexible and powerful
Garbage collection: Quickly runs
Typing: Risk tolerant
Interactivity: Interactive
Scope: Specific for the most part, but with aspirations to General
Popularity: Popular on the client-side, Rare elsewhere
Abstraction: Powerful
Library organization: Well, here’s a strange beast: while JS is purely OO language, many of its libraries are essentialy nothing more than procedural functions.
a Completion to your classification:
Strong vs. Weak Typed
weakly typed: PHP, Perl
strong typed: Python, Ruby
Advanced PHP Programming, by George Schlossnagle (Page 484) has a good explanation of the difference between strong and weakly typed languages.
Nice catch, Alex. I missed that one.
Smith, I AM being loose about comparing the language and frameworks. However you cannot really isolate the decision to use a language from the tools that are available for it. The problem is that there are not yet any popular, mature PHP frameworks to compare against. How would I define popular? When it starts showing up in job postings. You can bet that I will write some posts specifically comparing frameworks in the future.
Hi dude:
You have other small mistakes in your article.
1.- [ "Interestingly, this corresponds fairly closely with native executable versus virtual machine. "]
Efectly, languages like Java and C# run in a virtual machine, but PHP and Perl dont. PHP and Perl are interpreted languages. Olny Perl have a virtual machine named Parrot (Perl 6 or +).
2.- The section ['Static or Dynamic Typing'] is completely wrong.
Man, really you dont know about that. The differnece is in the casting form (implicit or explicit). The Interpreter or the compiler always store type information in the variables. Do you want to see a example?
if you write in PHP:
$e = 5;
var_dump($e);
you show the result: (int)5
Actually, I’m right on both counts. Internally, PHP uses a virtual machine to execute compiled byte code. Also, if you execute this statement in PHP:
var_dump(5)
you will get the same result: (int)5. PHP knows that you passed an integer to the function because the type is associated with the value, not the variable.
@Alex: How is PHP weakly typed? Each value has its pretty well-defined type… Unless Schlossnagle calls a weakly-typed language the one where type conversions are made automatically?
@smith & Jeff: Actually, it’s difficult to be very clear about that, because PHP is both a framework *and* a language. Apart from Java, Ruby and Python, which have many frameworks implemented *in themselves* and using themselves as working languages, and .NET which is implemented in whatever and uses several languages, PHP is implemented in C and uses its own syntax for working language.
I’m the author of the Pipeline framework (http://livepipe.net/) which includes a PHP5 ORM layer that has significant similarities with Rails’ ActiveRecord. I wanted to comment on the expressiveness of Ruby, and wether or not Rails can be built in PHP.
I need to start by saying that I’m only using PHP for this project, and only developed this framework because I think that Rails niche is hosted services (and will someday expand of course), but PHP still reigns king for the total number of users, and people building small to medium sized websites. Anyway, long story short, for a variety of reasons I do not think you can write Rails in PHP. Two main reasons: no closures, no class methods. Static isn’t implemented properly in PHP (in my humble opinion), which prevents things like Article::find() from working. Being able to call class methods “loose” in the class code is actually quite useful and just looks right in a way. I have to admit I was a little confused when I first saw ruby classes a few years back though.
[...] The headline here should be “Father of Java says writing web applications in PHP is simpler.” This is pretty much what I said in my Comparing PHP with other languages post: Why is Java considered so complicated? One reason is that it is designed to run on everything from servers to cell phones, from desktops to dishwashers. It’s scope is impossibly general. Of course, specialized tools such as Rails and PHP are more productive for their sweet spot applications. [...]
“Quickly runs… Runs quickly”
Most of the PHP programs on The Computer Language Shootout have been written by a novice PHP programmer.
http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&lang=php&lang2=ruby
It would be great if experienced PHP programmers contributed their expertise too show what PHP can do.
http://shootout.alioth.debian.org/gp4/faq.php#contributeprogram
best wishes, Isaac
“Strong vs. Weak Typed”
‘So what is “strong typing”? This appears to be a meaningless phrase, and people often use it in a non-sensical fashion. … If someone uses this phrase, be sure to ask them to define it for you. (For amusement, watch them squirm.)’
p263 Programming Languages: Application and Interpretation
http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/
Strong typing isn’t a meaningless phrase; it’s very well-defined. It simply means that all types are known at compile time.
I would agree with Alex and hence challenge Berislav’s comment that questions how weakly typed PHP really is; PHP is weakly typed, there’s no doubt about it.
A language like Java is statically (aka strongly) typed, because variables and their types must be explicitly declared, and attempting to store type x under var v of type y throws a type exception.
PHP on the other hand is dynamically (aka weakly) typed, since the type of a variable is not explicitly declared with the variable itself, and (by casts occurring behind the scenes, I assume) there is little limitation on what can be stored in which variable.
Try passing USD35 or 35USD to a PHP function. Pretend you are building a shopping cart.
Both are strings. Yet, PHP converts 35USD into an (int)35 while PHP converts USD35 into a (int)0. As a PHP programmer I will sit around and wondering what my types are going to be converted into, instead of knowing what my types are going to be converted into.
Write unit tests, you say? Sure – unit tests can be written. But unit tests can be written whether it is a dynamic, static, weak, or strong language. You just spend more time writing type checks in your unit tests, if you use a weakly typed language. Sure, you write unit tests that are unrelated to types – but you also write tests that ARE related to types. Writing the tests by hand is in some way more efficient? Not exactly. Writing type check unit tests by hand are actually tedious and time consuming – instead you could be spending your time writing other unit tests.
So people claim that weak/dynamic and dynamic/strong languages are faster to prototype than strong/static and static/weak languages – but in fact if you must spend more time writing unit tests for your weak/dynamic and dynamic/strong languages, you are essentially reinventing strong typing – and wasting your time.
All languages end up being strongly typed in the end. Whether you mention your types in your source code comments or whether you test for your times using unit tests, you are essentially reinventing strong typing.
Not that a compiler which strongly checks your types will solve all your errors. You still write tests for programs, whether they are of dynamic or static, weak or strong type.
And if you (in rare cases) need to pass a special type into your function which is dynamic/weak, but you are using a strongly typed language – you can use an untyped pointer or a variant. A variant is not as fast as a pointer, but a variant is more convenient. There are ways out of a type system in a strongly typed language.
You can also use an abstract class, but this is a common misconception that the only way to defeat the type system is to use an abstract class. You can use pointers or variants and never have to use an abstract class. But it’s rare that you need this functionality. It’s rare you need to escape the type system. So why make it an essential feature in a language – why not just make it an option. It’s not a feature you want on all the time, so save it for special cases.
Komodo, you seem to be using strongly typed as a synonym for statically type checked, and weakly typed as a synonym for dynamically type checked.
You’ll notice that isn’t what Alex Knaub meant by strongly typed and weakly typed. Python and Ruby are not statically type checked.
PHP sucks so much… Come on, people, PHP started as just a CGI for creating Personal Home Pages (PHP); it has evolved with many retrofits, it’s like converting a VW Beetle to compete in Formula 1; it can be done, it’s fun, but it will not win the race, or it will win it with an absurd amount of effort.
J2EE is more powerful and ASP.NET/C# is light years ahead PHP. And Ruby surely is the future.
[...] http://www.codewalkers.com - articles and sample code http://www.decodephp.com – a blog by a phpian. http://www.procata.com/blog/archives/2006/02/09/comparing-php-with-other-languages/ - Comparing php with other languages http://ilia.ws/archives/12-PHP-Optimization-Tricks.html - [...]
[...] happens that Ruby and PHP are equivalent in many of the ways that are important. See my post on comparing languages. PHP has some advantages with maturity, while ruby has some constructs, such as closures, that can [...]
[...] happens that Ruby and PHP are equivalent in many of the ways that are important. See my post on comparing languages. PHP has some advantages with maturity, while ruby has some constructs, such as closures, that can [...]
hey dude…
why don’t you compare staightly between C++ with PHP???
its a better comparison though…
and please don’t compare it too general, include some example would be better..
Take a Java example: variable types are known at the compile time. Variable types are immutable.
Type casting is allowed. Both explicit (by programmer) or implicit by the JVM (widening, autoboxing, etc).
Once the reference type is declared, it defines the scope of method accessibility as in:
Class A {
public void method1 {}
}
Class B extends A{
public void method2 {}
}
Class Test
{
public static void main (String args[])
{
A a = new B();
a.method2(); // will FAIL since method2 cannot be referenced by type A
B b = new B();
b.method2(); //allowed, method can be referenced by the type
}
}
Very good article although I have to disagree with PHP being function oriented. Since PHP5, all of my applications are heavily object oriented. Frameworks like Zend Framework (http://framework.zend.com) or dORM (http://www.getdorm.com) [disclosure: I wrote dORM] are the living proof that it is possible to write OO code with PHP.
Fully agree with you, about a week ago wrote about the same in his blog!
I like what you said here, very informational. I grew up in Memphis and your site brought back many fond memories. What is you favorite place to visit in Memphis? I cannot pick just one, there are too many to choose from. I always enjoyed visits to Graceland.
Hello friends! Everyone who reads this blog – Happy New Year!