What is the use of use in php anonymous function

怪我咯
Release: 2023-03-11 17:50:01
Original
2037 people have browsed it

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.

Copy after login

Run output

Array ( [0] => 1 [1] => 2 [2] => 3 )
Copy after login

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.

Copy after login

Run output

PHP Notice: Undefined variable: msg in /search/ballqiu/c.php on line 4
Copy after login

It can be seen that the behavior of inheriting variables is generated when the function is defined. In the above example,

msg is defined, so $msg is the un

defined variablewhen 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:

Copy after login

Run output

Array ( [0] => 2 [1] => 2 [2] => 3 ) Array ( [0] => 2 [1] => 2 [2] => 3 )
Copy after login

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:

Copy after login

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

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

Copy after login

We use reference passing instead

$func = function()use(&$msg){ echo $msg[0],"\n"; }; 运行输出1
Copy after login

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

_num++, "\n"; }; return $func; } public function get(){ echo $this->_num,"\n"; } }$obj = new C();$func = $obj->mkFunc();$func();$obj->get();?> 运行结果01
Copy after login
It can be seen that this in the anonymous function refers to the current

object

, 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 to

public function mkFunc(){ //唯一改动是此处加了use $func = function()use($this){ echo $this->_num++, "\n"; }; return $func; } 运行输出 PHP Fatal error: Cannot use $this as lexical variable
Copy after login

Modify it to

public function mkFunc(){ $self = $this; $func = function()use($self){ echo $this->_num++, "\n"; }; return $func; } 运行结果01
Copy after login

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!

Related labels:
php
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
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!