Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spl_autoload fails when script ran from command line

Tags:

php

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??

like image 928
jim Avatar asked Mar 08 '26 00:03

jim


1 Answers

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');
}
like image 168
Alix Axel Avatar answered Mar 09 '26 14:03

Alix Axel