Php 在Zend Framework中放置自定义系统特定异常的位置

Php 在Zend Framework中放置自定义系统特定异常的位置,php,zend-framework,exception,Php,Zend Framework,Exception,我需要创建一些基本异常类并从中继承 我认为有两种方法: 1. Create folder 'Exceptions' in library and use standard autoload mechanism /library /Exception/ Base.php InvalidParameter.php 2. Just declare exception classes in model, that uses it. 有更好的“Zend

我需要创建一些基本异常类并从中继承

我认为有两种方法:

1. Create folder 'Exceptions' in library and use standard autoload mechanism
    /library
      /Exception/
        Base.php
        InvalidParameter.php
2. Just declare exception classes in model, that uses it.

有更好的“Zend-ish”方法吗?

如果您查看Zend库,他们通常做的是

/library
   Exception.php
  /Exception/
    InvalidParameter.php
InvalidParameter类看起来像Exception\u InvalidParameter扩展了Exception。这与您在选项1中的建议非常相似。
我通常喜欢将我的所有自定义库类放入到
/library/custom

中。创建特定异常类的目的是将其与某些特定功能(数据库、会话等)关联起来。例如,如果您的功能可以在一个类中完全描述

Class AdminController extends Zend_Controller_Action
{

public function loginAction()
{
  throw new LoginException('Failed login');
}
}

Class LoginException extends Exception {
}

注意,这两个类位于同一个文件AdminController.php中。这种方法速度更快,因为加载LoginException时不需要自动加载,因为它位于同一个文件中。

我想说选项1非常简单,这使得
LoginException
类只在使用
AdminController
的地方可用(id.throwable和catchable)(id.loaded)。