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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 07:01:33 2025 UTC