Edit report at https://bugs.php.net/bug.php?id=65598&edit=1
ID: 65598
User updated by: php at davidstockton dot com
Reported by: php at davidstockton dot com
Summary: Closure executed via static autoload incorrectly
marked as static
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: Centos 6
PHP Version: 5.5.3
Block user comment: N
Private report: N
New Comment:
The closure is not created in a static method, it's just loaded via an
autoloader
that happens to be static. Seems odd that we should expect different behavior
of the
code based on if the autoloader is static or non-static.
Previous Comments:
------------------------------------------------------------------------
[2013-09-17 20:52:18] bixuehujin at gmail dot com
The reason is closures created in static methods are static, even without the
keyword.
see
https://github.com/php/php-src/blob/a447acdcc6f12ea3a5dcd22416cb033e62995935/Zend/tests/closure_045.phpt
------------------------------------------------------------------------
[2013-08-30 23:55:29] php at davidstockton dot com
Description:
------------
If I load a class via a static autoloader which has executable code (including
a
closure) and then try to bind the closure to an object of the class, I get a
warning and error:
Warning: Cannot bind an instance to a static closure
and
Fatal error: Using $this when not in object context
If I run the script directly (not via autoloader) everything works as expected.
If I make the autoloader non-static, everything works as expected.
Test script:
---------------
Sample contains two files, a static autoloader which will load the class file
and the class file which also contains executable code.
If the autoloader is a plain function or a non-static method then the code
works as expected. If the executable code is removed from the class file and
placed into the autoloader file, everything works as expected.
php Autoloader.php (bad behavior)
php ClosureBug.php (correct behavior)
https://gist.github.com/dstockto/6395158
Expected result:
----------------
42
Actual result:
--------------
Warning: Cannot bind an instance to a static closure in
/vagrant/src/Bug/ClosureBug.php on line 9
Fatal error: Using $this when not in object context in
/vagrant/src/Bug/ClosureBug.php on line 19
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65598&edit=1