问题描述
我有一个类Logger
,除其他外,它还有一个方法Log
.
由于Log
是Logger
实例的最常见用法,因此我已连线__invoke
来调用Log
I have a class Logger
which, among other things has a method Log
.
As Log
is the most common use of the Logger
instance, I have wired __invoke
to call Log
另一个类"Site"包含一个成员"Log",它是Logger的一个实例.
Another class, "Site" contains a member "Log", an instance of Logger.
为什么会起作用:
$Log = $this->Log;
$Log("Message");
但不是这样:
$this->Log("Message");
前者失败,并显示"PHP致命错误:调用未定义的方法Site :: Log()"
这是可调用对象实现的限制,还是我误会了什么?
The former fails with "PHP Fatal error: Call to undefined method Site::Log()"
Is this a limitation of the callable object implementation, or am I misunderstanding something?
推荐答案
由于某些原因您无法执行此操作:
Same reasons you can't do this:
$value = $this->getArray()["key"];
甚至是这个
$value = getArray()["key"];
因为PHP语法不能很好地简化速记.
Because PHP syntax doesn't do short hand very well.
这篇关于PHP可调用对象作为对象成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!