CKTIS / PHP
Keyword use
Import a name or alias within a namespace at compile time.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
use Vendor\Item as LocalItem;Import a name or alias within a namespace at compile time. The alias applies to that file’s namespace scope, not to class names assembled as strings. In a class body use imports traits. On an anonymous function it captures outer variables by value or, with &, by reference. Import namespace functions with use function and constants separately with use const.
namespace Store {const RATE=2; function rate(){return 3;} class Tag {public function name(){return "tag";}}}
namespace Report {use function Store\rate; use const Store\RATE; use Store\Tag as Label; trait Named {function value(){return "r";}} class Row {use Named;}
$n=2; $next=function() use (&$n){return ++$n;}; echo json_encode([(new Label())->name(),(new Row())->value(),$next(),$n,rate(),RATE]);}
["tag","r",3,3,3,2]
Examples
<?php
namespace Store {const RATE=2; function rate(){return 3;} class Tag {public function name(){return "tag";}}}
namespace Report {use function Store\rate; use const Store\RATE; use Store\Tag as Label; trait Named {function value(){return "r";}} class Row {use Named;}
$n=2; $next=function() use (&$n){return ++$n;}; echo json_encode([(new Label())->name(),(new Row())->value(),$next(),$n,rate(),RATE]);}
Result
["tag","r",3,3,3,2]