开发者

Anonymous function in array

开发者 https://www.devze.com 2023-01-06 12:44 出处:网络
I have declared $func = array( \'a\' => array( \'b\' => function() { echo \"hello\"; } ) ); I try to call in this way but it doesn\'t work

I have declared

$func = array(
    'a' => array(
        'b' => function() {
            echo "hello";
        }
    )
);

I try to call in this way but it doesn't work

$call =开发者_如何学Python $func['a']['b'];
$call();

I get a error Fatal error: Function name must be a string

How can I call the anonymous function? I'm using PHP 5.3.

Update It works, I just used wrong keys.


What you did works. Try this:

<?php
$func = array(
    'a' => array(
        'b' => function() {
            echo "hello";
        }
    )
);
$call = $func['a']['b'];
$call();

See also here.

0

精彩评论

暂无评论...
验证码 换一张
取 消