Я делаю сайт MVC без фреймворка. Не могли бы вы помочь мне написать код в автозагрузке класса, чтобы перенаправить на страницу с ошибкой 404 (уже существует), когда класс не существует.
http://cedricjager.com/stream/index.php?p=non_existant_class => 404 стр.
class Autoload {
static function register() {
spl_autoload_register (['Autoload', 'myAutoload']);
}
static function myAutoload($class_name) {
$class = ucfirst($class_name);
require 'Models/' . $class . '.php';
}
}
class Router {.....
elseif ($p === '404') {
$this->controller->error();
}
Спасибо






Самый простой вариант - использовать Файл существует, т.е.
if (file_exists('Models/' . $class . '.php')) {
require 'Models/' . $class . '.php';
} else {
header('Location: 404.php');
}
Это должно помочь вам :)
Я надеюсь, что этот код поможет вам
static function myAutoload($class_name)
{
$class = ucfirst($class_name);
$file = "Models/{$class}.php";
try {
if (!file_exists($file)) {
throw new Exception('File doesn\'t exists', 404);
}
require $file;
} catch (Exception $e) {
$message = $e->getMessage(); // File doesn't exists
$code = $e->getCode(); //404
// show your 404 error
// or rewrite to 404 url
}
}