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

时间:2015-06-23 12:11:36

标签: php recursion yii

我在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;
        }
    }   
}
?>

我要做的是如果邮件发送失败,则调用相同的功能重试发送电子邮件。如果电子邮件发送仍然失败,则返回0否则1.然后使用此返回值我试图通知用户有关邮件发送错误。

早些时候,我认为返回值以下的代码不会执行。但我错了。在上面的情况下,它在返回值之后执行,因为它处于递归函数中。

那么,如何解决这个问题呢?

2 个答案:

答案 0 :(得分:1)

因此,我在提供的代码中看到了2个问题。

1)无论重发是通过还是失败,该方法都返回0

2)如果函数失败两次,该方法将返回null而不是0或1,因为没有回退来获取它(即当$this->mail_try为3时)。

以下更新的代码被修改,以便无论返回值来自递归调用,它都会直接返回而不是仅返回0.另一个更改是,如果它两次都失败,它将返回0而不是为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 :(得分:0)

什么是mail->Send()方法? 如果它用于发送电子邮件,这可能是一个答案。

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

尚未测试,请尝试