The function that can be created without any specific name and used as an input argument in the PHP script, is known as anonymous function. These functions are implemented using Closure class. The process of assigning an anonymous function to a variable is same as any other assignment syntax. By passing a variable from parent scope to the use language construct, an anonymous function from child scope, can inherit the variable.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
An anonymous function does not carry any name:
function($arg1,$arg2,….,$argN){ //The definition for the anonymous function };
There are various objectives which can be achieved by using anonymous function in developing an effective PHP coding. An anonymous function exhibit different functionalities based on different type of use case for which the function is being used.
Five major use cases are given below:
Anonymous function can be used to assign values to variables. It follows same syntax as like other assignment operation.
Example:
The below code snippet is used to assign the given input value to an output value and print the value using the output variable.
Code:
Output:
The given input values of type string and integer are printed through the anonymous function call as shown below:
The feature of defining anonymous function plays an important role in creating an inline callback function.
In this case the anonymous function can be passed to another function as an input argument.
The below code is written to define a callback function preg_replace_callback.
Having an anonymous function as one of its input paramters.
Code:
Output:
On execution of the PHP script, the callback function is triggered and the output from the anonymous function is printed on the output window as shown below:
Anonymous function can be used to inheriting variable from parent scope.
This use case does not support super global variables, $this variable or any parameter variable having the same name.
Example:
Code:
Output:
The resultant output from the above code is produced as shown below:
For the PHP version 5.4 onwards, in case of declaration any class, the class is bound to anonymous function feature by default. This makes the variable ‘$this’ available within the scope of any anonymous function defined within the class.
Example:
Code:
Classfunction(); $function(); ?>
Output:
The dump information of the object from the class ‘AnonymousClass’ is printed on the output window as shown below:
On creation of an object, if a closure is instantiated from the scope of the same object and is registered, it creates a circular reference which results in prevention to immediate destruction of the object. Application of static anonymous function can enable the script to overcome the delay.
The comparative analysis of usage of regular anonymous function and static anonymous function is demonstrated by the below example.
Example:
Case 1: Without using static anonymous function
Code:
AnonymousVar = function () { }; } public function __destruct() { echo "Destruction function is called"; } } new TrialClass; echo "After the object is being defined"; echo "\n"; ?>
Output:
Case 2: Including static anonymous function
Code:
closure = self::createClosure(); } public static function createClosure() { return function () { }; } public function __destruct() { echo "Destruction function is called"; } } new TrialClass; echo "\n"; echo "\n"; echo "After the object is being defined"; echo "\n"; echo "\n"; ?>
Output:
The above is the detailed content of PHP Anonymous Function. For more information, please follow other related articles on the PHP Chinese website!