Grousing About PHP’s Limitations

Posted Tuesday, December 23rd, 2008 at 6:57 pm

Drat. I would like to chase back up the class hierarchy of a bunch objects, and then do something based on the second-from-the-top of that chain. (For example, when the chain goes Word > Noun > Animal > Mammal > Human, and I’ve instantiated a Human, I’d like that object to be able to tell that it’s a Noun.)

Unfortunately, I’m doing this in PHP, and it’s not possible to get a class’ parent — all you can do is find the parent of an instantiated object. So, in order to chase back up the class hierarchy, I’d have to instantiate one of each object type… and that would require coming up with parameters for each one, because these aren’t the sorts of things you can just create as anonymous items with no values.

I tried creating a static getParent() method in the top-level object, but then when I do the static call on a subclass (e.g., Human::getParent(), which should return Mammal), it apparently calls Word::getParent() anyway (and that complains that it has no parent class…)

I expect I’ll be running up against more and more of PHP’s limitations in the near future, now that I’ve gotten more accustomed to concepts like functional programming, introspection, and so on.

A quick glance at the Ruby Class documentation makes it painfully obvious how easy this would be in Ruby. While native JavaScript doesn’t support such things, Prototype.js does — down at the bottom of this page, under “Special Class Properties”, it mentions the subclasses and superclass properties, and gives handy examples.

Post a Comment

Your email is never shared. Required fields are marked *

*
*