Bug #68792 [Com]: Can't declare closure using $this within static method (context)

From: Date: Mon, 12 Jan 2015 21:31:14 +0000
Subject: Bug #68792 [Com]: Can't declare closure using $this within static method (context)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189914@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68792&edit=1 ID: 68792 Comment by: levim@php.net Reported by: llmll at gmx dot de Summary: Can't declare closure using $this within static method (context) Status: Duplicate Type: Bug Package: Scripting Engine problem Operating System: any PHP Version: 5.5.20 Block user comment: N Private report: N New Comment: > I really begin to wonder if you ever accept something is wrong, bogus, inconsistent or simply > stupid. You are being hostile. I am the person who made closures in static methods implicitly static as well, and I did so because of a bug report. So yes, I do accept things as bugs. Please be less accusatory in the future. Previous Comments: ------------------------------------------------------------------------ [2015-01-12 16:00:26] llmll at gmx dot de You have to try the second example from my comment. There is a working snippet. Re-enable the commented "static" and it fails. That must not happen, as the static only tags the enclosing method, not the closure itself. I really begin to wonder if you ever accept something is wrong, bogus, inconsistent or simply stupid. Claiming that a clear bug happens the same way as last year doesn't make it any better for anybody. ------------------------------------------------------------------------ [2015-01-12 15:49:35] levim@php.net In any case, your example does not work on any version of PHP I am aware of. In versions 5.4.0 - 5.6.3, I get: Warning: Cannot bind an instance to a static closure in %s on line 10 Fatal error: Using $this when not in object context in %s on line 6 If you are going to convince me this is a bug then I need proof it ever worked. ------------------------------------------------------------------------ [2015-01-10 23:08:00] llmll at gmx dot de Thanks for your attention. However, the warning is wrong, since the closure is *not* static. It is merely defined in a static method. This shouldn't trigger the warning. The containing element should not have any impact on the closure itself. Removing the "static" keyword from the enclosing method, makes it work. Adding static, produces the error again. This is clearly incosistent und unexpected, besides it is not documented. class Test { public /*static*/ function start() { $closure = function() { echo $this->foo; }; $object = new \StdClass; $object->foo = "Hello World"; $closure->bindTo($object)->__invoke(); } } (new Test)->start(); ------------------------------------------------------------------------ [2015-01-10 23:00:11] requinix@php.net You missed out on a warning Warning: Cannot bind an instance to a static closure in... making this a duplicate of bug #64761. ------------------------------------------------------------------------ [2015-01-10 17:43:13] llmll at gmx dot de Description: ------------ The problem arises, when you try to declare a Closure (anonymous function) from within a static method. The compiler breaks on the $this keyword which must not occur in a static method. However, $this is only used in the closure body and should be ignored, therfore. Test script: --------------- class Test { public static function start() { $closure = function() { echo $this->foo; }; $object = new \StdClass; $object->foo = "Hello World"; $closure->bindTo($object)->__invoke(); } } Test::start(); Expected result: ---------------- should echo "Hello World" Actual result: -------------- Fatal error: Using $this when not in object context ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68792&edit=1

« previous php.bugs (#189914) next »