Home  >  Article  >  Backend Development  >  Detailed explanation of use keyword usage in php

Detailed explanation of use keyword usage in php

小云云
小云云Original
2018-03-27 12:04:5319113browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn