phpinvokecallable

PHP Callable Object as Object Member


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

Another class, "Site" contains a member "Log", an instance of Logger.

Why would this work:

$Log = $this->Log;  
$Log("Message");  

But not this:

$this->Log("Message");

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?


Solution

  • Same reasons you can't do this:

    $value = $this->getArray()["key"];

    or even this

    $value = getArray()["key"];

    Because PHP syntax doesn't do short hand very well.