自定义 Zend_Autoloader 中的正确错误处理?

发布于 2024-08-25 16:47:29 字数 706 浏览 4 评论 0 原文

我正在构建一个基于 Zend Framework 的自动加载的自定义自动加载器(相关问题此处)。

从该问题中获取的基本方法是

class My_Autoloader implements Zend_Loader_Autoloader_Interface 
{
    public function autoload($class) 
    {
        // add your logic to find the required classes in here
    }
}

将新的自动加载器类绑定到类前缀。

现在我不确定如何以正确的、符合 ZF 的方式处理 autoload 方法内的错误(例如“未找到类文件”)。我对这个框架、它的惯例和风格都很陌生。

  • 我是否会悄悄返回 false 并让类创建过程崩溃?

  • 我是否以某种方式输出错误或日志消息(这可以很好地查明问题)并返回 false?如果是这样,Zend 的做法是什么?

  • 我会触发错误吗?

  • 我会抛出异常吗?如果是,是什么类型?

I'm building a custom autoloader based on Zend Framework's autoloading (related question here).

The basic approach, taken from that question, is

class My_Autoloader implements Zend_Loader_Autoloader_Interface 
{
    public function autoload($class) 
    {
        // add your logic to find the required classes in here
    }
}

and then binding the new autoloader class to a class prefix.

Now what I'm unsure about is how to handle errors inside the autoload method (for example, "class file not found") in a proper, ZF compliant way. I'm new to the framework, its conventions and style.

  • Do I quietly return false and let the class creation process crash?

  • Do I output an error or log message somehow (which would be nice to pinpoint the problem) and return false? If so, what is the Zend way of doing that?

  • Do I trigger an error?

  • Do I throw an exception? If so, what kind?

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

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

发布评论

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

评论(2

鹿! 2024-09-01 16:47:30

这取决于错误的类型。如果无法加载类,我会认为这是一个致命错误。因此我会抛出一个异常,例如

class My_Autoloader_Exception extends Exception {}

你会发现ZF在包级别使用了很多自定义异常,而且提供了一个类供其扩展(尽管我认为这是可选的)。

顺便说一句,有一个 Zend_Exception 及其自动加载器的使用示例:

  try {
      // Calling Zend_Loader::loadClass() with a non-existant class will cause
      // an exception to be thrown in Zend_Loader:
      Zend_Loader::loadClass('nonexistantclass');
  } catch (Zend_Exception $e) {
      echo "Caught exception: " . get_class($e) . "\n";
      echo "Message: " . $e->getMessage() . "\n";
      // Other code to recover from the error
  }

That depends on the kind of error. I'd consider it a fatal error if a class cannot be loaded. Thus I'd throw an Exception, e.g.

class My_Autoloader_Exception extends Exception {}

You will find that ZF uses a lot of custom Exceptions on the package level and also provides a class for this to extend from (though I'd consider this optional).

Incidentally, there is a usage example of Zend_Exception with their autoloader:

  try {
      // Calling Zend_Loader::loadClass() with a non-existant class will cause
      // an exception to be thrown in Zend_Loader:
      Zend_Loader::loadClass('nonexistantclass');
  } catch (Zend_Exception $e) {
      echo "Caught exception: " . get_class($e) . "\n";
      echo "Message: " . $e->getMessage() . "\n";
      // Other code to recover from the error
  }
天气好吗我好吗 2024-09-01 16:47:29

ZF 本身使用两种不同的方法:

  • Zend_Loader旧的自动加载机制)在出现问题时抛出 Zend_Exception
  • Zend_Loader_Autoloader当使用的注册自动加载器返回 false 时返回 false

Zend_Loader_Autoloader 不会捕获使用的自动加载器中抛出的任何异常,最终您的自定义异常会冒泡通过 Zend_Loader_Autoloader。如果我无法加载请求的类,我个人只会返回 false

ZF itself uses two different approaches:

  • Zend_Loader (the old autoloading mechanism) throws a Zend_Exception in case something's wrong
  • Zend_Loader_Autoloader returns false when the used registered autoloader returns false

The Zend_Loader_Autoloader doesn't catch any exception thrown in the used autoloader to eventually your custom exception would bubble up through the Zend_Loader_Autoloader. I personally just return false in case I'm not able to load a requested class.

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