摘要
正在生成中……
今天写laravel的邮件队列时候发现的,问了gpt4.0,这是 PHP 8.0 引入的新特性,叫做 Named Arguments(命名参数)
。比如有一个 Content
类:
<?php
namespace Illuminate\Mail\Mailables;
use Illuminate\Support\Traits\Conditionable;
class Content
{
use Conditionable;
public $view;
public $html;
public $text;
public $markdown;
public $with;
public $htmlString;
public function __construct(string $view = null, string $html = null, string $text = null, $markdown = null, array $with = [], string $htmlString = null)
{
$this->view = $view;
$this->html = $html;
$this->text = $text;
$this->markdown = $markdown;
$this->with = $with;
$this->htmlString = $htmlString;
}
/**
* Additional methods can be added here.
*/
}
//... 其他代码
可以这样传参数:
$newContent = new Content(
view: 'mail.kindle',
with: ['title'=>$this->subject, 'content' => $this->content],
);
此种特性让你在调用函数或方法(包括实例化类)时,参数顺序可以变得任意,并且更加明确所传递的参数对应于哪个形参。
我喜欢这个特性,不用被php参数搞得焦头烂额。