【发布时间】:2020-08-04 17:57:16
【问题描述】:
我不明白为什么类属性是一个可调用对象this->property()?
<?php
class A
{
public function __invoke($t)
{
return $t * 3;
}
}
class B
{
public function __construct(A $a)
{
$this->a = $a;
}
public function yo($t)
{
return $this->a($t);
}
}
echo (new B(new A))->yo(8);
这将导致错误:
<br />
<b>Fatal error</b>: Uncaught Error: Call to undefined method B::a() in [...][...]:21
Stack trace:
#0 [...][...](28): B->yo(8)
#1 {main}
thrown in <b>[...][...]</b> on line <b>21</b><br />
为了完成这项工作,我必须将方法 yo 更改如下:
public function yo($t)
{
return ($this->a)($t); // this works
$x = $this->a; // this works
return $x($t); // as well
}
我在网上找不到任何解释。有什么想法吗?
【问题讨论】:
标签: php