PHP可调用对象作为对象成员 [英] PHP Callable Object as Object Member

查看:101
本文介绍了PHP可调用对象作为对象成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类Logger,除其他外,它还有一个方法Log.
由于LogLogger实例的最常见用法,因此我已连线__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可调用对象作为对象成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆