by

PHP Annoys Me, Sorta

I’ve been learning (re)-learning PHP the last few weeks for fun and there’s a lot to the language and community that I’m loving. The PEAR libraries are by and large fantastic – there are hundreds of them, even on the most esoteric subjects (Image/Barcode?). However, there’s a ton about the language itself that is driving me bonkers. I’ll give a few examples. Now, before I get totally flamed, I know this is probably 80% me not knowing enough about the various language features to get around these issues, so bear that in mind.

Example 1 – Compound Statements are for the Weak

I have a class, that returns an associative array. I want to be able to access it like this:

$oldest_value =  $viz->find_oldest_newest()["oldest"];

This makes sense. $viz is an an instance of a class, find_oldest_newest is a function that returns an array, and [“oldest”] is the accessor in to that array.

Nope, sorry says PHP: Parse error: syntax error, unexpected ‘[‘. However, this works:

$tmp = $viz->find_oldest_newest();
$oldest_value = $tmp["oldest"];

Example 2 – DateTime is Crazy

Where do I begin. First of all, pretty much the entire C library exists with date(), time(), strtotime(), etc. That’s great, that’s good. They all work on standard Unix timestamps. It starts to get crazy when you throw in the DateTime. For a guy like me, you’re used to objects to deal with this kind of stuff (e.g. timezone offsetting), so I gravitated towards using the DateTime object.

First of all, the DateTime constructor doesn’t take a Unix timestamp directly as a value. You can blame ducktyping here, but this is what you have to do to get a DateTime from a Unix timestamp:

date_create("@" . $rawdate)

Second, the DateTime object has no default toString! I can’t explain how frustrating it is to always have to date_format something when I should have just been able to echo it directly. It leads me to a mess like this:

public static function format_date($rawdate, $format)
{
if(gettype($rawdate) == "integer")
return date_format(date_create("@" . $rawdate), $format);
elseif(gettype($rawdate) == "string")
return date_format(date_create("@" . strtotime($rawdate)), $format);
elseif(gettype($rawdate) == "object")
return date_format($rawdate, $format);
}

Example 3 – Types? What Types?

Go ahead, tell me what the difference should between gettype() and get_class(). Thought about it? Nothing was your answer? Exactly. Not in PHP. gettype() only works on primitives. For objects, it returns “object”. And get_class, it doesn’t really work on primitives. Now, I get it that PHP was originally not an object oriented language. But when in PHP4? PHP5? they started baking OO notions in the language, why not make it complete and make the primitive data types objects.

Example 4 – I, for one, accept our new String overlord

If you look at a couple of the examples above, half the default library functions return a string data type as their result (e.g. gettype()). When did strongly typing go out of fashion? I get ducktyping, it’s all good. I used to write a lot of VB (VARIANT, anyone?), but that isn’t an excuse to throw strongly typed constants out the windows.

What’s wrong with a static class like TypeCollection::String, TypeCollection::Integer, etc, etc. Maybe it’s there and I haven’t found it. Also riddled throughout the PHP libraries is the concept of an array as a data type. If you look at some of the date and time functions, they’ll unpack a timestamp in to an associative array whose keys are “minute” and “second”.

Okay, I’m done venting. PHP is acutally pretty cool. Like I said the PEAR libraries (Image/Graph, while still under development, is amazing) are phenomenal, and the availability of community resources is a big win for me. I just wish that some things of the language didn’t seem to, well, kludgey.