php:自动加载异常处理

发布于 2024-09-01 09:36:00 字数 1443 浏览 0 评论 0原文

我正在扩展我之前的问题(在异常句柄中处理异常)来解决我的不良编码实践。 我试图将自动加载错误委托给异常处理程序。

<?php
function __autoload($class_name) {
    $file = $class_name.'.php';
    try {
        if (file_exists($file)) {
            include $file;  
        }else{
            throw new loadException("File $file is missing");
        }
        if(!class_exists($class_name,false)){
            throw new loadException("Class $class_name missing in $file");
        }
    }catch(loadException $e){
        header("HTTP/1.0 500 Internal Server Error");
        $e->loadErrorPage('500');
        exit;
    }
    return true;
}
class loadException extends Exception {
    public function __toString()
    {
        return get_class($this) . " in {$this->file}({$this->line})".PHP_EOL
                                ."'{$this->message}'".PHP_EOL
                                . "{$this->getTraceAsString()}";
    }
    public function loadErrorPage($code){
        try {
            $page = new pageClass();
            echo $page->showPage($code);
        }catch(Exception $e){
            echo 'fatal error: ', $code;
        }
    }
}



$test = new testClass();
?>

如果 testClass.php 文件丢失,上面的脚本应该加载 404 页面,并且它工作正常,除非 pageClass.php 文件也丢失,在这种情况下,我看到

“致命错误:类'pageClass'不在第 29 行的 D:\xampp\htdocs\Test\PHP\errorhandle\index.php 中找到”而不是“致命错误:500”消息

我不想为每个类自动加载添加 try/catch 块(对象创建),所以我尝试了这个。

处理这个问题的正确方法是什么?

I'm extending my previous question (Handling exceptions within exception handle) to address my bad coding practice.
I'm trying to delegate autoload errors to a exception handler.

<?php
function __autoload($class_name) {
    $file = $class_name.'.php';
    try {
        if (file_exists($file)) {
            include $file;  
        }else{
            throw new loadException("File $file is missing");
        }
        if(!class_exists($class_name,false)){
            throw new loadException("Class $class_name missing in $file");
        }
    }catch(loadException $e){
        header("HTTP/1.0 500 Internal Server Error");
        $e->loadErrorPage('500');
        exit;
    }
    return true;
}
class loadException extends Exception {
    public function __toString()
    {
        return get_class($this) . " in {$this->file}({$this->line})".PHP_EOL
                                ."'{$this->message}'".PHP_EOL
                                . "{$this->getTraceAsString()}";
    }
    public function loadErrorPage($code){
        try {
            $page = new pageClass();
            echo $page->showPage($code);
        }catch(Exception $e){
            echo 'fatal error: ', $code;
        }
    }
}



$test = new testClass();
?>

the above script is supposed to load a 404 page if the testClass.php file is missing, and it works fine, UNLESS the pageClass.php file is missing as well, in which case I see a

"Fatal error: Class 'pageClass' not found in D:\xampp\htdocs\Test\PHP\errorhandle\index.php on line 29" instead of the "fatal error: 500" message

I do not want to add a try/catch block to each and every class autoload (object creation), so i tried this.

What is the proper way of handling this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李白 2024-09-08 09:36:01

您是否尝试过在此过程的早期检查 pageClass ,因为似乎甚至有必要取出错误页面?如果它不存在,并且您不想编写不包含任何对象(例如,仅 HTML)的 404 页面,那么在该类不存在的地方进行轰炸似乎是一个不错的方法。

希望有帮助。

谢谢,

Have you tried checking for pageClass early on in the process, since it seems to be necessary even to get the error page out? If it doesn't exist, and if you don't want to write the 404 page w/o any objects (e.g. just HTML), bombing out of execution where that class doesn't exist would seem to be a good path.

Hope that helps.

Thanks,
Joe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文