Disclaimer: PHP
I've been lurking for a while, chuckling at a lot of the code. A couple of times, I also thought about submitting a couple of things seen around. This one, however, just needed to be submitted. It's so big, so mind-boggingly WTF, that it just deserved it. It is in production and has been for six months.
public static function classExists($className)
{
$className = ucfirst($className);
if (! empty(self::$loadableClasses) && isset(self::$loadableClasses[$className]))
return true;
$paths = explode(PATH_SEPARATOR, get_include_path());
while ($path = array_shift($paths)) {
$filename = realpath($path . '/lib/' . str_replace("\\", DIRECTORY_SEPARATOR, $className) . '.php');
if (file_exists($filename)) {
self::$loadableClasses[$className] = $filename;
return true;
}
}
return false;
}
What you are seeing there is a replacement for class_exists in PHP - explained at: http://php.net/class_exists
For those who have not done any PHP recently, a long while back, SPL autoload functions were added along with a way to easily register them (spl_autoload_register). This meant that the include_path was no longer the de facto place for class files (phew!). class_exists bears this in mind by calling each autoload method, in succession, to check if the class actually does exist.
This snippet decided that SPL was essentially bullshit and that the include path was the law. It also decides to statically keep a cache of loaded classes (if a class is loaded, class_exists($className, false) does the same in one syscall).
When asked about it, the guy who coded it (lead dev) said that it is because class_exists throws warnings. When told that it doesn't, and has never, thrown warning on its own, he backed it up with this snippet:
spl_autoload_register(function($className) {
include 'lib/'.ucfirst(str_replace("\\", DIRECTORY_SEPARATOR, $className)).'.php';
});
Who knew that includes threw warnings?