I'm having trouble with my autoloader function working properly when my php code is deployed to heroku. I'm using namespaces.
It works correctly in localhost. I've already made the changes necessary for the paths to translate from localhost to heroku, since heroku uses /app as document root. Thus, in the case below, BASEURL is set to:
define('BASEURL', $_SERVER['DOCUMENT_ROOT']);
Here's part of the init file:
spl_autoload_register('myAutoLoaderPerson');
function myAutoLoaderPerson($className) {
$path = BASEURL . '/classes/';
$extension = '.class.php';
$fullPath = $path . $className . $extension;
require $fullPath;
}
What am I doing wrong?
Are you sure that
$_SERVER['DOCUMENT_ROOT'])is properly filled in?I would rather recommend defining
BASEURLusing some relative definition, e.g., if the Document Root is two folder above the file definingBASEURL:Or simplifying your autoloader, to make the path relative from it:
You suggested having
/app/classes/lib\foo.class.phpbeing returned.Note the mix of
/and\. Might be that the difference is that you are on windows locally, but on linux remotely.If you follow the PSR-4 convention, then it means your namespaces should match directories, but in order to do that, you probably need to transform
\to/.Maybe like this: