PHP value callback type and anonymous function (closure)

Source: Internet
Author: User
Tags closure

Callback, callable type

Callback type callback can be developed using the callable type from PHP5.4 onwards.
This document uses callback type information on the same grounds.

Some functions such as call_user_func () or usort () can accept user-defined callback functions as parameters, and the callback function can be more than just a simple function.
It can also be an object's method, including static class methods.

Passed???????????

PHP is a function that has been passed in string form. Keyi9 uses any built-in or user-defined functions, except for language constructs such as: Array, echo, empty, eval, exit, Isset, list, print, unset.

A method of an instantiated object is passed as an array. The small label 0 contains the object, and the subscript 1 contains the method name.
The projected and private methods can be accessed within the same class.

A static class method can also be passed without instantiating an object of that class. As long as the class name is included in subscript 0 instead of the object. From PHP5.2.3, you can also pass "type:: Method Name"

In addition to ordinary user-defined functions, you can pass an anonymous function to the callback function.

Call_user_func ();

anonymous functions

The anonymous function (Anonymous functions), also called the closure function (closures), allows temporary creation of a function without a specified name.
The value most often used as the callback function (callback) parameter. Of course, there are other applications as well.

anonymous function Example

echo Preg_replace_callback (' ~-([A-z]) ~ ', function ($match) {
return Strtoupper ($match [1]);
}, ' Hello-world ');
Output HelloWorld

The closure function can also be used as the value of a variable. PHP automatically transforms this expression into an object instance of the built-in class closure. Put a closure
An object assigns a value to a variable in the same way as a normal variable, preferably with a semicolon;

Example of an anonymous function variable assignment

$greet = function ($name)
{
printf ("Hello%s\r\n", $name);
};

$greet (' World ');
$greet (' PHP ');

Call_user_func ($greet);

Closures can inherit variables from the parent scope.
Any such variables should be passed in using the use language structure. php7,1, you cannot pass in such variables.

Superglobals. $this or duplicate the parameter.

These variables must be declared on the head of a function or class, and inheriting variables from the parent scope is different from using global variables.
Global variables exist in a global scope,
Regardless of the function that is currently executing, the parent scope of the closure is the function that defines the closure (not necessarily the function that called it).
Examples are as follows:

PHP value callback type and anonymous function (closure)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.