Edit report at https://bugs.php.net/bug.php?id=55573&edit=1

 ID:                 55573
 Updated by:         [email protected]
 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

Reply via email to