PHP Autoload not working?
Posted: Wed Aug 19, 2009 6:52 pm
I have the following Autoload function to load classes based on their name.
Then in my Router to load the class, I have this code:
Now, on my local MAMP server running PHP 5.2.3 this works perfectly as expected. On a remote development server running PHP 5.2.9, it gives me an error saying that it can't find the class. Why is it working as expected on my local computer, but not the remote server? Am I missing some sort of case sensitivity some where or something? If I enable the exceptions in the Autoloader, none of them get triggered. The ROOT_PATH is set like this in the index.php which handles all the requests.
I get these errors on the remote server (if I remove the class_exists($classname) statement):
Code: Select all
public static function loadClass($classname) {
$path = ROOT_PATH . '/' . str_replace('_', '/', $classname) . '.php';
if(file_exists($path)) {
include $path;
if(class_exists($classname)) {
return true;
} else {
//throw new Exception('Class '.$classname.' was not found by the autoloader');
return false;
}
}
//throw new Exception('File '.$path.' was not found by the autoloader.');
return false;
}Code: Select all
public function handleRequest()
{
$originalURI = $this->getRequestURI();
$uri = explode('/', $originalURI);
// The first element in the array is always empty
array_shift($uri);
if(empty($uri[0])) {
// The default route
$classname = 'Page_'.$this->_defaultPage.'_Action_'.$this->_defaultAction;
} elseif(empty($uri[1])) {
// Capitalize the first letter
$uri[0] = ucwords($uri[0]);
// We want to support .rss and other file types
// the action will just be called something like
//
// /forum.rss = Page_Forum_Action_IndexRSS
$action = explode('.', $uri[0]);
if(isset($action[1])) {
$action = $this->_defaultAction.strtoupper($action[1]);
} else {
$action = $this->_defaultAction;
}
$this->_page = $uri[0];
$this->_action = $action;
$classname = 'Page_'.$uri[0].'_Action_'.$action;
// Pop it off the array
array_shift($uri);
} else {
// Capitalize the first letter
$uri[0] = ucwords($uri[0]);
$uri[1] = ucwords($uri[1]);
// We want to support .rss and other file types
// the action will just be called something like
//
// /forum/index.rss = Page_Forum_Action_IndexRSS
$action = explode('.', $uri[1]);
if(isset($action[1])) {
$action = $action[0].strtoupper($action[1]);
} else {
$action = $uri[1];
}
$this->_page = $uri[0];
$this->_action = $action;
$classname = 'Page_'.$uri[0].'_Action_'.$action;
// Pop it off the array
array_shift($uri);
array_shift($uri);
}
// parse the parameters to make sure they are safe
foreach($uri as &$param) {
$param = str_replace(' ', '-', $param);
$param = strip_tags($param);
}
if(class_exists($classname)) {
if(!empty($uri)) {
// We pass the remaining parts of the URI
// for use by the page
$class = new $classname($uri);
} else {
$class = new $classname();
}
return $class;
} else {
// 404!
return false;
}
}Code: Select all
define('ROOT_PATH', dirname(__FILE__))Code: Select all
[Wed Aug 19 18:37:07 2009] [error] [client 68.5.57.63] PHP Fatal error: Class 'Page_Index_Action_Index' not found in /var/www/html/System/Router.php on line 88
[Wed Aug 19 18:37:08 2009] [error] [client 68.5.57.63] PHP Fatal error: Class 'Page_Favicon.ico_Action_IndexICO' not found in /var/www/html/System/Router.php on line 90, referer: