Tuesday, April 15, 2014

A function for array to object conversion

Ironically the title of this post is way longer than the function itself, which I've just found:

public static function arrayToObject($d) {
return (object) $d;
}

Monday, April 7, 2014

"NO TOUCHES"

You know how PHP works, great, that's a thing, but please. Don't torture me having to debug this.

$$$ENTITY_NAME=$e[$i]; //no touches

Yes, this guy used 3 freaking dollar signs the valid way. (I wouldn't post how the validity came out 'cuz of space). Now I can prove, egoism also exists in coding style.

Trust me, it's random!

Remember this soon-to-be-classic picture from xkcd?


There's a story for this, too. Not so obvious of course, but still rocks. Enjoy the new findings of Project Legacy Code:

$a = random8digit();
$b = random8digit();
$c = random8digit();
$d = random8digit();

for($i=0;$i<25;++$i){
$randomhash[$i] = $a.$b.$c.$d;
}

Trust me, they are chosen by a strong random value generator algorythm. This code is part of a free ecommerce script which -turns out- also has integrated parts of PayPal API. I would accept a bank transfer of random 8 digits :)

Sunday, April 6, 2014

Remainders of an obfuscated PHP core library

Working with something that's built on an obsolete legacy core library is priceless. The functions themselves look like this


Simulating mouse button hold

... is the easiest using a simple recursive function which continuously does clicking, thought Mr. Legacy Coder, and found out, that the best jQuery implementation for this would be onclick="$(this).click();". Too bad, that this throws a Maximum call stack size exceeded error. Yea, there's always hope, that such script execution ends in a finite time. The key element of legacy code is always Magic. But this came to his mind, too - turns out from the following few lines: preventDefault() called after 3000 milliseconds timeout. #genius

Friday, April 4, 2014

PHP SPL is a mess

The standard PHP library is a mess. There's no system in function naming neither parameter order whatsoever. Two key things, which would be crucial for learning the language without having to memorize all kind of random sh*t. Even more confusing, if you - as me, few seconds ago - face an incredible composition of two functions, that seemingly produce different behaviour. They don't. They are just named this way, thanks to the great SPL. Yes, I've met html_entity_decode(htmlentities($content)) in a code that's meant to clean some ckeditor input text. Just as horrible, as it seems. The more anti-conventional PHP is, the more newbies will make these mistakes. I'm seriously waiting the moment when camels come, dollar signs disappear, and PHP turns to an object-oriented language, while it stays dynamically typed. Sweet dreams, huh? (until then, Ruby is almost the whole package).

Reading directory contents the right way

I stumbled upon another magical line that uses the same scheme for logical evaluation. Goes for sure.

 while(false !== ( $file = readdir($dir)) )

After pasting this, I'm some kind of suffering from cognitive dissonance, so let me fix it.

while( $file = readdir($dir) )

The emptiness strikes

An empty included file can mess up character encoding if it's saved not as UTF8. But why does one include a blank document?