PHP:不引发异常的错误处理

PHP:不引发异常的错误处理,php,error-handling,Php,Error Handling,我正在从一个PHP应用程序发送多封电子邮件,我想通知用户未能发送的电子邮件 当发生错误时,最优雅的错误处理方式是什么 我不想抛出一个异常来终止剩余电子邮件的发送 该调用经过几个方法调用 我要做的是从Suggestion::notifyDeletionToAll()到SuggestionController的$NotificationSuccessfueld从所有通知中以某种方式返回 调用堆栈的深度让我怀疑通过所有方法返回它是否是最优雅的方式,特别是当我已经从Suggestion::cance

我正在从一个PHP应用程序发送多封电子邮件,我想通知用户未能发送的电子邮件

当发生错误时,最优雅的错误处理方式是什么

  • 我不想抛出一个异常来终止剩余电子邮件的发送
  • 该调用经过几个方法调用
我要做的是从Suggestion::notifyDeletionToAll()到SuggestionController的$NotificationSuccessfueld从所有通知中以某种方式返回

调用堆栈的深度让我怀疑通过所有方法返回它是否是最优雅的方式,特别是当我已经从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);
        }
    }
}
建议类:

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);
        }
    }
}

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

$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
}

你的问题不清楚。添加更多详细信息:您的重要代码片段、所需行为的描述e t.c.既然您说过“调用经过几个方法调用”,那么使用方法的返回值,每次返回“false”时,在变量中添加一行类似“Email#1无法发送”的内容?感谢您的快速响应,我澄清了问题。我澄清了问题。@Ipa Ok将检查并返回