Volume of variables with function inside function?

In Python, you can get the following:

def foo(param1, param2): def bar(): print param1 + param2 bar() 

I am having some difficulties with this behavior in PHP. I expect this to work as follows:

 function foo($param1, $param2) { function bar() { echo $param1 + $param2; } bar(); } 

But it fails. So I read some about closing (this is called closing, isn't it? It's in Python that I know). And in the php documentation about anonymous functions (which they said were implemented as closure), they tell you to use use () expression like this:

 function foo($param1, $param2) { function bar() use($param1, $param2) { echo $param1 + $param2; } bar(); } 

But this still fails. So I changed it to a PHP-anonymous function a-like as follows:

 function foo($param1, $param2) { $bar = function() use($param1, $param2) { echo $param1 + $param2; }; $bar(); } 

It really works, but it looks very ugly. Am I missing something? Can i improve this? Or do I just need to use the ugly way?

(I'm not looking for a discussion on whether closures are useful)

+7
source share
3 answers

I could not find the syntax of function bar() use($param1, $param2) on the manual page you linked to, only the ugly version that works (which is really anonymous). I think you have to use it.

In the second example, bar not a close. To create closure in PHP, you must use the ugly use or Closure class . Each function will create its own local scale, but the closure is not automatic.

PHP seems to have an odd definition of the term โ€œclosure,โ€ as you probably noticed when you read manual . They define it as a synonym for "anonymous function":

Anonymous functions, also known as closing (...)

Confused, right? They later explain that you need the use keyword if you want to inherit the parent scope:

Closing can also inherit variables from the parent scope. Any such variables must be declared in the function header.

The PHP Wiki rfc page on closure gives us some tips on why closure was implemented this way:

The PHP scope is significantly different from the concept defined by other languages. Combine this with variable variables ($$ var), and it becomes clear that automatic detection of variables from the external reference area inside is not possible. In addition, since, for example, global variables are not visible inside functions by default, automatically creating the accessibility of the parent area, using the existing concept of the PHP language.

+4
source

In PHP, you cannot access staging areas. You have only a local scope and global scope. This is an artifact of the rules of the vicious area of โ€‹โ€‹PHP, which are completely opposite to the rules of a reasonable language area.

+1
source

In PHP, the declared function name ( function bar () ) declares a function in the global scope. So, in the first example, when you run your foo function, it will define the bar function in the global scope, and then everyone can access the bar function as if you declared it outside of foo . Functions and variables are separate in PHP. For these functions, there is only one area: global; whereas you have local variables, there are no functions like on-premises functions.

In other words, putting inside foo is an illusion; Declaring a specified function always declares it globally; placing it inside foo simply delays the execution time of the declaration (thus, is delayed when it is defined) until foo executed.

Closing is what you need. Other answers showed how to do this.

+1
source

All Articles