1

I'm using the following snippet to auto-load my php classes

spl_autoload_register(function ($path) {
    $path = strtolower(str_replace('\\', '/', $path));
    require_once "./$path.class.php";
});

now that i'm using my classes like this use Core\ClassA as ClassA; or use ABC\ClassBBC as ClassBBC it's working perfectly.

but when i want to create a new instance of the php built-in class mysqli i get the following warning:

Warning: require_once(./mysqli.class.php): failed to open stream: No such file or directory in /var/www/html/projectA/autoloader.php on line 9

how can i enforce the auto-loader to load mysqli regardless of my auto-loader?

Mumen Yassin
  • 502
  • 4
  • 16

1 Answers1

1

Several things here :

  • first off, you should check that file exists before requiring it, using file_exists
  • second, don't start your require by "./". You should only use absolute paths. You can learn why here : PHP - Failed to open stream : No such file or directory
  • third, there is no need for you to use require_once. Since autoloader is called, you can be sure that the file was not already included. And require_ once is slower than require.

In any case, the fact that your autoloader is being called for mysqli means that it was not found before, so this is strange. Since you mention that you are working with legacy code, maybe you need to reregister the default autoloader as well.

Community
  • 1
  • 1
Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76