0

I have an autoloader which loads all the classes I need

<?php
spl_autoload_register('app_autoload');

function app_autoload($class)
{
    require "class/$class.php";
}
?>

But there is a problem, if I create a login folder and I need to use my autoloader inside this folder, the autoloader still do a **require "class/$class.php";** but the folder has changed and should be **../class/$class.php**

How can I manage my code for it to be available in every folders of my website ?

Thanks

Aman Kumar
  • 4,533
  • 3
  • 18
  • 40
whitelamp
  • 37
  • 1
  • 5
  • Use an absolute path to the class directory. You could use `$_SERVER['DOCUMENT_ROOT']` or a constant defined elsewhere or even just add the class directory to the list of include paths. – Jonathan Kuhn Jun 03 '16 at 19:13
  • Related (on the fence about a dupe) : [Are PHP include paths relative to the file or the calling code?](http://stackoverflow.com/questions/7378814/are-php-include-paths-relative-to-the-file-or-the-calling-code) – HPierce Jun 03 '16 at 19:24
  • Is there any reason you can't rely on composer's autoloader? – Jonathan Jun 03 '16 at 21:29

0 Answers0