使用此代码:
$notis = [
new InternalNotification([
$this->receiver()->associate(User::first()->id),
'product_id' => 1,
'notification_id' => 1,
'is_read' => false,
]),
];
如何确保在第3行(InternalNotification
)上获得$this->
的当前实例?
答案 0 :(得分:0)
如果我理解正确的问题,那么答案是“您不能”,这是一个非常根本的原因:它尚不存在。
PHP(和大多数语言)的表达式从内到外进行评估-给出一个简单的示例var_dump(foo());
必须运行函数foo()
才能转储其输出。
所以您的代码与此等效:
$a = User::first()->id;
$b = $this->receiver()->associate($a);
$notis = [
new InternalNotification([
$b,
'product_id' => 1,
'notification_id' => 1,
'is_read' => false,
]),
];
$ b无法查看未来并获取尚不存在的对象。最早可以访问该对象的地方是它的构造函数。