|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 18 19:00:01 2025 UTC |
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.