这部分代码为什么可以作为 mail::send 的一个参数呢,不是数组也不是变量?

public function send()
{
    $name = '学院君';
    $flag = Mail::send('emails.test',['name'=>$name],function($message){
        $to = '1072155122@qq.com';
        $message ->to($to)->subject('测试邮件');
    });
    if($flag){
        echo '发送邮件成功,请查收!';
    }else{
        echo '发送邮件失败,请重试!';
    }
}

问题是 mail::send 的第三个参数

function($message)
{
    $to = '1072155122@qq.com';
    $message ->to($to)->subject('测试邮件');
}

这部分代码为什么可以作为 mail::send 的一个参数呢,不是数组也不是变量?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 3

匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。最经常用作回调函数 callable参数的值。当然,也有其它应用的情况。

2年前 评论

闭包函数也可以作为变量的值来使用。PHP 会自动把此种表达式转换成内置类 Closure 的对象实例。把一个 closure 对象赋值给一个变量的方式与普通变量赋值的语法是一样的,最后也要加上分号:

2年前 评论
Richard852555 (楼主) 2年前
sreio

vendor/laravel/framework/src/Illuminate/Mail/Mailer.php

/**
     * Send a new message using a view.
     *
     * @param  string|array|\Illuminate\Contracts\Mail\Mailable  $view
     * @param  array  $data
     * @param  \Closure|string|null  $callback
     * @return void
     */
    public function send($view, array $data = [], $callback = null)
    {
        if ($view instanceof MailableContract) {
            return $this->sendMailable($view);
        }

        // First we need to parse the view, which could either be a string or an array
        // containing both an HTML and plain text versions of the view which should
        // be used when sending an e-mail. We will extract both of them out here.
        [$view, $plain, $raw] = $this->parseView($view);

        $data['message'] = $message = $this->createMessage();

        // Once we have retrieved the view content for the e-mail we will set the body
        // of this message using the HTML type, which will provide a simple wrapper
        // to creating view based emails that are able to receive arrays of data.
        call_user_func($callback, $message);

        $this->addContent($message, $view, $plain, $raw, $data);

        // If a global "to" address has been set, we will set that address on the mail
        // message. This is primarily useful during local development in which each
        // message should be delivered into a single mail address for inspection.
        if (isset($this->to['address'])) {
            $this->setGlobalToAndRemoveCcAndBcc($message);
        }

        // Next we will determine if the message should be sent. We give the developer
        // one final chance to stop this message and then we will send it to all of
        // its recipients. We will then fire the sent event for the sent message.
        $swiftMessage = $message->getSwiftMessage();

        if ($this->shouldSendMessage($swiftMessage, $data)) {
            $this->sendSwiftMessage($swiftMessage);

            $this->dispatchSentEvent($message, $data);
        }
    }
2年前 评论
Richard852555 (楼主) 2年前
sreio (作者) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!