There is an mvc structure and for convenience it is used:

set_include_path(implode(PATH_SEPARATOR, array( get_include_path(), './some_dir', ))); spl_autoload_register(function($name) { require_once $name . '.php'; }); 

The user makes a request and you need to connect the controller class:

 function getController($request_name) { // операции с именем return new $request_name; } 

In the absence of the controller, I get errors.

I try to check through class_exists - the function for some reason does not work.

try ... catch - does not work.

Format errors: warning first that there is no file. Then it gets into autoload and there is already a fatal error.

    2 answers 2

    Check class without calling autoload:

     var_dump(class_exists('BarController', false)); // false, если класс не найден 

    Update

     set_include_path(implode(PATH_SEPARATOR, array( get_include_path(), __DIR__ . '/some_dir', ))); spl_autoload_register(function($name) { $filename = $name . '.php'; if (stream_resolve_include_path($filename) === false) { return; } require_once $filename; }); var_dump(class_exists('BarController')); // false, если класс не найден 

    If you have a PHP version> = 5.3, then use a namespace, then your autoloader will look like this .

    For the future: use composer . This package manager is the de facto standard for PHP. There are plenty of examples of configuration on the same github.

    • Thus, the author will never find the desired class, there is a problem in the autoloader, and not in the call. - etki
    • @Etki Why. If it is located in the directory ./some_dir , it will find. Naturally, the file name must match the class name. For the rest, I agree with you (your answer): checking for the existence of a file, etc. - romeo
    • Look carefully at how the folder ./some_dir . - etki
    • @romeo rewrote through __ DIR __ - the problem remained. - cisido
    • Was wrong about include_path . - etki

    I try to check through class_exists - the function for some reason does not work.

    The class_exists have a stupid property to try to autoload the class if the second argument is set to true (this is the default value). The function checks not the presence of a class in the current runtime, but the presence of a class in the application - is it available, if a new instance is now created using new . At the moment of loading, PHP will contact your autoloader, which will definitely try to load the file, and not include_once , after which you can save the situation, but require_once , and you do this even without checking for the existence of the file. Therefore, you have a potentially killer situation: what is new , what is class_exists .

    What to do?

    First of all, the autoloader should never do potentially destructive actions. There may be a lot of them , the PHP autoloader system is designed so that twelve people can come in, install on the loader, and they all worked - if one failed, the next one goes. Therefore, your autoloader should simply do nothing if it cannot find the class.

    The second thing worth mentioning is Composer dependency manager. I really don’t like the fact that it combines the functions of the dependency manager and autoloader, but at the moment it is the industry standard, so the easiest thing would be to put autoloading on it.

    If you still want to write your own bootloader, you need to make it check for the existence of the file and register the directory for which to count. Even better, do it according to the PSR-4 standard, according to which the Composer autoloader works. As far as I understand, you have the first warning because you specify include_path from the noodle, and PHP first looks for a class there.

    In any case, after implementing the correct loader, you can class_exists use class_exists to check for the existence of a class.

    • in set_include_path the list of folder paths for controller classes, models, etc. How can I rewrite autoloading, without Composer, for class_exists to work? - cisido
    • @cisido, you have one folder there, which adds to the fact that you already have an include_path (and also DIRECTORY_SEPARATOR, which is able to issue the construct /./some_dir ). How to do - I have already written above: do require_once after making sure of the existence of the file using file_exists . However, I do not understand the fear of using Composer. - etki
    • rewritten pastebin.com/TTrHgzpn . Now nothing works. - cisido
    • @cisido your autoloader is looking for a file inside the current folder. include_path and user autoloader do not overlap in any way, and this folder must be specified inside the loader. And yet, I suggest either writing a full-fledged loader, or submitting this Composer task. - etki
    • Yes file_exists searches for a file in the current folder. Decided to use the stream_resolve_include_path function - cisido