PHP: Error handling without throwing an exception -
i'm sending multiple emails php application, , want inform user of emails failed sent.
what elegant way error handling when
- i don't want throw exception terminates sending of remaining emails
- the call goes through several method calls
what want $notificationsucceeded suggestion::notifydeletiontoall() suggestioncontroller somehow nicely notifications.
the depth of call stack made me doubt if returning through methods elegant way, when have return value suggestion::cancel().
is there better way?
controller:
class suggestioncontroller { function cancelsuggestion($suggestionid) { $suggestion = new suggestion(); $suggestion->fetch($suggestionid); $suggestiondeleted = $suggestion->cancel(); print json_encode(array( 'status' => 'ok', 'suggestiondeleted' => $suggestiondeleted, )); } }
suggestion class:
class suggestion { /** * cancels membership of current user in suggestion */ public function cancel() { $this->cancelmembership(); if (!$this->hasacceptedmembers()) { $this->deleteandnotify(); return true; } return false; } /** * deletes suggestion , notifies users in */ private function deleteandnotify() { $this->notifydeletiontoall(); db::inst()->query("delete suggestions id = {$this->id}"); } /** * notifies deletion of suggestion members (users in suggestion) */ private function notifydeletiontoall() { $result = db::inst()->query("select user_id suggestions_users suggestion_id = {$this->id}"); while ($member_id = db::inst()->fetchfirstfield($result)) { $member = new user(); $member->fetch($member_id); $notificationsucceeded = $member->notifysuggestiondeleted($this); } } }
i can't understand question clearly. hope you
$successfully_sent_arr = array(); $failure_notsent_arr = array(); if($mail->send()) { $successfully_sent_arr[] = $to_email; //once last loop occurs, update array in database } else { $failure_notsent_arr[] = $to_email; //once last loop occurs, update array in database }
Comments
Post a Comment