Call closure stored as object property directly without use of temporary variable

From: Date: Sat, 08 Dec 2012 21:48:50 +0000
Subject: Call closure stored as object property directly without use of temporary variable
Groups: php.internals 
Request: Send a blank email to internals+get-64199@lists.php.net to get a copy of this message
First, thanks everyone for the hard work on the PHP 5.5 alpha. I just ran the tests and submitted some results, but from the little bit I've used it so far, things are looking really nice. Second, and to the main point of this email, I would like to be able to call closures that are stored as object properties directly without having to make use of a temporary variable. For example: <?php $o = new stdClass(); $o->func = function(){ return 'Yes!'; }; $o->func(); ?> The above code elicits the error: Call to undefined method stdClass:test() in... I realize I could create a class that implements __call() to intercept and redirect the call to the closure, but this adds much overhead for a simple approach and also precludes the use of stdClass (I like the option of being able to cast to an array, and then back with very little overhead.) It seems as though the parser could be updated to accommodate this intention, but I understand this could pose a backwards compatibility issue (current users could have closures stored as object properties and be expecting the magic method __call() to be invoked instead of the closure itself.) Has anyone else wanted this functionality? Has anyone else thought of ideas of addressing this (or come to the conclusion it really isn't safely addressable without causing disproportionate amounts of grief?) Thank you, Adam

« previous php.internals (#64199) next »