Req #49765 [Opn->Csd]: Closure: Unreachable static declaration breaks lexical reference

From: Date: Wed, 27 Sep 2017 14:26:30 +0000
Subject: Req #49765 [Opn->Csd]: Closure: Unreachable static declaration breaks lexical reference
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-211391@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=49765&edit=1

 ID:                 49765
 Updated by:         cmb@php.net
 Reported by:        robinf@php.net
 Summary:            Closure: Unreachable static declaration breaks
                     lexical reference
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   *
 PHP Version:        *
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

This issue has been fixed with 65e456f[1] (PHP 7.1.0).

[1] <http://git.php.net/?p=php-src.git;a=commit;h=65e456f>


Previous Comments:
------------------------------------------------------------------------
[2009-10-04 13:03:43] robinf@php.net

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?

------------------------------------------------------------------------
[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 https://bugs.php.net/bug.php?id=49765&edit=1


Thread (3 messages)

« previous php.bugs (#211391) next »