1

I made a simple autoloader using spl_autoloader_register function, it works fine on my virtual server, but in the server I only got "Fatal Error: Class 'X' not found". Im running it on a mac with PHP 5.4, but it also works in windows/ubuntu with 5.3 version, which is the same as my physic server. I don't have SSH access to it. Here is my autoload code:

class Load { public static function autoload($class) { $class = strtolower($class); $lib = $_SERVER['DOCUMENT_ROOT'] . BASENAME . "/libs/{$class}.php"; $model = $_SERVER['DOCUMENT_ROOT'] . BASENAME . "/models/{$class}.class.php"; $controller = $_SERVER['DOCUMENT_ROOT'] . BASENAME . "/controllers/{$class}.php"; if(is_readable($lib)){ require_once $lib; }elseif (is_readable($model)) { require_once $model; }elseif (is_readable($controller)){ require_once $controller; } } } spl_autoload_register("Load::autoload"); 

I always used spl for local apps, but its the first time I'm trying it on the server. Any advice for better practices will be helpful. Thanks

8
  • Check the return value of spl_autoload_register. If it's FALSE, something went wrong with the registering. Commented Sep 17, 2013 at 19:22
  • I put an else in the condition, and it stopped there, so its failing the is_readable, but the path/filename its right, besides, if was the path, it should not work local, right? Commented Sep 17, 2013 at 19:27
  • Make sure your $lib, $model and $controller variables contain the right path. Commented Sep 17, 2013 at 19:33
  • It is, because it works local Commented Sep 17, 2013 at 19:38
  • stackoverflow.com/questions/3710480/php-spl-autoload-register Commented Sep 17, 2013 at 19:44

1 Answer 1

4

A good practice can be to add your own include path. Then you can disclaim $_SERVER['DOCUMENT_ROOT']. For example..

// Define path to library define('MY_LIBRARY_PATH', realpath(dirname(__FILE__) . '/../insert_path_here_relativly')); // Ensure library is on include_path set_include_path( get_include_path() . PATH_SEPARATOR . MY_LIBRARY_PATH ); 

Then your autoloader..

class Load { public static function autoload($class) { $class = strtolower($class); $lib = MY_LIBRARY_PATH . "/libs/{$class}.php"; $model = MY_LIBRARY_PATH . "/models/{$class}.class.php"; $controller = MY_LIBRARY_PATH . "/controllers/{$class}.php"; if(is_readable($lib)){ require_once $lib; }elseif (is_readable($model)) { require_once $model; }elseif (is_readable($controller)){ require_once $controller; } } } spl_autoload_register("Load::autoload"); 
Sign up to request clarification or add additional context in comments.

4 Comments

is_readable($file) returns TRUE if file either a dir or a regular file. So what if it was a directory, like /foo/bar.php? Then require_once() will attempt to include a directory, jeez!
And also, this class is full of bad practices, including global state and tight-coupling. Also, spl_load_register() is a part of Load class, thus it should be implemented within a method that belongs in the class.
Yeah you are right, but its a simply way to build an own autoloader. You can also use is_file in additional to check on real file. Its better to build a real Loader class, with namespacing, more than one directories and so on.
Yes, that looks fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.