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
{: lang=ruby }

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’);
}
{: lang=php }

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 Rails[^3] 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.

[^1]: I’m intentionally not naming names here; but some examples I’ve seen
are much closer than others.
[^2]: I also didn’t write a more complex `has_many` relation with a less
obvious PHP equivalent, but that’s beside the point.
[^3]: …or in any system, depending on who you ask.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Comments are closed.