$str = 'Какая-то строка'; // Вариант со ссылкой use на переменную $str $foo = function ($sep) use ($str) { $out = $sep . $str . $sep; return $out; }; // Вариант с объявлением переменной $str глобальной $foo = function ($sep) { global $str; return $sep . $str . $sep; }; echo $foo('~'); 

The result of their work is identical. I understand that the use statement transfers the $ str variable to the function scope. The global operator performs the same function inside the function as use - so these two declarations can be considered synonymous?

  • Global variables are evil so it's better to use. And Global will change the variable - Naumov
  • Probably not, if I do not confuse. With global, one instance will be captured, and use will capture the context and, for example, the loop will capture different $ str - vitidev
  • @vitidev, if I understand you correctly, when using the $ str variable obtained using use in a loop, will the effect be the same as with a static variable? In other words, I can only read data from $ str, but not change it? - Edward
  • I can confuse as there in php for they do everything in their own way, but ... global captures the variable from the global scope. use captures from the local scope. global visibility is one, and local visibility is not one. For example, wrap it all up with a loop where $ str assigns a new value at each iteration — this new variable will be captured. In the case of global, the idea is always the same. all this needs to be checked - vitidev

2 answers 2

You use anonymous functions, also known as closures, which allow you to create functions that do not have specific names.

Closures can also inherit variables from the parent scope. Any such variable must be declared in the use construction. Changes inside will not be reflected on a variable from the parent scope, in order to change the variable inside and reflect changes to the parent area you must add before the start of &, then the variable will be passed by reference.

 $foo = function ($sep) use (&$str) { $out = $sep . $str . $sep; return $out; }; 

The global $ a construction says that the $ a variable is global, that is, it is synonymous with global $ a and changes within the function will be reflected in the global scope. After defining variables through global, all references to any of these variables will point to their global version.

Thus, the global instruction creates a reference to the $ GLOBALS array, so these two entries will be equivalent:

 function test() { global $a; $а = 10; } function test() { $а = &$GLOBALS['a']; $а = 10; } 

The $ GLOBALS array is accessible from anywhere in the program — including from the function body — and it does not need to be additionally declared.

For your question, I can say that you define functions in two ways is exactly the same. You simply change the way parameters are passed to the function.

PS Excessive definition of global variables threatens to complicate the search for errors in the program. This is the real way to drive the craziness of the programmer, who will then disassemble your program.

  • Thank you for an exhaustive answer! ) - Edward
  • @Edward, but if you add this code to a class method and functions will become completely different. use will capture the nearest context, and global will capture variables defined outside the class. - vitidev
  • @vitidev, it was clear to me about the use in the OOP, I was interested in the behavior of functions in a procedural style. Thank you) - Edward
  • @Edward and procedurally (there are no classes), but if this code is inside the loop, there will be a different result. - vitidev

I think the code below should make it clear that closures use copies of variable values ​​at the time of declaration (case without &), and global uses a link to the latest version. Passing by reference to closure, equivalent to using global

 <?php $d = 0; $closure = function() use($d){ return $d; }; $d = 2; echo $closure(); // выведет 0 function display(){ global $d; // return $d; } $d = 6; echo display(); // выведет 6