clearPHP icon indicating copy to clipboard operation
clearPHP copied to clipboard

Avoid direct include or require in spl_autoload_register

Open joanhey opened this issue 10 years ago • 2 comments

In https://github.com/dseguy/clearPHP/blob/master/rules/use-smart-autoload.md example:

function my_autoloader($class) { include 'classes/' . $class . '.class.php'; }

For every include that fail, will generate a warning and increase your log. If you have 4 functions (methods,..) registered using include directly, will create 4 warnings for every request.

It is worse with require, that create a fatal error and stop the execution.

It 's a shame than spl_autoload_register don't work like include_path, that try every path and show the error if all fail.

joanhey avatar May 27 '15 12:05 joanhey

What is your suggestion here ? Add a 'file_exists' to the example, to avoid those errors ?

dseguy avatar May 27 '15 13:05 dseguy

File_exists it's the only solution that I know

joanhey avatar May 27 '15 13:05 joanhey