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

Use

The use operator has three distinct usages.

The use operator allows the import and aliasing of fully qualified names, such as classes, interfaces, constants, etc. in the current namespace.

When use used inside a class or a trait, it imports a trait and its methods and properties. It is also the place to solve naming conflicts.

When use is used with a closure, it imports the listed variables in the closure, from the defining environment.

<?php

namespace My\Space {
    // class importation with use
    use \Some\Other\Space\Elephpant as Pachyderm;

    class Herd {
        // trait importation of use
        use Foraging;

        function trumpet() {
            $trunk = 1;
            // use imports the variable $trunk in the closure. Its value is accessible at execution time.
            $closure = function ($calf) use ($trunk) { return $trunk + $calf;};
        }
    }

    // class importation with use
    use \Some\Other\Space\Elephpant as Tusker;
}

?>

Documentation

See Also