PHP core features anonymous functions
Proposed
Before the emergence of anonymous functions, all functions needed to be named before they could be used
function increment($value) { return $value + 1; } array_map('increment', [1, 2, 3]);
Sometimes a function may only need to be used once. Using anonymous functions will make the code more concise and intuitive, and also prevent the function from being used elsewhere
array_map(function($value){ return $value + 1; }, [1, 2, 3]);
Definition and use
PHP treats closures and anonymous functions as are equivalent concepts (collectively referred to as anonymous functions in this article), and are essentially objects disguised as functions.
The essence of anonymous functions is objects, so just like objects, anonymous functions can be assigned to a variable
$greet = function(string $name){ echo "hello {$name}"; } $greet("jack") // hello jack
All anonymous functions are instances of Closure objects
$greet instanceof Closure // true
The object has no parent scope to speak of, so you need to use use to manually declare the variables used.
$num = 1; $func = function() use($num){ $num = $num + 1; echo $num; } $func(); // 2 echo $num; // 还是 1
If you want the variables in the anonymous function to take effect, you need to use reference pass-by-value
$num = 1; $func = function() use(&$num){ $num = $num + 1; echo $num; } $func(); // 2 echo $num; // 2
Starting from PHP 5.4, when using an anonymous function in a class, $this of the anonymous function will be automatically bound to the current class
class Foo { public function bar() { return function() { return $this; }; } } $foo = new Foo(); $obj = $foo->bar(); // Closure() $obj(); // Foo
If you do not want the automatic binding to take effect, you can use a static anonymous function
class Foo { public function bar() { return static function() { return $this; }; } } $foo = new Foo(); $obj = $foo->bar(); // Closure() $obj(); // Using $this when not in object context
The essence of anonymous functions
The essence of anonymous functions is the Closure object, including the following five methods
Closure { private __construct ( void ) public static bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) : Closure public bindTo ( object $newthis [, mixed $newscope = "static" ] ) : Closure public call ( object $newthis [, mixed $... ] ) : mixed public static fromCallable ( callable $callable ) : Closure }
__construct - Prevent anonymous functions from being instantiated
$closure = new \Closure(); // PHP Error: Instantiation of 'Closure' is not allowed
Closure::bindTo - Copies the current anonymous function object and binds the specified $this object and class scope. In layman's terms, it means manually binding an anonymous function to a specified object. Using this, you can extend the functionality of the object.
// 定义商品类 class Good { private $price; public function __construct(float $price) { $this->price = $price; } } // 定义一个匿名函数,计算商品的促销价 $addDiscount = function(float $discount = 0.8){ return $this->price * $discount; } $good = new Good(100); // 将匿名函数绑定到 $good 实例,同时指定作用域为 Good $count = $addDiscount->bindTo($good, Good::class); $count(); // 80 // 将匿名函数绑定到 $good 实例,但是不指定作用域,将无法访问 $good 的私有属性 $count = $addDiscount->bindTo($good); $count(); // 报错
Closure::bind - A static version of the bindTo method, with two usages:
Usage 1: To achieve the same effect as the bindTo method
$count = \Closure::bind($addDiscount, $good, Good::class);
Usage 2: Bind the anonymous function to the class (rather than the object), remember to set the second parameter to null
// 商品库存为 10 class Good { static $num = 10; } // 每次销售后返回当前库存 $sell = static function() { return"当前库存为". --static::$num ; }; // 将静态匿名函数绑定到 Good 类中 $sold = \Closure::bind($sell, null, Good::class); $sold(); // 当前库存为 9 $sold(); // 当前库存为 8
call - PHP 7 New The call method can bind and call anonymous functions. In addition to simpler syntax, the performance is also higher
// call 版本 $addDiscount->call($good, 0.5); // 绑定并传入参数 0.5,结果为 50 // bindTo 版本 $count = $addDiscount->bindTo($good, Good::class, 0.5); $count(); // 50
fromCallable - Convert a given callable function into an anonymous function
class Good { private $price; public function __construct(float $price) { $this->price = $price; } } function addDiscount(float $discount = 0.8){ return $this->price * $discount; } $closure = \Closure::fromCallable('addDiscount'); $good = new Good(100); $count = $closure->bindTo($good); $count = $closure->bindTo($good, Good::class); // 报错,不能重复绑定作用域 $count(); // 报错,无法访问私有属性
fromCallable is equivalent to
$reflexion = new ReflectionFunction('addDiscount'); $closure = $reflexion->getClosure();
One thing that needs special attention here is that whether it is a closure converted fromCallable or a closure obtained using reflection, when using bindTo, if the second If the parameter specifies the binding class, an error will be reported
Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure()
The above is the detailed content of PHP core features anonymous functions. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
