Bug #55573 [Opn->Bgs]: "Undefined variable" when calling closure/anon function from static property
| From: | johannes@php.net | Date: | Fri, 02 Sep 2011 20:47:22 +0000 |
| Subject: | Bug #55573 [Opn->Bgs]: "Undefined variable" when calling closure/anon function from static property | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-163266@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55573&edit=1
ID: 55573
Updated by: johannes@php.net
Reported by: luke at cywh dot com
Summary: "Undefined variable" when calling closure/anon
function from static property
-Status: Open
+Status: Bogus
Type: Bug
Package: Scripting Engine problem
Operating System: Mac OS X
PHP Version: 5.3.8
Block user comment: N
Private report: N
New Comment:
Foo::$bar['bar'](); has been a valid construct for a long time in PHP. This will first
interpret $bar['bar'] and take that as method which will then be executed. So if you have
$bar = array('bar' => 'baz');
Foo::$bar['bar']();
the method Foo::baz() will actually be called.
Changing this is part of a bigger project which revamps the relationship between properties and
methods. If/When that will be implemented I can't say. For now this is expected behavior.
Previous Comments:
------------------------------------------------------------------------
[2011-09-02 19:12:42] luke at cywh dot com
Description:
------------
When trying to call a function stored in a static property I get the "Undefined
variable" notice and a "Function must be a string" fatal error. This happens
whether or not the function is stored directly in the property or in an array.
A work around (for now) seems to be storing the property into a temp variable.
Test script:
---------------
// Test Case 1:
class Foo
{
public static $bar;
}
Foo::$bar = function() {
echo 'Foo bar!';
};
Foo::$bar();
// Work around:
/*
$f = Foo::$bar;
$f();
*/
// Test Case 2:
class Foo
{
public static $bar = array();
}
Foo::$bar['bar'] = function() {
echo 'Foo bar!';
};
Foo::$bar['bar']();
Expected result:
----------------
Foo bar!
Actual result:
--------------
Notice: Undefined variable: bar
Fatal error: Function name must be a string
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55573&edit=1