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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software