Detailed explanation of use keyword usage in php

小云云
Release: 2023-03-22 12:14:01
Original
19125 people have browsed it

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');
Copy after login

// 输出 hello php
function test(){
    $word = 'world';
    $func = function($para)use(&$word){ // 引用传值
        echo $para ." ".$word;
    };
    $word = 'php';
    return $func;
}
$func = test();
$func('hello');
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!