In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What are closures and anonymous functions in PHP? many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
Closures are functions that encapsulate the surrounding state at creation time. Even if the environment of the closure no longer exists, the state of encapsulation in the closure still exists.
Anonymous functions are functions that do not have a name. Anonymous functions can be assigned to variables and passed like any other PHP object. However, anonymous functions are still functions, so they can be called and parameters can be passed in. Anonymous functions are particularly suitable as callbacks to functions or methods.
Note: in theory, closures and anonymous functions are different concepts. However, PHP treats it as the same concept. So, when we refer to closures, we also refer to anonymous functions, and vice versa.
PHP closures and anonymous functions use the same syntax as ordinary functions, but closures and anonymous functions are actually objects disguised as functions (instances of the Closure class).
Create a closure
$closure = function ($name) {return sprintf ("Hello% s", $name);} echo $closure ("jerry"); / / check whether the $closure variable is a closure var_dump ($closure instanceof Closure)
The above code creates a closure object and assigns it to the $closure variable. Closures are very similar to ordinary PHP functions in that they use the same syntax, take parameters, and return values.
Note: we can call the $closure variable because the value of this variable is a closure, and the closure object implements the _ _ invoke () magic method. As long as the variable name is followed by (), PHP finds and calls the _ _ invoke () method.
Use closures
We usually use PHP closures as callbacks for functions and methods. Many PHP functions use callback functions, such as array_map () and preg_replace_callback (). For the following example, we will use array_map () to process the array, incrementing each item by 1:
$nubmers = array_map (function ($number) {return $number++;}, [1mem2 number++; 3]); var_dump ($numbers)
Additional state
The PHP closure does not automatically encapsulate the state of the application like the real javascrypt closure. We must manually call the bindTo () method of the closure object or use the use keyword to attach the state to the PHP closure.
Use the use keyword
It is more common to use the use keyword to attach closure states, so let's look at this approach first. When you use the use keyword to attach a variable to a closure, the attached variable remembers the value assigned to it at the time of attachment.
Function Car ($name) {return function ($statu) use ($name) {return sprintf ("Car% s is s", $name, $statu);} / / encapsulate the car name in a closure $car = Car ("bmw"); / / call the action of the car / / output-- > "bmw is running" echo $car ("running")
Note: multiple parameters can be passed into the closure using the use keyword, which should be separated by commas, just like the parameters of a PHP function or method.
The state of the closure is appended using the bindTo () method
Like other PHP objects, each closure instance can use the $this keyword to get the internal state of the closure. The default state of the closure object is useless, but there is a _ _ invoke () magic method and a bindTo () method.
The bindTo () method adds some interesting potential to closures. We can use this method to bind the internal state of the Closure object to other objects.
The second parameter of the bindTo () method is important, which specifies the PHP class to which the object that binds the closure belongs. Therefore, closures can access protected and private member variables in objects that bind closures.
Class TestClosure {private $name= []; private $age; private $sex; public function addPerson ($name, $personCallback) {/ / bind the closure object to the current instance $this- > name [$name] = $personCallback- > bindTo ($this, _ _ CLASS__);} public function display ($name) {foreach ($this- > name as $key = > $callback) {if ($key = $name) {/ / execute the closure object and attach the closure state to the class $callback () } echo "name: {$name}\ n"; echo "age: {$this- > age}\ n"; echo "sex: {$this- > sex}\ n";}} $person = new TestClosure (); $person- > addPerson ("jerry", function () {$this- > age = 19; $this- > sex = "man";}); $person- > display ("jerry"); / * * outputname: jerryage: 19sex: will man*/ help you after reading the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.