mercredi 6 septembre 2017

Rebuild PHP's extract() or how to pass variables to outer scope

I wonder if there is a way to pass variables "one scope level up" in PHP (not to the global scope), just like extract() does. I know I can do this:

function inner()
{
    $foo = [
        'bar' => 11,
        'baz' => 22
    ];
    return $foo;
}

function outer()
{
    extract(inner());
    echo $bar;
}

outer(); // 11

But is there a pattern where I can omit the extract() call in function outer(), but still access $bar? I ask this just for curiosity.

Aucun commentaire:

Enregistrer un commentaire