I have strange problem with autoload function.
my structure is like this
Project
|
+-- test.php
|
+-- class
| |
| +-- class.news.php
I have this code:
function __autoload($class_name) {
if(file_exists('class/class.'.strtolower($class_name).'.php')){
require_once('class/class.'.strtolower($class_name).'.php');
} else {
throw new Exception("Unable to load $class_name.");
}
}
try {
$a = new News();
} catch (Exception $e) {
echo $e->getMessage(), "
";
}
i got
Fatal error: Class 'News' not found
file class.news.php
class News{
function insert($request){
return "ok";
}
}
im running this on wamp server on windows 10
Use document root to avoid issues with relative paths:
$prefix = $_SERVER['DOCUMENT_ROOT'] . '/class/class.';
$filename = $prefix . strtolower($class_name) . '.php';
if(file_exists($prefix)){
require_once($prefix);
}