dtdd25012 2015-06-23 12:11
浏览 80
已采纳

如何在递归函数中返回值后停止执行代码?

I have a code like below in yii.

<?php
class MediaController extends Controller {
     public $mail_try = 1;

    public function actionUpdate() 
    {
        // ........... Other Code
        $return_err = array();
        /* Now Send Email */
        if(sizeof($mail_queue)>0)
        {
            foreach($mail_queue as $resmail)
            {
                $this->mail_try = 1;
                $to_arr = $resmail['to_arr'];
                $cc_arr = $resmail['cc_arr'];
                $from = $resmail['from'];
                $subject = $resmail['subject'];
                $message = $resmail['message'];
                $log_msg = $resmail['log_msg'];
                $attachment = $resmail['attachment'];
                $log =  $resmail['log'];
                $output = $this->mailsend($to_arr, $cc_arr, $from, $subject, $message, 'Image/Media update (action : insert) ', $attachment,$log);
                if($output==0)
                {
                    $return_err[] = $resmail['vendor_name'];
                }
            }
        }
    }


    public function mailsend($to, $cc, $from, $subject, $message, $crontask, $attachment = array(),$log='') {
        //.......  Other Code
        //.......  Other Code
        try{
             if (!$mail->Send()) {
                if($this->mail_try < 3)
                {
                    $this->mail_try++;
                    $this->mailsend($to, $cc, $from, $subject, $message, $crontask,$attachment,$log);
                    return 0;
                }
            } else {
                return 1;
            }
        } catch (Exception $ex) {
            return 0;
        }
    }   
}
?>

What i am trying to do is if mail sending is failed, then call the same function to retry to send the email. If email sending is still failed then return 0 else 1. Then using this return value i am trying to notify the user about mail sending error.

Earlier, i thought that code below the return value will not execute. But i am wrong. In above case it execute after the return value, because it is in recursive function.

So, How to solve this problem?

  • 写回答

2条回答 默认 最新

  • dongzhan7253 2015-06-23 12:43
    关注

    So there are 2 problems that I see with the code provided.

    1) No matter whether the resend passes or fails, the method returns 0

    2) If the function fails twice, the method will return null instead of 0 or 1 as there is no fallback to pick it up (i.e. when $this->mail_try is 3).

    The following updated code is modified so that no matter what the return value is from a recursive call, it is returned back directly rather than returning just 0. The other change is that if it fails both times, it will return 0 instead of null

    public function mailsend($to, $cc, $from, $subject, $message, $crontask, $attachment = array(),$log='') {
        //.......  Other Code
        //.......  Other Code
        try{
             if (!$mail->Send()) {
                if($this->mail_try < 3)
                {
                    $this->mail_try++;
                    return $this->mailsend($to, $cc, $from, $subject, $message, $crontask,$attachment,$log);
                }
                return 0;
            } else {
                return 1;
            }
        } catch (Exception $ex) {
            return 0;
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办