tags:

views:

88

answers:

2

I know this has to do with the path not being quite right but it has me baffled. I can run my script with no problems at all from the browser but when I do to the exact same spot from a shell, spl_autoload complains and dies:

Fatal error: spl_autoload(): Class db could not be loaded in...

I am using the absolute path from the root directory, echoed to screen and pasted it into a shell and verified that it is good. Please... what am I missing??

+1  A: 

Try using the __DIR__ constant to locate the files, CLI PHP doesn't uses the same working dir.

Use something like this:

function __autoload($class)
{
    require_once(dirname(__FILE__) . '/path/to/libraries/' . $class . '.php');
}
Alix Axel
Alix, thanks for helping. I have never used the __DIR__ constant and tried to echo the result but only got back '__DIR__. I used echo $_SERVER['SCRIPT_FILENAME']; instead and it gave me the exact path where my script resides. Is this what you wanted to see?
jim
`__DIR__` is only available since PHP 5.3.0, use `dirname(__FILE__)` instead.
Alix Axel
A: 

you can usually grab your root directory for the project with something along the lines of :

// The file that defines this is 2 directories below root, hence the ../ changes.
define('PATH_ROOT', realpath(dirname(__FILE__) . '/../../'));

Once you have your root path you can modify your include path, using set_include_path. (remember to include get_include_path when you set it otherwise you'll lose the defaults)

once thats sorted, just setup your autoloader assuming against the root dir and you should be fine, since its a bit more concrete than relying on relative paths which can change according to the working dir.

devians