Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Interpretation and implementation of PHP closure

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on the explanation and implementation of PHP closures, which interested friends may wish to take a look at. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the explanation and implementation of PHP closures.

Anonymous function

When it comes to closures, you have to think of anonymous functions, also known as closure functions (closures). It seems that the implementation of PHP closures mainly depends on it. To declare an anonymous function is as follows:

$func = function () {}; / / with Terminator

As you can see, the anonymous function does not have a name, so if you want to use it, you need to return it to a variable. Like ordinary functions, anonymous functions can declare parameters, and the calling method is the same:

$func = function ($param) {echo $param;}; $func ('some string'); / / output: / / some string

By the way, before PHP introduced closures, there was a function that could create anonymous functions: create function, but the code logic could only be written as strings, which looked obscure and difficult to maintain, so few people used it.

Implement closure

An anonymous function is passed in as an argument in an ordinary function, or it can be returned. This implements a simple closure.

Here are three examples

/ / example 1 / / define an anonymous function in the function and call it function printStr () {$func = function ($str) {echo $str;}; $func ('some string');} printStr (); / / example 2 / / return the anonymous function in the function and call it function getPrintStrFunc () {$func = function ($str) {echo $str;}; return $func } $printStrFunc = getPrintStrFunc (); $printStrFunc ('some string'); / / example 3 / / pass the anonymous function as a parameter and call it function callFunc ($func) {$func (' some string');} $printStrFunc = function ($str) {echo $str;}; callFunc ($printStrFunc); / / you can also pass the anonymous function directly. If you know js, you may be familiar with callFunc (function ($str) {echo $str;})

Keyword that connects closures to external variables: USE

Closures can hold some variables and values in the context of the code block. PHP by default, anonymous functions cannot call the context variable of the code block in which they reside, but rather by using the use keyword.

Let's take another example:

Function getMoney () {$rmb = 1; $dollar = 6; $func = function () use ($rmb) {echo $rmb; echo $dollar;}; $func ();} getMoney (); / / output: / / 1 / / error, dorllar variable not found

As you can see, dollar is not declared in the use keyword, so you can't get it in this anonymous function, so you should pay attention to this problem in development.

One might wonder whether it is possible to change contextual variables in anonymous functions, but I find that it is not possible:

Function getMoney () {$rmb = 1; $func = function () use ($rmb) {echo $rmb; / / add the value of $rmb to 1$ rmb++;}; $func (); echo $rmb;} getMoney (); / / output: / / 1 / / 1

Ah, it turns out that what use refers to is just a copy of the variable. But I want to fully reference the variable instead of copying it.

To achieve this effect, you can actually add an & symbol to the variable:

Function getMoney () {$rmb = 1; $func = function () use (& $rmb) {echo $rmb; / / add the value of $rmb to 1$ rmb++;}; $func (); echo $rmb;} getMoney (); / / output: / / 1 / / 2

OK, so that anonymous functions can refer to variables in the context. If you return an anonymous function to the outside world, the anonymous function will hold the variables referenced by use, and the outside world will not get these variables, so the concept of 'closure' may be clearer.

Change the above example according to the description:

Function getMoneyFunc () {$rmb = 1; $func = function () use (& $rmb) {echo $rmb; / / add the value of $rmb to 1$ rmb++;}; return $func;} $getMoney = getMoneyFunc (); $getMoney (); $getMoney (); $getMoney (); / / output: / / 1 / / 2 / / 3

Summary

The features of PHP closures are not a big surprise. In fact, similar or even much more powerful functions can be achieved with CLASS, let alone on a par with js closures. We can only look forward to the improvement of PHP closure support in the future. However, anonymous functions are useful, for example, when using functions such as preg_replace_callback, you don't have to declare callback functions externally.

At this point, I believe you have a deeper understanding of "the interpretation and implementation of PHP closures". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report