Anonymous function in array

I announced

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

I try to call this way, but it does not work

$call = $func['a']['b'];
$call();

I get an error Fatal error: function name must be a string

What can I call an anonymous function? I am using PHP 5.3.

Update It works, I just used the wrong keys.

+3
source share
1 answer

What you did works. Try the following:

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

See also here .

+6
source

Source: https://habr.com/ru/post/1754013/


All Articles