Spl_autoload does not work when the script is run from the command line

I know that this is due to the fact that the path is not quite right, but it puzzled me. I can run my script without any problems from the browser, but when I do this with the same place from the shell, spl_autoload complains and dies:

Fatal error: spl_autoload (): db class could not be loaded into ...

I use the absolute path from the root directory, echo to the screen and paste it into the shell and verify that it is good. Please ... what am I missing?

+3
source share
2 answers

Try using a constant __DIR__to search for files; the CLI PHP does not use the same working directory.

Use something like this:

function __autoload($class)
{
    require_once(dirname(__FILE__) . '/path/to/libraries/' . $class . '.php');
}
+2

- :

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

, include, set_include_path. ( get_include_path, , )

Thats , , , , .

0

Source: https://habr.com/ru/post/1727097/


All Articles