php匿名函数是什么?
官网是这样定义的:匿名函数(Anonymous functions),也叫闭包函数(closures),在 php 5.3 中被引入,允许临时创建一个没有指定名称的函数。最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。匿名函数目前是通过 Closure 类来实现的。
那么我们通过上面这段话可以理解出什么呢?
1、匿名函数 = 闭包函数(网上还有的说,匿名函数和闭包是两种概念,不过 php 将其视作相同的概念,提到闭包就理解成匿名函数,反之亦然)
2、匿名函数其实就是没有名称的函数,使用的句法与普通函数相同,但闭包和匿名函数其实是伪装成函数的对象(Closure 类的实例)。它可以赋值给变量,也可以当做参数传递。
3、可以调用也可以传入参数,匿名函数特别适合作为函数或方法的回调使用。
那么它的使用场景有哪些呢?
Example #1 创建一个匿名函数当做回调函数 callback 参数的值<?php
// 比如 preg_replace_callback 函数执行一个正则表达式搜索并且使用一个回调进行替换
echo preg_replace_callback('~-([a-z])~', function ($match) {
return strtoupper($match[1]);
}, 'hello-world'); // 记得写分号
// helloWorld
// 比如 array_map 将函数作用到数组中的每个值上
$numberPlusOne = array_map(function($number) {
return $number + 1;
}, [1, 2, 3]);
print_r($numberPlusOne); // Array ( [0] => 2 [1] => 3 [2] => 4 )
Example #2 闭包函数赋值给一个变量<?php
$greet = function ($name) {
printf("Hello %s\r\n", $name);
};
// $greet('World');
// $greet('PHP');
// var_dump($greet instanceof Closure); // 检查一个函数是否是闭包
// php还有一个 create_function 函数也可以创建匿名函数的
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
Example #3 从父作用域继承变量<?php
// 继承
$message = 'hello';
$example = function () use ($message) {
var_dump($message);
};
var_dump($example instanceof Closure); // true
echo $example(); // hello
// 通过引用继承
$message = 'hello';
$example = function () use (&$message) {
var_dump($message);
};
$message = 'world';
echo $example(); // world
Example #4 call_user_func_array() 和 call_user_func() 方法<?php
// call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
function bar($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2\n";
}
}
// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two")); // foobar got one and two
// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four")); // foo::bar got three and four
// call_user_func — 把第一个参数作为回调函数调用
$rs = call_user_func(function (...$params) {
return func_get_args();
}, 1, 2, 3);
var_dump($rs); // [1,2,3]
我们看下官方文档中怎么使用 Closures 和作用域/**
* 基本购物车类.
* Class Cart
*/
class Cart
{
// 定义商品价格
const PRICE_BUTTER = 1.00;
const PRICE_MILK = 3.00;
const PRICE_EGGS = 6.95;
// 购物车数组
protected $products = array();
/**
* 添加商品和数量.
* @param $product 商品
* @param $quantity 数量
*/
public function add ($product, $quantity)
{
$this->products[$product] = $quantity;
}
/**
* 获取总价.
* @param $tax 销售税
* @return float
*/
public function getTotal ($tax)
{
$total = 0.00; // 默认总价 0.00 元
// 闭包函数 use 使用外部变量,“&” 表示引用继承变量 $total
$callback = function ($quantity, $product) use ($tax, &$total) {
$pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); // 动态获取常量的值
$total += ($pricePerItem * $quantity) * ($tax + 1.0); // 商品价格 *(1 + 增值税率)= 商品税后价格
};
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);
// 打出总价格,其中有 5% 的销售税.
print $my_cart->getTotal(0.05) . "\n";
// 最后结果是 54.29