如何在php中异步调用函数

I want to execute these functions when user click on button but I want to execute $model->user->email ? $model->sendUserMail(): ""; that function asynchronously in background while other function will execute as click occur Here is my code

if (isset($_POST['create_send'])) {
    if ($model->save()) {
        $model->order_id = strtotime(date("Y-m-d H:i:s")) + '' + $model->id;

        // send mail
        $model->sendMail();

        //send sms
        $messages = $model->sendSMSMessage($model);

        // create booking history
        $model->createBookingHistory($model , $messages);                     

        $model->user->email ? $model->sendUserMail(): "";

        if($model->booking_stage != 'inprogress'){
            $this->sendPushToSeller($model);
        }

        $model->update();

        $this->redirect(array('index', 'id' => $model->id));
    }
} 

There is nothing like Async Tasks in Standard PHP. You could install pthreads or you have to work with some sort of a Queue. With a Queue it would work like that:

  • Create a Mail-Send-Job
  • Push that Job into a Queue (for example a Database)
  • A third party programm (for example a cronjob) has to poll the Queue and execute the Tasks (send the Mail) if there are some

Or like @Alan Machado said, use AJAX. So your page can load, and the Buttons sends an AJAX Request to the Server that sends the Mail.

You tagged it yii, so perhaps consider a RESTful API extention, and research AJAX and CRUD. It depends on how far you want to take it. But I feel a RESTful API with yii is the best way to go when it comes to AJAX.