I am trying to set up automatic loading of classes in a wordpress plugin using the spl_autoload_register function. Everything connects normally, the plugin works, but wordpress generates warnings that do not seem to relate to my plugin:

Warning: include(ΠΏΠ°ΠΏΠΊΠ° ΠΏΠ»Π°Π³ΠΈΠ½Π°/class-loco-hooks-loadhelper.php): failed to open stream: No such file or directory in ΠΏΠ°ΠΏΠΊΠ° ΠΏΠ»Π°Π³ΠΈΠ½Π°\functions.php on line 8 Warning: include(): Failed opening 'ΠΏΠ°ΠΏΠΊΠ° ΠΏΠ»Π°Π³ΠΈΠ½Π°/class-loco-hooks-loadhelper.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in ΠΏΠ°ΠΏΠΊΠ° ΠΏΠ»Π°Π³ΠΈΠ½Π°\functions.php on line 8 

Class autoload function:

 function my_plugin_autoload($class){ $file = str_replace('_', '-', strtolower($class)); include (__DIR__.'/class-'.$file.'.php'); // Ρ‚Π° самая 8-ая строка } 
  • Well, you read what you kindly write - there is no such file, that's an error. Your loader does not correctly create a path from a class. Or just the file is not there. - user207618
  • @other so judging by the name of the file it and can not be in my folder - the question is why wordpress is looking for it there - Leo240
  • So you yourself have written. Apparently, the class with the name of the loco_hooks_loadhelper type is loco_hooks_loadhelper , converted and it is searched in the __DIR__ folder. - user207618
  • just another point is that on a single virtual server with a built-in __autoload function - there are no problems β€” that's what is strange - Leo240
  • Just look at what path is created and see where the error is. - user207618

1 answer 1

This problem arises from the fact that when using autoload classes in wordpress plugins, the classes loaded from the plugin are in the common autoload queue, and to avoid problems in the autoload function, you must set the class names to match the pattern

 function my_plugin_autoload($class){ //провСряСм Π½Π°Π»ΠΈΡ‡ΠΈΠ΅ Π² ΠΈΠΌΠ΅Π½ΠΈ класса ΠΊΠΎΠ½Ρ‚Ρ€ΠΎΠ»ΡŒΠ½ΠΎΠΉ строки if(strpos($class, 'my_class_prefix_') === 0){ $class_dir = realpath(plugin_dir_path( __FILE__ )).DIRECTORY_SEPARATOR; $class_file = str_replace('_', '-', strtolower($class)); include_once $class_dir.'class-'.$class_file.'.php'; } }