Reproduced with this code
<?php
$justReturn1 = function() { return 1;};
class A {
public $f;
public function __construct($func) {
$this->f = $func;
}
}
$instance = new A($justReturn1); // Create instance
$f = $instance->f; // Assign the function within $instance->f to a variable external to object
echo $f(); // Returns: 1
echo $instance->f(); // Returns: Fatal error: Call to undefined method A::f()
The same function can be invoked if I assign it to a variable, but can't be invoked if I assign it to an object property.
This is really annoying, since I was counting on this consistency (common sense).
I'm writing a class that receives a big configuration array with some validation functions inside it. I then store those validation functions on an object just fine, but I need an indirection step (creating a variable and assigning this function to it) before I can invoke them!
Is there a flaw in my test? Is this actually PHP unimplementedness?