How to create closure function of PHP function?

WBOY
Release: 2024-04-10 10:33:02
Original
830 people have browsed it

A closure function in PHP is an anonymous function nested in another function that can access the variables of the external function. The use keyword can be used to access external variables in the closure function, which can be applied in actual scenarios where discounts need to be applied to each element in the list.

PHP 函数的闭包函数如何创建?

Closure function in PHP function

A closure function is an anonymous function nested in another function. Variables of external functions can be accessed even if the external function has returned. Closure functions are very useful in PHP because they allow creating reusable blocks of code and maintaining access to the external environment.

Creating a Closure Function

To create a closure function in PHP, use thefunctionkeyword, followed by an optional name and parameters List:

$closure = function($arg1, $arg2) { // 函数体 };
Copy after login

The variable scope rules in closure functions are as follows:

  • A closure function can access all variables declared in the scope where it is defined, including local variables and parameters. .
  • A closure function cannot access variables declared outside the function in which it is nested, unless these variables are explicitly declared asusein the closure function.

Useusekeyword

usekeyword is used to introduce external variables into the closure function . For example:

function outerFunction($arg1) { $outerVar = '外部变量'; $closure = function() use ($arg1, $outerVar) { // 闭包函数可以访问 $arg1 和 $outerVar }; }
Copy after login

Practical Case

Suppose you need to create a function that applies a discount to each element in a list. To do this, you can create a closure function to calculate the discount amount:

function applyDiscount($list, $discountPercentage) { // 创建闭包函数来计算折扣 $discountClosure = function($item) use ($discountPercentage) { return $item - ($item * ($discountPercentage / 100)); }; // 将折扣闭包函数应用于列表中的每个元素 return array_map($discountClosure, $list); }
Copy after login

Full code:

function outerFunction($arg1) { $outerVar = '外部变量'; $closure = function() use ($arg1, $outerVar) { // 闭包函数可以访问 $arg1 和 $outerVar echo "arg1: $arg1
"; echo "outerVar: $outerVar
"; }; // 调用闭包函数 $closure(); } applyDiscount([10, 20, 30], 10); // [9, 18, 27]
Copy after login

The above is the detailed content of How to create closure function of PHP function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!