diff --git a/_posts/03-02-01-Programming-Paradigms.md b/_posts/03-02-01-Programming-Paradigms.md index 4754ca0..f0c4d77 100644 --- a/_posts/03-02-01-Programming-Paradigms.md +++ b/_posts/03-02-01-Programming-Paradigms.md @@ -18,23 +18,72 @@ interfaces, inheritence, constructors, cloning, exceptions, and more. ### Functional Programming -PHP has had support for anonymous functions and closures since PHP 5.3: +PHP supports first-class functions. It is possible to define a new function and assign it to a variable name and built-in functions +can be referenced and called dynamically. Functions can be passed as arguments to other functions (Higher-order functions) and function +can return other functions. New anonymous functions (also known as Closures) are present since PHP 5.3 (2009). {% highlight php %} $min + */ +function criteria_greater_than($min) +{ + return function($item) use ($min) + { + return $item > $min; + }; +} + +$input = array(1, 2, 3, 4, 5, 6); +$output = array_filter($input, criteria_greater_than(3)); + +print_r($output); // items > 3 +{% endhighlight %} + +Early binding is used by default for importing `$min` variable into the created function. For true closures with late binding one should use +a reference when importing. This can be used with some templating or input validation libraries, where anonymous function is defined to capture +out-of-scope variables and access them later when the anonymous function is evaluated. + PHP 5.4 added the ability to bind closures to an object's scope and also improved support for callables such that they can be used interchangeably with anonymous functions in almost all cases. * [Read about Anonymous Functions][anonymous-functions] * [Read about the Closure class][closure-class] +* [More details in the Closures RFC][closures-rfc] * [Read about Callables][callables] * [Read about dynamically invoking functions with `call_user_func_array`][call-user-func-array] @@ -58,3 +107,4 @@ available as `__call()` and `__callStatic()`. [reflection]: http://www.php.net/manual/en/intro.reflection.php [traits]: http://www.php.net/traits [call-user-func-array]: http://php.net/manual/en/function.call-user-func-array.php +[closures-rfc]: https://wiki.php.net/rfc/closures