php - Spl_autoload_register file not found -
i trying autoload classes spl_autoload register not finding file , getting file not found error.
my file structure:
index.php system/ configs/ config.php messages/ message.php user/ user.php so here doing.
config.php
i got below function answer
spl_autoload_register(function ($classname) { # concatenate directly $file variable below # easy viewing on stack overflow) $ds = directory_separator; $dir = __dir__; // replace namespace separator directory separator (prolly not required) $classname = str_replace('\\', $ds, $classname); // full name of file containing required class $file = "{$dir}{$ds}{$classname}.php"; require_once $file; }); use system\user\user; $obj = new user(); user.php
namespace system\user; class user{ ... } i getting below error:
fatal error: require_once(): failed opening required 'c:\xampp\htdocs\system\configs\system\user\user.php' (include_path='.;c:\xampp\php\pear') in any help?
replace
$file = "{$dir}{$ds}{$classname}.php"; with
$file = "{$dir}/../../{$ds}{$classname}.php"; so root dir system/ instead of system/configs/
Comments
Post a Comment