2

Possible Duplicate:
Workaround for basic syntax not being parsed
Why don't PHP attributes allow functions?

I'm getting

Parse error: syntax error, unexpected T_FUNCTION in /home/codemonkey/dev/broadnet/bito.api2/broadnet/resources/broadmapresource.php on line 87

From this code:

private $debugFunctions = array(
    "testing" => function() // Line 87
    {
        return "I'm a test function!";
    },
    "foo" => function()
    {
        return "bar";
    }
);

I was under the impression I could use anonymous PHP functions anywhere I could use $variables. Is this an exception, or am I doing something wrong?

I'm using PHP 5.3.9

Community
  • 1
  • 1
Hubro
  • 56,214
  • 69
  • 228
  • 381
  • I guess it's because it's in a class, and related to this: [Why don't PHP attributes allow functions?](http://stackoverflow.com/q/3960323) Does it work in a normal array outside a class? This reeks of a PHP design flaw... But then, functions aren't really first class citizens in PHP yet – Pekka Feb 01 '12 at 11:26
  • 1
    Have you tried doing this assignment in a constructor instead of directly in the class definition? – Czechnology Feb 01 '12 at 11:28
  • as usual: runtime information cannot be assigned to properties before runtime. Lambdas are instances of the Closure class. You cannot have instances as properties on declaration either. If you want lambdas in there, you have to add them from the ctor. – Gordon Feb 01 '12 at 11:29
  • @Czech that would surely work, but the way he shows would arguably be much nicer. It sucks that it's not possible to do this way – Pekka Feb 01 '12 at 11:29
  • I think somebody is stalking me and down voting all my questions. They aren't **that** stupid. – Hubro Feb 01 '12 at 11:34
  • I know it's the old question but I just want to do exactly you did and I found the solution by wrapping the anonymous function with [call_user_func](http://cn2.php.net/manual/fa/function.call-user-func.php). Not sure if it work in PHP 5 but works on PHP7. – spicydog Jun 28 '18 at 08:15

4 Answers4

4

You can't do this with the class property, the property initialization must be a constant value.

The initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

xdazz
  • 158,678
  • 38
  • 247
  • 274
4

Use a constructor:

class TestClass {
    private $debugFunctions;

    public function __construct() {
        $this->debugFunctions = array(
            "testing" => function()
            {
                return "I'm a test function!";
            },
            "foo" => function()
            {
                return "bar";
            }
        );
    }
}

For explanation why, see xdazz's answer: class properties must be constant and known at compile time. If you need it to be dynamic or more complex, you cat populate it during initialization of the instance (in the constructor).

Czechnology
  • 14,832
  • 10
  • 62
  • 88
1

Update: This answer was intended for PHP 5, it will not work with PHP 8.

Not like that, take a look at create_function. It creates a new function in the current scope and returns the function name.

private $debugFunctions = array(
    "testing" => create_function('', 'return "I\'m a test function!";'),
    "foo" => create_function('', 'return "bar";');
);

Not sure if the above example works, though. I didn't have time to test it.

Also note that using create_function can significantly slow down your application, depending on how frequent you use it.

Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102
0

You can do that.

Just init it in the costructor:

function __construct() {
    $debugFunctions = array(
        "testing" => function() // Line 87
        {
            return "I'm a test function!";
        },
        "foo" => function()
        {
            return "bar";
        }
    );
}
Czechnology
  • 14,832
  • 10
  • 62
  • 88
dynamic
  • 46,985
  • 55
  • 154
  • 231