Search code examples
phpxamppnamespacesautoload

PHP autoloading namespace on xampp


I am trying to autoload php class under namespace on xampp. But for some reason it cannot find class under its absolute path.

here is my autoloader:

spl_autoload_register(function($className){
    require_once($_SERVER["DOCUMENT_ROOT"] .'/' . str_replace('\\', '/', $className). '.php');
})

here is my class

namespace app\admin\modules\smartForm;
class smartForm {
/* Class logic*/
}

This is my file system:

This is my file system

This i error i am getting:

Warning: require_once(C:/xampp/htdocs/app/admin/modules/smartForm/smartForm.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\php\smartfrom\app\system\autoLoader.php on line 3


Solution

  • Looks like after 3 days i finally figure it out.

    Instead of going trought absolute path you can choose relative path from autoloader file.

    here is final include:

      include_once(__DIR__.'\..\\'.$className.'.php');