有几种常见的模式:
2.返回NULL或FALSE,并将引用设置为错误.例如.
function createUser($user, $password, &$error)
{
//...
// You could use any type of object here. This is just a simple example.
if(uniqueKeyFailure)
{
$error = new UserAlreadyExists();
return NULL;
}
//..
}
它可以称为:
$userCreateError = NULL;
$res = createUser($user, $pass, $userCreateError);
if($res === NULL)
{
// do something with $userCreateError
}
3.返回NULL或FALSE,并提供最后一个错误(例如curl_error).
我建议1或2.主要原因人们避免例外的“不寻常的”错误,如用户输入是性能.有这么多的讨论,比如Performance of try-catch in php.
我不推荐3,因为它不是可重入的.