Multiple problem spl_autoload_register

I am working on creating a custom structure. And I ran into a problem when I tried to dynamize the call of my classes.

This is an image of my files:

enter image description here

So, I decided to create another function for each folder (libs, controllers et modelses):

function autoloadLibs($class) { //require the general classes require 'libs/' . $class . '.php'; } function autoloadModels($class) { //require the models classes require 'models/' . $class . '.php'; } function autoloadControllers($class) { //require the controllers classes require 'controllers/' . $class . '.php'; } spl_autoload_register ('autoloadLibs'); spl_autoload_register ('autoloadControllers'); spl_autoload_register ('autoloadModels'); 

However, I have this message: Warning: require (libs / admin.php): could not open the stream , and this is not a good folder. But I do not know how to fix it. Is there a way to optimize my class calls?

+7
source share
6 answers

After several tests, I found this solution for my case:

 set_include_path(implode(PATH_SEPARATOR, array(get_include_path(), './libs', './controllers', './models'))); spl_autoload_register(); 
+9
source

Before you try to execute require , you need to check the file with is_file() .

When using spl_autoload_register() I found that it is best to register one method for including files. The fact that you can use several functions, I believe it is easier to interact with different libraries (so that they are not clobber __autoload() ). It will also save you the need to write code repeatedly to check an existing file, match _ with a directory delimiter (if you do), etc.

So, if you change your file names according to the Underscore_Separated_Name convention, for example. Controller_Admin_Dashboard , you can use ...

 function autoload($className) { $path = SYSPATH . str_replace("_", DIRECTORY_SEPARATOR, strtolower($className)) . ".php"; if (is_file($path)) { require $path; } } 

The first time you create an instance of Controller_Admin_Dashboard , PHP may include a file, such as /app/controller/admin/dashboard.php .

+4
source

If you have multiple spl_autoload_register calls, you need to make sure that you are not using the require keyword to include files, because it means "include the file or die if it cannot . "

Personally, I disagree with others that I have only one autoload function, especially if you include classes from different places, for example, controllers and some libraries. I also check if the file exists first, then include it.

tl; dr version: Do not allow spl_autoload_register calls spl_autoload_register block each other.

+4
source

your answer is here. when you register several autoloaders, php tries to load the class with any of these autoloaders. Then , php calls these autoloaders from the first registered to the last. Then , in any of these autoloaders, you should check that file_exists or not, else , php will try to enable it and throw an error if this file does not exist. Then , before turning it on, check for the file. Change autoloaders to:

 function autoloadLibs($class) { #require the general classes $file = 'libs/' . $class . '.php'; if(file_exists($file)) require $file; } function autoloadModels($class) { #require the models classes $file = 'models/' . $class . '.php'; if(file_exists($file)) require $file; } function autoloadControllers($class) { #require the controllers classes $file = 'controllers/' . $class . '.php'; if(file_exists($file)) require $file; } 
+2
source

You must check the class names before requesting a file, for example:

 function autoloadControllers($class) { //require the controllers classes if( substr( $class, -10) == 'Controller')){ require 'controllers/' . $class . '.php'; } } 

I consider it correct to cause an error if the class cannot be loaded, but you must make sure that require is called only in the correct path.

+1
source

Note that spl_autoload_register provides a third parameter ( prepend ). You can set this to true if you want to place a specific autoload function on top of the autoload stack. This means that this particular function will then be called first.

Example:

spl_autoload_register (array ('My_Class', 'My_Method'), true, true);

http://www.php.net/manual/en/function.spl-autoload-register.php

+1
source

All Articles