Detailed explanation of PHP closure function_php skills
Anonymous function is also called a closure function (closure allows the creation of a function without specifying a function, most often used as the value of a callback function parameter.
The closure function does not have a function name. You can directly pass in the variable in function(). When using it, the defined variable will be treated as a function
$cl = function($name){ return sprintf('hello %s',name); } echo $cli('fuck')`
Call directly through the variable name defined as an anonymous function
echo preg_replace_callback('~-([a-z])~', function ($match) { return strtoupper($match[1]); }, 'hello-world');`
use
$message = 'hello'; $example = function() use ($message){ var_dump($message); }; echo $example(); //输出hello $message = 'world'; //输出hello 因为继承变量的值的时候是函数定义的时候而不是 函数被调用的时候 echo $example(); //重置为hello $message = 'hello'; //此处传引用 $example = function() use(&$message){ var_dump($message); }; echo $example(); //输出hello $message = 'world'; echo $example(); //此处输出world //闭包函数也用于正常的传值 $message = 'hello'; $example = function ($data) use ($message){ return "{$data},{$message}"; }; echo $example('world');
example
class Cart{ //在类里面定义常量用 const 关键字,而不是通常的 define() 函数。 const PRICE_BUTTER = 1.00; const PRICE_MILK = 3.00; const PRICE_EGGS = 6.95; protected $products = []; public function add($product,$quantity){ $this->products[$product] = $quantity; } public function getQuantity($product){ //是否定义了 return isset($this->products[$product])?$this->products[$product]:FALSE; } public function getTotal($tax){ $total = 0.0; $callback = function($quantity,$product) use ($tax , &$total){ //constant 返回常量的值 //__class__返回类名 $price = constant(__CLASS__."::PRICE_".strtoupper($product)); $total += ($price * $quantity)*($tax+1.0); }; //array_walk() 函数对数组中的每个元素应用用户自定义函数。在函数中,数组的键名和键值是参数 array_walk($this->products,$callback); //回调匿名函数 return round($total,2); } } $my_cart = new Cart(); $my_cart->add('butter',1); $my_cart->add('milk',3); $my_cart->add('eggs',6); print($my_cart->getTotal(0.05));
The above is the relevant content about PHP closure function. I hope it will be helpful to everyone's learning.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use anonymous functions and closures in PHP Anonymous functions and closures are powerful and commonly used features in PHP. They allow you to flexibly define and use functions in your code, which is especially useful when dealing with callback functions, event handlers, and asynchronous programming. This article will introduce how to use anonymous functions and closures in PHP, and provide some sample code to help readers understand better. 1. The definition and use of anonymous functions Anonymous functions, as the name suggests, are functions without names. It can be accessed via the keyword "function" and a pair of small

In the PHP language, an anonymous function is also called a closure (Closure). It is a function that can be defined and used at runtime without naming it in advance. Compared with ordinary functions, anonymous functions can be used directly as a variable and passed to other functions, and at the same time, they can access the variable values of their environment. The syntax format of the anonymous function is as follows: $function_name=function($parameter){//...functioncode

How to use PHP closures, generators and reflection technology to simplify code complexity Introduction: In daily development, we often encounter problems of dealing with cumbersome logic and redundant code. PHP provides us with some powerful features, such as closures, generators and reflection technology, which can help us simplify code complexity and improve development efficiency. This article will introduce how to use these techniques to simplify your code and explain it through specific code examples. Use closures to simplify logic. A closure is an anonymous function that can be used outside the function.

Practical methods for mastering PHP closures, generators, and reflection technologies require specific code examples. In PHP programming, closures, generators, and reflection technologies are three very important and useful technologies. This article will introduce the basic concepts and usage of these three technologies, and provide specific code examples to help readers better understand and apply them. 1. Closure A closure is a special function that has the ability to access the context when it was created, even if the context no longer exists. Closures are defined using the keyword use. Here is a simple example showing closures

To deeply understand the underlying principles of PHP closures, generators, and reflection technologies, specific code examples are required. In PHP programming, closures, generators, and reflection technologies are very important and commonly used features. Understanding their underlying principles can help us use them better and apply them more flexibly in actual development. 1. The underlying principle of closure Closure means that the variables in the external scope of a function can be accessed within a function. Even if the function is called outside the function, these variables can still be accessed. The underlying principle: When PHP implements a closure, it will create a

How to use PHP's closures, generators and reflection technologies for efficient programming Introduction: PHP is a scripting language widely used in web development, and closures, generators and reflection technologies are powerful and flexible features in PHP. By skillfully applying these techniques, developers can write code that is more efficient and easier to maintain. This article will introduce the concepts of closures, generators, and reflection techniques in detail, and provide specific code examples to help readers better understand and apply these techniques. 1. Closure Closure means that it can be self-contained (

Detailed explanation of closure functions and common problems in PHP In PHP, a closure function is a special function type that allows functions to be passed and returned as parameters, and external variables can be accessed inside the function. In this article, we will take a closer look at closure functions in PHP and their common problems. What is a closure function? A closure function is an anonymous function that has access to variables within the scope of its definition. Normally, the scope of a function definition is limited to the function itself, but a closure function can access variables within the scope of the definition, even after the function has finished executing.

How to use PHP closures, generators, and reflection techniques to improve code maintainability Introduction: Maintainability is a very important factor in the software development process. Maintainable code can be easily modified, extended, and debugged, making projects more flexible and robust. This article will introduce how to improve the maintainability of code by using closures, generators and reflection techniques in PHP, and illustrate it through specific code examples. 1. Use of closures Closure is a function that can capture context variables. It can be used to achieve more flexible code structure.
