8

I'm playing around with the SPL autoload functionality and seem to be missing something important as I am currently unable to get it to work. Here is the snippet I am currently using:

// ROOT_DIRECTORY translates to /home/someuser/public_html/subdomains/test
define('ROOT_DIRECTORY', realpath(dirname(__FILE__)));
define('INCLUDE_DIRECTORY', ROOT_DIRECTORY . '/includes/classes/');
set_include_path(get_include_path() . PATH_SEPARATOR . INCLUDE_DIRECTORY);
spl_autoload_extensions('.class.php, .interface.php, .abstract.php');
spl_autoload_register();

When I echo get_include_path() I do get the path I expected:

// Output echo get_include_path();
.:/usr/lib/php:/usr/local/lib/php:/home/someuser/public_html/subdomains/test/includes/classes/

However when I run the code I get this error message:

Fatal error: spl_autoload() [function.spl-autoload]: Class Request could not be loaded in /home/someuser/public_html/subdomains/test/contact.php on line 5

Request.class.php is definitely in the /home/someuser/public_html/subdomains/test/includes/classes/ directory.

What am I missing?

hakre
  • 193,403
  • 52
  • 435
  • 836
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • @Yannis Rizos - That is indeed my problem. Make this an answer so I can vote it up and give you credit for the correct answer. – John Conde Jul 06 '10 at 15:06

3 Answers3

20

There is a comment (anonymous) on http://www.php.net/manual/en/function.spl-autoload-register.php#96804 that may apply to your problem: spl_autoload_register() doesn't seem to play nice with camelcase, and in your case could be trying to find request.class.php instead of Request...

CodeVirtuoso
  • 6,318
  • 12
  • 46
  • 62
yannis
  • 6,215
  • 5
  • 39
  • 49
  • 1
    I have found this bug as well. As soon as I changed the class name to lower case all worked fine. Do you know if this has been reported as a bug? – AntonioCS May 18 '11 at 22:19
  • 2
    There are bugs open for this. Ridiculous that it's still an issue. I went through the bug system and voted on relevant ones, here's a good place to start: https://bugs.php.net/bug.php?id=53065. – zombat Sep 05 '11 at 01:21
  • 1
    If you still want to use spl, you can use a closure that overloads the default function so you don't get lowercased: `spl_autoload_register( function( $class ) { include $class . '.php'; }); ` - tune your paths ;) – Xavi Montero Oct 10 '14 at 10:44
  • Not necessarily a huge deal, but NOT using the default spl_autoload() and defining your own generates a performance hit due to the stat() execution generated during the include function... saw it in comments on php docs... – Rimer Mar 08 '15 at 01:57
0

I got a similar error message but my issue was different. My error message looked like

PHP Fatal error:  spl_autoload(): Class Lib\Lib\Regex could not be loaded in /dir1/dir2/lib/regex.php on line 49

It turned out I forgot to remove the Lib\ from Lib\Regex inside the Regex class definition itself. I had something like the following:

namespace Lib;

class Regex {

...

   public static function match($pattern, $str) {

      $regex = new Lib\Regex($pattern);

      ...
   }
}
David Winiecki
  • 4,093
  • 2
  • 37
  • 39
0

The path where the class is supposed to be seems not to match the path were you expect them. Compare

.:/usr/lib/php:/usr/local/lib/php:/home/someuser/public_html/subdomains/test/includes/classes/

with

/home/someuser/public_html/subdomains/test/

The difference is, that your class is not in includes/classes/ as your SPL requires it but a few directories above.

DrColossos
  • 12,656
  • 3
  • 46
  • 67