by brokenparser · 13y ago · 0 comments
$o = (object)[
'foo' => function(){ print 'bar'; },
];
$o->foo();
You have to do it this way: call_user_func($o->foo);
Or this way: $foo = $o->foo;
$foo();
Which doesn't work when foo is defined as a class method.Using __get/__set behaves the same way. Let's say $o is of SomeType that defines those magic methods. You can have a function foo on that type that is defined as a public method.
$o->foo(); // the actual instance method
$o->foo = function() { print 'bar'; };
$o->foo(); // same instance method
$foo = $o->foo;
$foo(); // prints bar
In the case of StdClass it might be possible to do it your way where foo could be callable ($o->foo()) but I can see why they wouldn't allow this in order to keep things consistent.Edit: It also behaves the same way if you assign a function to a public field of a class (I'm using 5.4) so I actually support not allowing a method to be directly callable in this fashion because this would be the only case where this behavior would occur.