我需要创建一些基类异常类并从中继承。
我看到两种方法:
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”方法吗?
答案 0 :(得分:4)
如果你看一下zend库,他们通常做的是
/library
Exception.php
/Exception/
InvalidParameter.php
InvalidParameter类看起来像Exception_InvalidParameter扩展Exception。它与您在选项1中建议的非常相似。
我一般都希望将所有自定义库类都放到/library/custom
答案 1 :(得分:-3)
创建特定异常类的目的是将其与某些特定功能(数据库,会话等)相关联。如果您的功能可以在一个类中完整描述,例如
Class AdminController extends Zend_Controller_Action
{
public function loginAction()
{
throw new LoginException('Failed login');
}
}
Class LoginException extends Exception {
}
请注意,这两个类都在同一个文件AdminController.php中。这种方法更快,因为加载LoginException不需要自动加载,因为它在同一个文件中。