This is what’s wrong with PHP

Today was a bad PHP day, a very bad PHP day. Now all of the below is known by seasoned PHP coders already, you learn to live with it, however it does make for a good anectode, rarely does PHP’s crappier aspects converge in such a profound way to completely screw you up for hours on end.

Granted I’ve been tired due to a lack of sleep today, that certainly had something to do with the hilarity of what’s happened to me, anyway, it all started with me trying to be clever with array_map and array_filter.

I knew array_filter existed and what it was all about since before, however I started working with something requiring array_map first, all well and OK, array_map looks like this: array_map(’callback’, Array). So then I assumed I could use array_filter in the same fashion, big mistake. That one works like this: array_filter(Array, ‘callback’). It took me 15 minutes or something to figure that out, that’s just a strange idiotic idiosyncrasy, to put it mildly. Hardly something I even take notice of anymore these days after roughly 5 years of PHP hacking, yes, it’s that bad.

So the class method containing both the array_map and array_filter callbacks has only one argument: $name, which contains a partial string to match against users’ full names or usernames. I wanted to use that variable inside the array_filter callback.

What was I thinking? I knew that it wouldn’t work, of course it wouldn’t since callbacks are just an ugly hack bolted on top of PHP, hell you’re calling the name of the callback in the form of a string. I’ve got no idea how the interpreter is actually evaluating this stuff and I don’t want to know, I’d rather keep what I have in my stomach where it is.

My only excuse is that I’ve been doing waaayyy too much JavaScript, ActionScript 3 and PicoLisp of late, it gets to you, all that real functionality, funny what a positive connotation the word functional has. If it’s not functional then what the hell is it? Non-functional, dysfunctional maybe? Hardly flattering epithets.

But it doesn’t end there, oh no, I was trying to get all the people with a partial in their real names or usernames, of course their real names have big caps. Yes I know you know where this is going, I started out with strpos() purely by routine, and you know the funny thing is that the manual doesn’t even mention (at least not the CHM version) that this function is case sensitive.

I probably lost over an hour just on that one and it’s happened to me at least 5 times, yes roughly one time per year, just this fucking shitty little function. Why oh why are there two of them? You have to be rainman to navigate this swamp, of course I should’ve used stripos()! What an idiot I am right? Why couldn’t my pea brain remember that, especially since it’s happened to me so many times before? I must truly be a complete moron.

So in the end I ended up with this solution:

function getFriendsByPartial($name){
  	$rarr = array();
  	foreach($this->getFriends() as $f){
  		if(stripos($f->name, $name) !== false || stripos($f->username, $name) !== false){
  			$obj 			= new stdClass();
	  		$obj->name 		= $f->name;
	  		$obj->id 		= $f->id;
	  		$obj->username 	= $f->username;
  			$rarr[] 		= $obj;
  		}
  	}
  	return $rarr;
  }

Note the absence of array_map and array_filter, why do they even exist given how hilariously stupid and underpowered they are?

What’s the take away here you might wonder maybe? Only this: When it comes to PHP, don’t try to be clever, and most importantly of all, know your functions, all 5000 of them.

Related Posts

Tags: ,