PHP8.1新特性之First-class Callable Syntax
hi,我是温新,一名PHPer
First-Class callables是一种引用闭包和函数的新方法。
php8.1之前版本
<?php
$closure = Closure::fromCallable('array_merge');
print_r($closure([1,2], [3,4]));
// Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
该案使用Closure
将array_merge
转成一个闭包形式使用。
php8.1版本
<?php
$closure = array_merge(...);
print_r($closure([1,2], [3,4]));
// Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
$closure = strlen(...);
$str = 'hello';
print_r($closure($str));
保持对技术的热忱
就是对自己最好的驱动
请登录后再评论