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


How to stop execution of code after return the value in a recursive function?

我在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 else 1。然后使用这个返回值,我试图通知用户邮件发送错误。

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

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

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

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

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

以下更新的代码经过了修改,因此无论递归调用的返回值是什么,都会直接返回,而不是只返回0。另一个变化是,如果两次都失败,它将返回0而不是空

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;
    }
} 
什么是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;
}
}

尚未测试,请尝试