Functional Programming

博客介绍了PHP支持一等函数,函数可赋值给变量、作为参数传递及返回其他函数,即高阶函数,常用于实现策略模式。还讲解了闭包,它是能访问外部作用域变量的匿名函数,可绕过变量作用域限制。此外,介绍了call_user_func_array函数的使用。

PHP supports first-class functions, meaning that a function can be assigned to a variable. Both user-defined and built-in functions can be referenced by a variable and invoked dynamically. Functions can be passed as arguments to other functions and a function can return other functions (a feature called higher-order functions).

 

Higher-order Functions:

The most common usage of higher-order functions is when implementing a strategy pattern. The built-in array_filter() function asks both for the input array (data) and a function (a strategy or a callback) used as a filter function on each array item.

<?php
$input = array(1, 2, 3, 4, 5, 6);

// Creates a new anonymous function and assigns it to a variable
$filter_even = function($item) {
    return ($item % 2) == 0;
};

// Built-in array_filter accepts both the data and the function
$output = array_filter($input, $filter_even);

// The function doesn't need to be assigned to a variable. This is valid too:
$output = array_filter($input, function($item) {
    return ($item % 2) == 0;
});

print_r($output);

 

Closure:

A closure is an anonymous function that can access variables imported from the outside scope without using any global variables. Theoretically, a closure is a function with some arguments closed (e.g. fixed) by the environment when it is defined. Closures can work around variable scope restrictions in a clean way.

<?php
/**
 * Creates an anonymous filter function accepting items > $min
 *
 * Returns a single filter out of a family of "greater than n" filters
 */
function criteria_greater_than($min)
{
    return function($item) use ($min) {
        return $item > $min;
    };
}

$input = array(1, 2, 3, 4, 5, 6);

// Use array_filter on a input with a selected filter function
$output = array_filter($input, criteria_greater_than(3));

print_r($output); // items > 3

  

call_user_func_array:

call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数

call_user_func_array ( callable $callback , array $param_arr ) : mixed

callback

被调用的回调函数。

param_arr

要被传入回调函数的数组,这个数组得是索引数组。

<?php
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"));

// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>

The Results would be:

foobar got one and two
foo::bar got three and four

  

转载于:https://www.cnblogs.com/victorchen/p/10813652.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值