#49765 [Opn]: Closure: Unreachable static declaration breaks lexical reference
| From: | robinf@php.net | Date: | Sun, 04 Oct 2009 13:03:44 +0000 |
| Subject: | #49765 [Opn]: Closure: Unreachable static declaration breaks lexical reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-143229@lists.php.net to get a copy of this message | ||
ID: 49765
Updated by: robinf@php.net
Reported By: robinf@php.net
Status: Open
Bug Type: Scripting Engine problem
Operating System: *
PHP Version: 5.3SVN-2009-10-04 (snap)
New Comment:
From an implementation perspective, I can see why using the
static_variables container for lexical vars makes sense.
But I think the behaviour above might be too prone to confusion.
Perhaps there could be a warning when name clashes are detected, or at
least something in the doc?
Previous Comments:
------------------------------------------------------------------------
[2009-10-04 13:00:11] robinf@php.net
Description:
------------
Hi,
Closures' lexical variable descriptors are stored in the same container
as function-scope statics: zend_function.op_array.static_variables.
This can cause unexpected behaviour when the same name is used for a
lexical variable and a function-scope static. For example, the testcase
below shows how an apparently unreachable function-scope static
declaration impacts an assignment to a lexical reference.
This is because static declarations are identified at compile time
(regardless of the execution path within the function), and they end up
taking precedence over lexical vars.
Reproduce code:
---------------
<?php
$ref = 0;
$closure = function () use (&$ref) {
var_dump($ref++);
if (false) { static $ref = 777; }
};
echo "Unreachable static declaration breaks lexical reference:\n";
$closure(); // 777
var_dump($ref); // 0
Expected result:
----------------
Unreachable static declaration breaks lexical reference:
int(0)
int(1)
Actual result:
--------------
Unreachable static declaration breaks lexical reference:
int(777)
int(0)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49765&edit=1