Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous function in array

Tags:

function

php

I have declared

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

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

$call = $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.

like image 276
Codler Avatar asked May 04 '26 01:05

Codler


1 Answers

What you did works. Try this:

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

See also here.

like image 168
Artefacto Avatar answered May 05 '26 15:05

Artefacto