php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55573 "Undefined variable" when calling closure/anon function from static property
Submitted: 2011-09-02 19:12 UTC Modified: 2011-09-02 20:47 UTC
From: luke at cywh dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.8 OS: Mac OS X
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: luke at cywh dot com
New email:
PHP Version: OS:

 

 [2011-09-02 19:12 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-02 20:47 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2011-09-02 20:47 UTC] johannes@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 12:01:27 2024 UTC