Thu 21 Sep 2006
Up until a few weeks ago, I had used PHP only a handful of times, generally when tinkering with WordPress. That was up until I started my current job, maintaining a terrible content management “system” written in a mixture of PHP and perl.
What kicked this client from “suck” to “kill me now” was having to deal with PHP. I hate this language. So. Much. The office can tell when I’m working on this project from the stream of profanity alone.
The current version of my “Why PHP is destroying my soul little by little” list:
-
You can’t write something like
get_active_subscription($user)->subscription_type. You first have to assign the return value ofget_active_subscription()to an intermediate variable, then grab thesubscription_typeattribute from that intermediate. The same thing goes for array subscription. -
PHP’s three billion functions were named by someone on psychotropic drugs. As evidence:
countvstrlenvcount_charsvstr_word_count. Quick — what’s the difference? -
$some_object->nonexistant_attributeevaluates to the empty string. -
$misspelled_variableevaluates to the empty string. -
Pushing a value on to an array is spelled
$arr[] = $object. You’re right, that is better than a function (perl’spush @array, $object) or a method (python’slist.append(object)). -
The incredibly consistent naming scheme for functions. My favorite examples are the
array_*and*_arrayfunctions. -
My absolute favorite comes from the PHP manual, talking about why you shouldn’t use barewords to index an array, i.e.,
$foo[bar]instead of$foo["bar"]:[Using a bareword] is wrong, but it works. Then, why is it wrong? The reason is that this code has an undefined constant (bar) rather than a string. and PHP may in future define constants which, unfortunately for your code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string ‘bar’ and use that.
Brilliant.
-
You can use
returnoutside of functions without PHP complaining. -
The entire array() class. Everything about it. Absolutely everything. I’ve been using it for a month, and I still don’t feel comfortable with the thing’s semantics.
-
From the docs for array_diff(): “Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.”
Best. Equivalence test. Ever.
-
Writing
$a = $b || $cgives you a boolean, not the value of$bor$clike some more useful languages I can think of.
I’m still debating what conclusion to draw about PHP. I can’t decide whether it’s the diseased creation of a bunch of retards gone off their meds, or if it’s one of the most successful and widespread practical jokes ever conceived (”Let’s see how many people we can get to use this piece of shit!” “Dude, that’s awesome!”).
September 21st, 2006 at 16:35
1) Yes, that does suck.
2)
* count - number of elements in an array
* strlen - number of characters in a string
* count_chars - returns an array containing the number of times each character occurs in the string
* str_word_count - returns an array containing the number of unique words in a string, how often each occurs, and their numeric position
Each of these functions serves an unique purpose. Sure, you could roll them all into a single count function and then let the parser try and guess what you mean, but why bother when you can make your code more readable? If you’re going to hate on PHP, do it for a real reason (like why don’t PHP’s functions follow a standard naming convention?), not because you’re unfamiliar with it.
3) First of all, if you try an access a non-existent property on an object it throws a notice-level error which you probably didn’t notice (pun intended) because you have your error reporting set too low for development work. Secondly, it doesn’t evaluate to an empty string. What’s happening is that you’re probably echoing out the variable or assigning it to something. PHP is loosely typed, so during both of those operations PHP has to cast it into something. It chooses the empty string (since there’s nothing there). However, if you strictly check the value you’ll see it’s not really an empty string. For example:
echo ($object->doesNotExists === “”) ? “True” : “False”;
will echo “False”. Also, related to this, is calling an non-existent method.
$object->notReallyHere()
will throw a fatal error.
4) Yes, but once again that’s because the variable is being cast into an empty string. Using a non-existent variable will throw a notice level error.
5) Not sure if you’re being sarcastic about this one. Whether or not you like the convention is irrelevant. However, using the $arr[] = $foo syntax is faster (in PHP) than using the equivalent array_push($arr, $foo).
6) Yes. Like I said in #2, the crazy naming schemes are a valid reason to hate on PHP. I blame this on poor management by the project leaders. Just something you have to get used to.
7) True, that is a little crazy for being allowed inside an array, but, once again, this will throw an error in your logs alerting you to the problem.
9) array() isn’t a class. It’s a function which returns a new variable of type array. That said, there is no array class. There are array variables, but they’re not objects. Leave your preconceived notions from other languages at the door and the semantics will make more sense? What specifically is confusing you?
10) Can’t win them all.
11) You need to use the “or” operator instead. || takes a higher precedence than “or”. The same goes for && and “and”.
September 23rd, 2006 at 02:14
I feel your pain. During the summer between my two years of my masters degree (damn, that was the summer of 2004!) I worked on a PHP-driven web app for testing distributed applications. The choice of language was not mine; an undergrad was working on it and had fallen behind, so I was brought in to make some initial progress for her to build off of.
PHP just felt like the worst parts of Perl and JavaScript all rolled into one. The flat namespace is just painful. And the default values for non-existent variables (ala Lua which ruined what could have been a nice language otherwise for me) is just a recipe for bugs that can be a pain in the ass to debug.
Anyway, great post. I appreciate the humour in the face of inflicted pain.
September 25th, 2006 at 22:03
(Full disclosure: Tyler and I work together)
3, 4, others) I didn’t change my error reporting level after installing PHP. What you’re saying is that, by default, PHP chooses not to report what other languages consider to be fatal errors: referring to non-existent variables, methods, attributes, etc. AKA, by default, PHP chooses to mask disgustingly-hard-to-debug errors from the programmer. Good choice.
5) I dislike “the convention” because it makes absolutely no sense. How does it generalise that assigning to an empty subscript equals “push”. Also, the reason it’s faster than
array_push()is that the compiler can generate the necessary bytecode directly, saving the associated overhead of a function call. I don’t begrudge them the desire for speed, I just think they picked a damn stupid way of obtaining it.9) PHP has made same mistake as perl: bolting on an OO mechanism as an afterthought, without adapting the built-in language to use that same framework. I have the same gripe about PHP’s “exceptions” (which are a joke); the only thing that a
catchblock can trap are exceptions that arethrown, something that none of the language’s built-ins use. With that in mind, why in the hell would anyone use PHP’s exceptions?11) Even after we talked about precedence for an hour and diagrammed this out on the white board, you’re still wrong. The only thing separating
orand||is precedence. They both coerce the true argument to 1.Next question: why did the geniuses behind PHP decide, “you know what this language needs? Two boolean or operators!” Do they really hate parentheses that much?
Conclusion: PHP’s devs == dumbasses. I have a tough time believing these people have CS degrees, since they clearly failed their compiler design classes.