javascript - PHP closure functions: why does a closure have to be an anonymous function? -


A lambda or anonymous function is a function without only one name.

Example

$ lambda = function ($ a, $ b) {echo $ a + $ b; };

A closing is a function whose variable parameters are not specified in the list. PHP 5.3+ These variables are used after the keyword and from that time the definition of the function of the link function has been performed by the value or context.

For example

  $ foo = 'hello'; Use $ Close = function () ($ Foo) {echo $ foo; }; $ Off (); In Javascript, all functions are closed in the sense that when the functions are nested then there is a radius series, for example 1 can be used in the internal function. Its local variables, 2. It is known as its closure scope, local variables of the function in its closing scope, 3. Local variables of closing scope of closing scope, known as the scope of closing of the scope of closing Goes ..., and so on until we can not even use it. Global variables which are defined in most scope, the variables declared with the verb in the internal scope make them appear in outer scope, otherwise if any internal variable is similar to an external area but it is not declared with var Variable scope uses variables.  
  var zero = 0; // global scope function F1 () {var one = 1; // closing scope of the third level of the F4 function F2 () {var 2 = 2; // Third level closing scope of F4 function F3 () {var three = 3; // The first level closing scope of the F4 function is F4 () {var 4 = 4; // local area of ​​f4 console.log (zero + one + two + three + four); } F4 (); } F3 (); } F2 (); } F1 ();  

This is equivalent to the following PHP code:

  & lt ;? Php $ zero = 0; // Global Scope $ F1 = Use function () ($ zero) {$ one = 1; // F4 $ F2 = Use the scope to close the third level of the function ($ zero, $ one) {$ two = 2; // Function of second level closing of F4 $ F3 = function () use ($ zero, $ one, $ 2) {$ three = 3; // F4 $ F4 = Use the first-level closing scope of the function ($ zero, $ one, $ 2, $ three) {$ 4 = 4; // F4 local area $ zero + $ one + $ two + $ three + $ 4 echo; }; $ F4 (); }; $ F3 (); }; $ F2 (); }; $ F1 (); ? & Gt;  

It's all.

So my question comes here. Why should the lambda function be closed in PHP? In javascript, every function is closed, but the lambda function is not required. So why can not we do the following in PHP?:

  $ foo = 'hello'; Use the function closed ($ foo) {echo $ foo; } // PHP closes a syntax error here;  

(Fatal error: syntax error, unexpected T_USE, expected of ';' or '{'})


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -