PHP and Abstraction
I’ve been working with PHP lately. Certain circles have really been giving this language a beating for quite some time. When the uproar started (how over dramatically phrased of me) I was quick to join them. What are some of the arguments put forth? Here’s a few off the top of my head, it might be added that these really are true statements:
- Inconsistencies in the STD library.
- STD library bloat (“This perl function does what 13 PHP functions do!”)
- Poor support for OOP.
- It was called Personal Home Page, after all (I’m not sure if I’ve heard this argument elsewhere, but this is my standard PHP joke!)
A lot of points made would hardly have been mentioned, were it not for the fact that people seem to think that the more bullets they can line up in their arguments, the worse the language is (more or less true, I suppose.)
Myself, I think PHP became an okay language when PHP5 arrived. I might add that I usually like languages I learn, so “okay” means I’ll use it, but I’ll never choose if we solely look at the language.
Look at the first bullet point I wrote. Sure, when you start out with PHP you will probably hate the fact that some function names have underscores and some don’t, that sometimes the argument order for array functions is $needle, $haystack, and sometimes $haystack, $needle. But if you have code suggest, or if you spend some time and learn PHP, will this matter? Probably not. I actually have neither, but I spend so little time on that level of abstraction that I don’t mind looking the syntax up on php.net.
With proper conventions, will the OO support still be poor? One could of course argue that it’s decent to begin with, but some things need to be fixed. So, this might be a problem, or it might not.
The point I never hear anyone argue about is the means of abstraction the language supports. The fact that the STD library is inconsistent won’t change anything except the order of arguments, or some other petty detail. After the code is written, it’s perfect. As a big fan of functional programming you might see where I am going. I constantly find myself writing methods looking like this:
function foo($arg) { foreach ($this->bar as $bar) { if ($bar->baz() == $arg) { return $bar; } } return null; }
Code duplication doesn’t get much worse than this, it’s not like I can refactor it later to remove it. There might be special cases where it can be removed, but most often not. Look at the JavaScript equivalent:
foo = function (arg) { return this.bar.select(function (bar) { return bar.baz() === arg }); };
It’s concise. Matter not whether select is implemented
already. it’s easily created by the client programmer. PHP has made
attempts to implement similar constructs, using for example
array_walk. But I find it unusable since I don’t want to
bloat the global namespace. It’s probably even worse a tool for higher
order procedures than C function pointers.
How can anyone choose to work with a language that has these kinds of limitations? Why are we not allowed to write code the way we want? It’s not a really big deal, seeing as PHP implements most means of abstraction we’ve come to expect, but it’s still pretty sad. This could of course be extended; How can anyone choose to work with JavaScript when it doesn’t even support macro expansion of expressions?
Hal Abelson suggests to ask these three questions when looking at a new language (paraphrased):
- What are the primitive elements?
- What are the means of combination?
- What are the means of abstraction?
And along that line he continues; “If the language did not come with some feature built in, how could I then build that thing? […] And then, what are the means of abstraction which allow me to use those things as elements in making more complex things?”
Also note the corresponding haskell code:
or more consicely
Post new comment