Home >Backend Development >PHP Tutorial >PHP core features anonymous functions

PHP core features anonymous functions

藏色散人
藏色散人forward
2019-11-01 13:43:282739browse

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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete