Same shit, different language?
I like PHP. I also like Ruby on Rails. So, it naturally follows that I would also like Rails ported to PHP, right? Well, maybe — but I’m not sure I’ve seen a good example of this.1
Here’s a totally made-up example (in Ruby):
class RockStar < ActiveRecord::Base
has_many :roadies
has_many :groupies
end
Sure, it doesn’t do much, but most of the interesting model behavior comes automatically (that being the whole point of Rails). So, here’s a fairly literal translation into PHP:
class RockStar extends ActiveRecord::Base {
has_many('roadies');
has_many('groupies');
}
Literal as in, I didn’t bother making it valid PHP; I just replaced the basic syntax elements with the PHP equivalents.2 Ignoring the rather basic issue of namespaces, the interesting problem is that I called an inherited function in the middle of declaring the child class — PHP doesn’t allow this.
Here’s the thing; different languages are different. That doesn’t mean one has to be better than the other, it just means they’re different. The problem is most of the ports I’ve come across are losing something in translation.
The elegance of Rails3 is in the code you don’t write. There’s not a single line of CRUD up there; the base class sorts all that out for me. Sure, you could create some valid PHP out of that, but quite often that leads to Rails crammed into PHP syntax, instead of a real PHP equivalent to Rails.
Don’t get me wrong — has_many works well in Rails, but the next time I find a new `ActiveRecord` in PHP, I’d rather see some elegant PHP instead of the closest PHP someone could think of to some elegant Ruby.
February 21, 2006
at 2:15 pm