Question

I am hosting on a GoDaddy shared hosting account. My absolute hosting path is:

/home/content/a/d/m/admwta/html/eqflow/

I have a directory structure like this:

eqflow
->api
   ->classes
     ->security
     ->utils
   ->v1

I have defined one class per file hosted in either the security or util directory. All files are all lower case and for the class names I followed the PEAR convention of _ to / so a file called getpasswordhash.php in the security directory directory has the name api_classes_security_getpasswordhash.

I have this autoload function:

function replaceunderscores ($classname) {
$path = str_replace('_', DIRECTORY_SEPARATOR, $classname);
$fullpath = "/home/content/a/d/m/admwta/html/eqflow/".$path.".php";

echo $fullpath . " \n";
if (file_exists($fullpath))  {

    require_once ($fullpath);
}
else {
    echo "could not find file \n";
}
}
spl_autoload_register('replaceunderscores');

when I call login.php, it always fails with this message

/home/content/a/d/m/admwta/html/eqflow/api/classes/security/getpasswordhash.php could not find file
Fatal error: Class 'api_classes_security_getpasswordhash' not found in /home/content/a/d/m/admwta/html/eqflow/api/v1/login.php on line 27

it is not passing the file_exists test in the autoload script, I don't know why? You can see in the echo statement I put to echo full path I am giving the full path of the file?

Was it helpful?

Solution

I got this to work by changing the way I constructed the $fullpath in the autoload function. Rather than hard code the path, I used the $_SERVER["DOCUMENT_ROOT"] variable. So for me the path becomes:

$fullpath =  $_SERVER["DOCUMENT_ROOT"]."/eqflow/".$path.".php";

That works perfectly. I am unsure what the difference is between hardcoding the document root and using the server variable but using the server variable worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top