PHP:错误处理而不抛出异常

时间:2014-04-07 06:00:35

标签: php error-handling

我从PHP应用程序发送多封电子邮件,我想告知用户未能发送的电子邮件。

时,最优雅的方法是进行错误处理
  • 我不想抛出终止发送剩余电子邮件的异常
  • 电话会通过多种方法调用

我想要的是将所有通知中的$ notificationSucceeded从Suggestion :: notifyDeletionToAll()返回到SuggestionController。

调用堆栈的深度让我怀疑是否通过所有方法返回它是最优雅的方式,特别是当我已经从Suggestion :: cancel()返回值时。

有更好的方法吗?

控制器:

class SuggestionController {
    function cancelSuggestion($suggestionId)
    {
        $suggestion = new Suggestion();
        $suggestion->fetch($suggestionId);

        $suggestionDeleted = $suggestion->cancel();

        print json_encode(array(
            'status' => 'ok',
            'suggestionDeleted' => $suggestionDeleted,
        ));
    }
}

建议类:

class Suggestion {

    /**
     * Cancels membership of current user in the suggestion
     */
    public function cancel()
    {
        $this->cancelMembership();

        if (!$this->hasAcceptedMembers()) {
            $this->deleteAndNotify();
            return true;
        }

        return false;
    }

    /**
     * Deletes the suggestion and notifies all the users in it
     */
    private function deleteAndNotify()
    {
        $this->notifyDeletionToAll();
        DB::inst()->query("DELETE FROM suggestions WHERE id = {$this->id}");
    }

    /**
     * Notifies about the deletion of the suggestion to all members (users in the suggestion)
     */
    private function notifyDeletionToAll()
    {
        $result = DB::inst()->query("SELECT user_id FROM suggestions_users
            WHERE suggestion_id = {$this->id}");
        while ($member_id = DB::inst()->fetchFirstField($result)) {
            $member = new User();
            $member->fetch($member_id);
            $notificationSucceeded = $member->notifySuggestionDeleted($this);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我无法清楚地理解你的问题。但我希望这会对你有所帮助

$successfully_sent_arr = array();
$failure_notsent_arr   = array();
if($mail->Send())
{
    $successfully_sent_arr[] = $to_email;
    //Once the last loop occurs, update this array in database
}
else
{
    $failure_notsent_arr[] = $to_email;
    //Once the last loop occurs, update this array in database
}