This article mainly shares with you the detailed explanation of the use keyword in php, I hope it can help you. At present, I summarize the use of the use keyword in three ways.
1. Declare the use of a class in a certain namespace
There are many online information on usage in a namespace, and the manual explains it in detail, so I won’t go into details here
2. Used after an anonymous function to add parameters to the anonymous function
Mainly explains the use of use in anonymous functions. Use in anonymous functions can achieve the effect of using internal variables outside the function and changing the variables. Scope.
// 输出 hello world function test(){ $word = 'world'; $func = function($para)use($word){ echo $para ." ".$word; }; $word = 'php'; return $func; } $func = test(); $func('hello');
// 输出 hello php function test(){ $word = 'world'; $func = function($para)use(&$word){ // 引用传值 echo $para ." ".$word; }; $word = 'php'; return $func; } $func = test(); $func('hello');
Related recommendations:
New PHP features useEnhanced use
The above is the detailed content of Detailed explanation of use keyword usage in php. For more information, please follow other related articles on the PHP Chinese website!