Even I will admit this is a drawback to PHP
Jul 30th, 2008 by Gordon | 0 Comments
I'm a PHP developer, a PHP defender, and generally a PHP fanboy, but I'm actually writing this post to comment on how I'm a little disillusioned with the people calling the shots on language decisions, and why. Don't get me wrong, I still love the language (as can be noted by how I've written before in defense of it against some of the typical "PHP sucks" arguments). But I will freely admit that some of the complaints are perfectly valid points that I even agree with, and recently I noticed something amidst a discussion on PHP 6, and can only express my disappointment with the management.
One of the biggest complaints I've heard about PHP is that the order of arguments is inconsistent. There are some functions where you search for something small within something larger (for instance, searching for a substring inside a string, or an element inside an array), which is usually denoted by searching for a needle in a haystack. For instance, the strpos function does the former, while the in_array function does the latter. But the problem should become clear when you look at the prototypes for these functions:
in_array($needle, $haystack) strpos($haystack, $needle)
So it's plain to see that the order is inconsistent. The complaint is that this makes development slower because no one ever remembers what the order is for any given function, so they have to look it up. With the number of editors that offer "code hints," such as Dreamweaver or Zend Studio, this point is somewhat moot, but for those still typing in Notepad I can sympathize. I can hardly remember whether it's "haystack, needle" or "needle, haystack" for any given function.
So I was pleased to see that the head PHP developers actually addressed this issue in a meeting where they discussed changes for PHP6. But I was not pleased to see that they essentially turned a bit of a blind eye to the truth. Either that or they're just downright lazy (in regard to this issue). From the meeting notes:
...there are only two functions that have "needle, haystack" instead of "haystack, needle"... [Thus] we decided not to change the order.
False false false false false. They state that the only two functions who deviate from the "haystack, needle" standard are in_array and array_search. Um...hello? I can think of a few more...
ereg eregi ereg_replace eregi_replace preg_match preg_match_all preg_replace preg_split split spliti str_replace str_ireplace
And I don't even think that list is exhaustive, either. I only did a small amount of digging to find those; but I'm pretty sure I could keep going. Now, it is true that none of the official prototypes for those functions above actually use the terms "needle" or "haystack" in the documentation, but all of them effectively look for needles in haystacks. (I would have thought the head developers would have been aware of those, seeing as...oh, I don't know...they designed the freaking language.) The prototype for str_replace, for instance, is as follows:
str_replace($search, $replace, $subject)
The only difference here is that they chose to identify things as "search" and "subject" instead of "needle" and "haystack," respectively. But they're effectively doing the same thing. You're searching for the $search string in the $subject string, a.k.a. you're searching for a needle in a haystack (and in this case performing a replacement). And all the regular expression matching functions essentially follow the "needle, haystack" order of parameters. But for some reason none of those functions seem to count in the minds of the head PHP developers. I'm not sure if they glossed over them accidentally, or if they were being willfully ignorant on the matter.
I will admit, however, that the developers make one very valid point in the meeting notes when they say that "changing [these functions] would cause quite some problems for current applications." If they did decide to suddenly flip the order of parameters, it would indeed break a lot of existing applications, which is probably the real reason they won't do it (even though consistency is better). So in all likelihood, that's an issue that simply will not be resolved.
As I said, there are workarounds already in place--just use an editor that tells you the parameter order as you type and it's a non-issue. But I must grudingly admit that it is most definitely a flaw in the design of the language. But what bothers me more deeply is how the meeting notes seem to indicate that the head developers essentially ignored and discredited this issue without really giving it any weight, as demonstrated by their lack of research. They seem to have rather quickly come to the false conclusion that there were only two functions out of line, when in fact there were many more.
So in conclusion, even I will admit that PHP kind of sucks in regard to the "needle, haystack" issue. But to all you PHP-haters: don't confuse kindness with weakness; it's still definitely my favorite language.