Sunday, March 30, 2014

How to overload a PHP constructor properly?

The answer is obviously this:

if(func_num_args()==0){
//do nothing
}else{
if(func_num_args()>0 && isset(func_get_args[0])){

}
}

Saturday, March 29, 2014

Query massacre

Wow! I got myself over the shock of facing another deprecated mysql_ function, but look at this construction:

for($i=0;$i<5;$i++){
$q[$i]=mysql_query("SELECT * FROM users LIMIT $i,1");
}

Uses a for loop to execute multiple queries selecting one line each instead of LIMIT 0,5. GENIUS.

Thursday, March 27, 2014

Trust me, it's magic!

You know, something's fscking wrong with the code when you find out that the longest class is actually called Magic.


Unconditonally loving WHERE

What if we glue the query string's segments together as a future condition string in a WHERE clause of an SQL statement, but there are no conditions set? Easy. Just substr($cond,0,-5) , today's legacy code tells. But what if it's followed by a space? No problem, check it five different times like this: if( isset($cond) && ($cond!='W') && $cond!=''). Check again right before building the query, if($cond=='W') $cond =''; .

The Byte Order Mark

Internal Server Error. The number one symptom of messed up .htaccess.

When you're debugging a longer .htaccess with all of its tasty regular expressions and hard-to-figure-out stuff. Minutes, hours pass, the code is completely rewritten, but does not work. Asking for the help of audience, posting it on Stackoverflow looking for possible answers. Nothing. For some reason replacing the code with one single line even thinking about RewriteEngine being corrupt. It does not work. "Fsck this sht" - deleted the file. A short while later got courage to face the problem again. Created a file. Pasted the code. Works. Wow. Curiousity takes over me, I must find the reason somehow. From the backups, the mystery is now solved. The code was made with an ancient junky editor, that added a Byte Order Mark, and Mr. Apache couldn't defeat it.

Deny From All

Given some legacy code (admin surface) doing AJAX calls. The error either could be in the file that calls AJAX and the file being called. That's for sure. Or is it? Checked both, rewritten all PHP without any magic effect. Turns out, that /controller/ folder had a crucial .htaccess with three magic words: Deny From All, which restricted access to everything in that folder (except includes and requires), including the precious script that should have been called.

How to assign a boolean value properly

I pray to this sublime line since the time I found it. Mostly not to take away my life.

($loggedin) ? $loggedin = true : $loggedin = false;

The mysterious case of custom web framework

Few weeks ago I've come across one of the most annoying debugging sessions of all time. The task was to change few things and implement a custom PHP framework having the code provided. The project should have been live on Monday, and I've added new functionality on Friday, so I had plenty of time. On Monday morning I've uploaded the final version of the improved system with some more fast changes in the code.

But it didn't work. The most annoying after all, there were no error messages. I rarely get no error messages at all, even with xdebug turned on. No messages typically indicate a missing semicolon or an unneccessary opening curly bracket. But not in this case. Since everything worked by Friday, the error must be in the few lines I just added, so I started analyzing the new stuff.

I decomposed the new functions into smaller parts, smaller methods, essential fragments, as always. Finally I said, it must be dependency problem, something with the classes these new functions are built on. Debugging all classes took a lot of time, but still found nothing. Couldn't resurrect the code. It was Tuesday, deadline yesterday, what should I do... Continued debugging the parent classes of these, got rid of many unneccessary lines, but added way too much more.

By Thursday I had investigated and rewritten everything. Still no good result. Taking deep breaths, I started to look at the code again being pretty sure that the code is right. After a while it just popped up in my mind. The server ran ancient PHP 5.3, while the framework was using the shorthand array syntax first introduced in PHP 5.4.

What's this?

As developers, we have lots of stupendous stories about the time spent debugging legacy code. This blog is designed to highlight some of the most annoying / interesting / hair-rising / hilarious solutions of the past.