You know something about closures in PHP. PHP: Anonymous Functions

Wikipedia says that an anonymous function is a function that can exist without an identifier. Sounds pretty interesting! In this lesson, I will show you several examples of how you can create and cause the function with non-standard methods.

Let's start at once with examples:

Function Test ($ var) (Echo "This is $ var";)

This is a very simple feature. Now, in addition to the usual call, we can run this feature using a variable that stores the name of this function. Approximately like this:

$ f \u003d "test"; $ F ("Variable Function");

If you start the code, you will see the message this is variable function. To say, to say, the processing of single quotes is triggered faster than double.

We can apply this technique in the OOP. Example with php.net:

Class Foo (FUNCTION VARIABLE () ($ name \u003d "bar"; $ this -\u003e $ name (); // this Calls The Bar () Method) Function Bar () (Echo "This Is Bar";)) $ foo \u003d new foo (); $ FUNCNAME \u003d "VARIABLE"; $ foo -\u003e $ funcname (); // This Calls $ Foo-\u003e Variable ()

This concept is quite interesting. It can be used to implement the callbacks, feature tables and so on.

Now I will try to explain to you what anonymous features are:

$ input \u003d array (1, 2, 3, 4, 5); $ Output \u003d Array_Filter ($ Input, Function ($ V) (RETURN $ V\u003e 2;));

function ($ V) (Return $ V\u003e 2;) This is an anonymous function. We can also assign it to the variable to use in the future.

$ max \u003d function ($ V) (Return $ V\u003e 2;); $ input \u003d array (1, 2, 3, 4, 5); $ Output \u003d Array_Filter ($ Input, $ MAX);

Now let's get acquainted with the new USE keyword. We will write another anonymous feature for this (works with PHP 5.3):

$ max_comp \u003d function ($ MAX) (RETURN FUNCTION ($ V) USE ($ MAX) (RETURN $ V\u003e $ Max;);); $ input \u003d array (1, 2, 3, 4, 5); $ Output \u003d Array_Filter ($ Input, $ Max_comp (2));

In this example, we use a closure effect using a USE keyword. This technique allows anonymous feature to access external variables. Did the breeze of procedural programming?

Here is another example simpler:

$ string \u003d "Hello World!"; $ closure \u003d function () USE ($ String) (Echo $ String;); $ closure ();

As I said, the variables that we want to use (from the global scope) in such functions must be transmitted via use. It is important to note that only the value is transmitted by default, so if you want to change the contents of the transmitted variable and want it to change outside an anonymous function, transfer the value to the address:

$ x \u003d 1; $ closure \u003d function () use (& $ x) (++ $ x;); Echo $ x "
"; $ closure (); echo $ x."
"; $ closure (); echo $ x."
";

In this example, our method changes the content of the $ x variable with each call anonymous function. If we had not passed the address of the variable, and we would have the variable itself three 1.

The true beauty of the "closure" is that it does not overturn global namespace. As soon as an anonymous function performed the action, all variables used in it are automatically destroyed.

Hello everyone and today we will talk about what is anonymous functions in PHP And where to use them.

Anonymous functionsor differently lambda functions - These are functions that have no name.

An example of the usual function

FUNCTION FUNAME ($ TXT) (
Echo "MY TEXT:" $ TXT;
}

$ FUNC \u003d "FUNAME";
$ FUNC ("MY TEXT");
?>

We have a feature that takes one parameter. Then we write the name of the function to the variable and call it. Nothing unusual. IN OOP This style will be more justified.

Class MyClass (
FUNCTION BAR () (
// the code
}
}

$ class \u003d new myclass ();
$ FUNC \u003d "BAR";
$ Class -\u003e $ FUNC ();
?>

Anonymous functions

If you are familiar with anonymous features from the language Javascript., then you understand why they are needed, and if not, then read on.

To cause our anonymous function somewhere on the program code, we can assign it to a variable

$ FUNC \u003d FUNCTION () (
// the code
};

$ func ();
?>

Note that at the end it is worth ; because We assign a variable value equal to the function. But all the salt is not in this, but in the fact that we can use these functions as callback functions. For example:

$ Val \u003d Array_Filter ($ Input, Function ($ V) (Return $ V\u003e 2;));
?>

Use keyword

Again, if you know Javascript.This will not be a problem for you. Keyword Use Allows you to use the reception closure. With it, we can reach external variables.

$ MX \u003d FUNCTION ($ HIGH_VAL) (
RETURN FUNCTION ($ VAL) USE ($ High_Val) (
Return $ Val\u003e $ High_Val;
};
};

$ i \u003d array (1, 2, 3, 4, 5, 6, 7);
$ O \u003d Array_Filter ($ INPUT, $ MAX_COMP (2));
?>

Here we are inside our lambda functions We use a global variable high_Val. So that it was possible, we must after the word function. and parameters in parentheses write use And pass the name of the external variable there, which we want to use inside the function code.

However, we will not be able to change the importance of our global variable in this way. To do this, you need to specify a sign before its name. &

$ name \u003d "brian";
$ setName \u003d Function ($ n) Use (& $ Name) (
$ name \u003d $ n;
};

$ setname ("DAVID");
Echo $ Name;
?>

Conclusion

So today you have learned what is anonymous functions in php and how to use them. Good luck!

The function can be assigned to a variable, as well as the usual value. To do this, the function name must be assigned to a variable in the form of a string, but without specifying parentheses:

\\ n ";) $ My_func \u003d" foo "; // Now we can run the Foo () function using the $ My_Func variable, which stores the name of the specified function as a string $ My_Func (); // call the foo () function ?\u003e

Such a PHP concept has the name "function variables". It lies in the fact that if you add to the variable at the end of the round brackets, then the PHP interpreter will check first, there is no function with the name of equal meaning variable and if there is such a function - it will execute it.

So, as shown in the example above, you can only do with the functions of certain users. Built-in language structures and functions, such as ECHO, UNSET (), ISSET () and other similar things cannot be directly assigned to variables in the same way. But you can make your wrapper function (wrapper) so that the built-in language structures can work like user functions.

Anonymous functions

Anonymous function - A function that does not have its own name, sometimes you can meet another name of such functions - lambda function. Anonymous features can be transmitted to other functions as arguments or assign a variable as conventional values:

Note the example, at the end of the definition of the function there is a semicolon, since an anonymous function is inherently value, and we assign the value of the variable, then at the end, as for ordinary instructions, the point is plated.

Anonymous functions differ from named by the fact that they are created only at the moment when execution comes to them, so you can use them only after their definition: