【发布时间】:2019-01-04 16:57:52
【问题描述】:
有没有办法只获取Mailable类中的公共类属性(不是继承的),例如:
<?php
namespace App\Mail;
use App\Mail\Mailable;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestMail1 extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $name; // i want to get only this
public $body; // and this
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.simpleview')->subject('New Mail');
}
}
这个类 (TestMail1) 继承了扩展类 (Mailable) 的许多属性,但我只想获取类本身中定义的 name 和 body 属性
我试试这个:
$mailable = (new mailable1);
$data = new ReflectionClass($mailable);
$properties = $data->getProperties(ReflectionProperty::IS_PUBLIC);
$properties_in = [];
foreach ($properties as $prop) {
if ($prop->class == $data->getName())
$properties_in[] = $prop->name;
}
dd($properties_in);
但这会返回:
array:8 [▼
0 => "name"
1 => "body"
2 => "connection"
3 => "queue"
4 => "chainConnection"
5 => "chainQueue"
6 => "delay"
7 => "chained"
]
有什么办法吗?
【问题讨论】: