Skip to content

Instantly share code, notes, and snippets.

@macghriogair
Last active November 23, 2021 07:21
Show Gist options
  • Save macghriogair/efbce404b8e0cac6c724 to your computer and use it in GitHub Desktop.
Save macghriogair/efbce404b8e0cac6c724 to your computer and use it in GitHub Desktop.
basic autoload for php 5.2
<?php
/**
* A basic autoload implementation that should be compatible with PHP 5.2.
*
* @author pmg
*/
global $autoLoadFolders;
$srcPath = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'src';
$scannedItems = scandir($srcPath);
foreach ($scannedItems as $item) {
if ($item === '.' || $item === '..') {
continue;
}
if (is_dir($folder = $srcPath . DIRECTORY_SEPARATOR . $item)) {
$autoLoadFolders[] = $folder;
}
}
function legacyAutoload($className)
{
global $autoLoadFolders;
foreach ($autoLoadFolders as $folder) {
$classPath = $folder. DIRECTORY_SEPARATOR . $className . '.php';
if (file_exists($classPath)) {
require_once $classPath;
return true;
}
}
return false;
}
spl_autoload_register('legacyAutoload', true/*, true*/);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment