OK, I figured it out. My issue was indeed not setting up the path correctly; using the __DIR__
constant was the ::ahem:: path to success. Here's the working code I'm now using:
<?php
spl_autoload_register('my_autoloader');
function my_autoloader($class) {
$classMap = array(
'classes/',
'classes/sites/',
'classes/data/'
);
foreach ($classMap as $location) {
if (file_exists(__DIR__ . '/' . $location . $class . '.php')) {
require_once(__DIR__ . '/' . $location . $class . '.php');
break;
}
}
}
?>
It turns out __DIR__
returns the directory location of the actual file in which it is called (as opposed to, say, a file that is including it), which means this will work so long as this file remains in the root of the directory with my /classes directory. And, it exists exclusively for defining these kinds of settings...
Hope this helps someone else down the line! And, of course, if anyone can shed light on why this solution is sub-optimal, I'm all eyes...
EDIT: One optimization I will perform will be to convert the $classMap array() to an associative array (e.g,. $classMap = array('root' => 'classes/', 'sites' => 'classes/sites/');
so I can do a lookup rather than loop through all the directories I create over time. But, I suppose that's for another thread.
EDIT2: If anyone's interested, here's the solution I came up with for doing this as an associative array. Basically, I set up an associative array in $GLOBALS and use that to store a map of Classes -> Packages. Then, in the autoloader, I map Packages -> Locations and then call in the file necessary for the instantiated class.
This is the global config file:
<?php
// INITIALIZE GLOBAL Class -> Package map
if (!array_key_exists('packageMap',$GLOBALS)) {
$GLOBALS['packageMap'] = array();
}
spl_autoload_register('my_autoloader');
function my_autoloader($class) {
$classMap = array(
'root'=>'/classes/',
'sites'=>'/classes/sites/',
'data'=>'/classes/data/'
);
// RESOLVE MAPPINGS TO AN ACTUAL LOCATION
$classPackage = $GLOBALS['packageMap'][$class];
$location = $classMap[$classPackage];
if (file_exists(__DIR__ . $location . $class . '.php')) {
require_once(__DIR__ . $location . $class . '.php');
}
}
?>
Here's a specific site config file that uses the autoloader:
<?php
// LOAD GLOBAL CONFIG FILE
require_once('../config/init.php');
// CREATE LOCAL REFERENCE TO GLOBAL PACKAGE MAP (MOSTLY FOR READABILITY)
$packageMap = $GLOBALS['packageMap'];
// ADD Class -> Package MAPPINGS
$packageMap['DAO'] = 'data';
$packageMap['SomeSite'] = 'sites';
$packageMap['PDOQuery'] = 'data';
// INSTANTIATE SOMETHING IN ONE OF THE PACKAGES
$someSite = new SomeSite();
?>
Hopefully this is useful to others...if it raises any red flags for anyone, please chime in. Ultimately, I'd like to replace the use of $GLOBALS with apc, but it's not installed on my hosted server :/. I might consider memcached, but I've heard mixed reviews...