【发布时间】:2011-08-02 21:41:16
【问题描述】:
我正在尝试重新构建我开发的使用 MVC 模式的 Web 应用程序,但我不确定如何处理错误。例如:
class AM_Products extends AM_Object
{
public function save( $new_data = array() )
{
// Validate input
// Save input
}
}
如果我传递了无效的输入来保存,我应该抛出这样的异常吗:
class AM_Products extends AM_Object
{
public function save( $new_data = array() )
{
// Validate input
if ( ! validate( 'text', $new_data['name'] ) ) {
throw new Exception( 'Invalid data entered' );
}
// Save input
}
}
或者,我是否应该添加一个额外的功能并将其留给视图/控制器:
if ( $product->save( $data )->has_error() ) {
$error = $product->get_error();
}
echo '<p>' . $error . '</p>';
【问题讨论】:
标签: php model-view-controller architecture