What is the use of use in php anonymous function
Anonymous functionThe function of use is to inherit variables from the parent scope.
The following example is the most common usage. If use is not used, the variable $msg will not be found in the function.
<?php $msg = [1,2,3]; $func = function()use($msg){ print_r($msg); }; $func(); ?>
Run output
Array ( [0] => 1 [1] => 2 [2] => 3 )
About the timing of inheriting variables
The behavior of inheriting variablesIs it generated when the function is defined or Generated when a function is called? Let’s adjust the order of the code in the above example and place $msg after the function definition.
<?php $func = function()use($msg){ print_r($msg); }; $msg = [1,2,3]; $func(); ?>
PHP Notice: Undefined variable: msg in /search/ballqiu/c.php on line 4
msg is defined, so $msg is the un
defined variable when the function is run.
About using reference in usePassing valueWe know that if you use reference to pass value in use of an anonymous function, then Changes to parameter values in anonymous functions will also affect the corresponding external variables. For example, the following example:
<?php $msg = [1,2,3]; $func = function()use(&$msg){ $msg[0]++; print_r($msg); }; $func(); print_r($msg); ?>
Run output
Array ( [0] => 2 [1] => 2 [2] => 3 ) Array ( [0] => 2 [1] => 2 [2] => 3 )
So in any case, if you want to change the value of an external variable through an anonymous function, you must pass the value to use by reference? Look at the following example:
<?php $msg = new ArrayObject([1,2,3], ArrayObject::ARRAY_AS_PROPS); $func = function()use($msg){ $msg[0]++; print_r($msg); }; $func(); print_r($msg); ?>
Run output
ArrayObject Object ( [storage:ArrayObject:private] => Array ( [0] => 2 [1] => 2 [2] => 3 ) ) ArrayObject Object ( [storage:ArrayObject:private] => Array ( [0] => 2 [1] => 2 [2] => 3 ) )
It can be seen that if you pass a variable of type
object class, even if you do not display the use of reference passing, the variable value in the anonymous function Changes also affect external related variables.
But, the problem came again. When passing an object variable to use, is there any difference between using a reference and not using a reference? Let’s look at the example
<?php $func = function()use($msg){ echo $msg[0],"\n"; }; $msg = new ArrayObject([1,2,3], ArrayObject::ARRAY_AS_PROPS); $func(); ?>
We use reference passing instead
$func = function()use(&$msg){ echo $msg[0],"\n"; }; 运行输出1
It can be seen that when using reference passing, even if the variable lags behind the function definition, the corresponding external variable can still be found inside the function and will not appear The variable is undefined. There is still a difference between the two.
About this and use in the anonymous function in the class
<?phpclass C{
protected $_num = 0; public function mkFunc(){
$func = function(){
echo $this->_num++, "\n";
}; return $func;
} public function get(){
echo $this->_num,"\n";
}
}$obj = new C();$func = $obj->mkFunc();$func();$obj->get();?>
运行结果01
Copy after loginIt can be seen that this in the anonymous function refers to the current
object<?phpclass C{ protected $_num = 0; public function mkFunc(){ $func = function(){ echo $this->_num++, "\n"; }; return $func; } public function get(){ echo $this->_num,"\n"; } }$obj = new C();$func = $obj->mkFunc();$func();$obj->get();?> 运行结果01
, no You can find it directly if you need to use use. Still the above example, what will be the effect if you must use use?
Change mkFunc topublic function mkFunc(){ //唯一改动是此处加了use $func = function()use($this){ echo $this->_num++, "\n"; }; return $func; } 运行输出 PHP Fatal error: Cannot use $this as lexical variable
Modify it to
public function mkFunc(){ $self = $this; $func = function()use($self){ echo $this->_num++, "\n"; }; return $func; } 运行结果01
It can be seen that the effect is the same whether use is used.
The above is the detailed content of What is the use of use in php anonymous function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
