Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Inherited Variable

An inherited variable is a variable that was passed to a closure via the use clause in the closure definition.

The variable is said ‘inherited’, as it is extracted from the context at definition time, and not passed as parameter, at call time.

Contrary to parameters, inherited variable cannot be typed.

<?php

$inherited = 'yes';

$closure = function() use ($inherited) {
    return $inherited;
};

trumpet($closure);

function trumpet($closure) {
    $inherited = 'no'; // This is local to the trumpet function

    echo $closure(); // display yes
}

?>

Documentation

See Also