From 36a7359759ae7a1d60486a20f3a2cf4dc8670010 Mon Sep 17 00:00:00 2001 From: Goran Rakic Date: Tue, 10 Jul 2012 18:13:09 +0200 Subject: [PATCH 1/8] Extends the functional paradigm section. State the key elements of functional paradigm supported by PHP and extends the example. --- _posts/03-02-01-Programming-Paradigms.md | 58 ++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 4 deletions(-) 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 From 44576ed005b14259942c949409d65821e25080ee Mon Sep 17 00:00:00 2001 From: Goran Rakic Date: Wed, 11 Jul 2012 00:50:38 +0200 Subject: [PATCH 2/8] anonymous functions !== closures --- _posts/03-02-01-Programming-Paradigms.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_posts/03-02-01-Programming-Paradigms.md b/_posts/03-02-01-Programming-Paradigms.md index f0c4d77..e7c5f52 100644 --- a/_posts/03-02-01-Programming-Paradigms.md +++ b/_posts/03-02-01-Programming-Paradigms.md @@ -20,7 +20,7 @@ interfaces, inheritence, constructors, cloning, exceptions, and more. 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). +can return other functions. New anonymous functions (with support for closures) are present since PHP 5.3 (2009). {% highlight php %} Date: Fri, 13 Jul 2012 15:37:54 +0200 Subject: [PATCH 3/8] Merge some changes from @wilmoore given in issue #72 --- _posts/03-02-01-Programming-Paradigms.md | 54 ++++++++++++------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/_posts/03-02-01-Programming-Paradigms.md b/_posts/03-02-01-Programming-Paradigms.md index e7c5f52..1208b39 100644 --- a/_posts/03-02-01-Programming-Paradigms.md +++ b/_posts/03-02-01-Programming-Paradigms.md @@ -18,42 +18,36 @@ interfaces, inheritence, constructors, cloning, exceptions, and more. ### Functional Programming -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 (with support for closures) are present since PHP 5.3 (2009). +PHP supports first-class function, meaning that a function itself can be assigned to a variable, both user-defined and built-in. Those +functions referenced by a variable can be invoked dynamically. Functions can be passed as arguments to other functions (feature called +Higher-order functions) and function can return other functions. + +New anonymous functions (with support for closures) are present since PHP 5.3 (2009). + +The most common usage of higher-order functions is when implementing a strategy pattern. Built-in `array_filter` function asks both +for the input array (data) and a function (strategy, callback) used as a filter criteria on each array item. {% highlight php %} $min; }; } $input = array(1, 2, 3, 4, 5, 6); + +// Use array_filter on a input with a selected criteria function $output = array_filter($input, criteria_greater_than(3)); print_r($output); // items > 3 {% endhighlight %} +Each criteria function in the family accepts only elements greater than some minimum value. Single criteria returned by `criteria_greater_than` +is a closure whith `$min` argument closed by the value existing in the scope (given as argument when `criteria_greater_than` is called). + 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. From 9f40674396bf4fb96884872975af5cfb5aba2322 Mon Sep 17 00:00:00 2001 From: Goran Rakic Date: Fri, 13 Jul 2012 18:00:37 +0200 Subject: [PATCH 4/8] Improve functional programming section - Change criteria for filter - Extend the first example to show anonymous function without assignment - Add paragraph on recursion - Wrap to 120 chars --- _posts/03-02-01-Programming-Paradigms.md | 56 ++++++++++++++---------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/_posts/03-02-01-Programming-Paradigms.md b/_posts/03-02-01-Programming-Paradigms.md index 1208b39..8fbee17 100644 --- a/_posts/03-02-01-Programming-Paradigms.md +++ b/_posts/03-02-01-Programming-Paradigms.md @@ -5,8 +5,8 @@ isChild: true ## Programming Paradigms PHP is a flexible, dynamic language that supports a variety of programming techniques. It has evolved dramatically over -the years, notably adding a solid object-oriented model in PHP 5.0 (2004), anonymous functions and namespaces in PHP -5.3 (2009), and traits in PHP 5.4 (2012). +the years, notably adding a solid object-oriented model in PHP 5.0 (2004), anonymous functions and namespaces in PHP 5.3 +(2009), and traits in PHP 5.4 (2012). ### Object-oriented Programming @@ -18,41 +18,52 @@ interfaces, inheritence, constructors, cloning, exceptions, and more. ### Functional Programming -PHP supports first-class function, meaning that a function itself can be assigned to a variable, both user-defined and built-in. Those -functions referenced by a variable can be invoked dynamically. Functions can be passed as arguments to other functions (feature called -Higher-order functions) and function can return other functions. +PHP supports first-class function, 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 (feature called Higher-order functions) and function can return other functions. + +Recursion, a feature that allows a function to call itself is supported by the language, but most of the PHP code focus +on iteration. New anonymous functions (with support for closures) are present since PHP 5.3 (2009). -The most common usage of higher-order functions is when implementing a strategy pattern. Built-in `array_filter` function asks both -for the input array (data) and a function (strategy, callback) used as a filter criteria on each array item. +The most common usage of higher-order functions is when implementing a strategy pattern. 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. {% highlight php %} $min + * 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) { @@ -63,18 +74,19 @@ function criteria_greater_than($min) $input = array(1, 2, 3, 4, 5, 6); -// Use array_filter on a input with a selected criteria function +// Use array_filter on a input with a selected filter function $output = array_filter($input, criteria_greater_than(3)); print_r($output); // items > 3 {% endhighlight %} -Each criteria function in the family accepts only elements greater than some minimum value. Single criteria returned by `criteria_greater_than` -is a closure whith `$min` argument closed by the value existing in the scope (given as argument when `criteria_greater_than` is called). +Each filter function in the family accepts only elements greater than some minimum value. Single filter returned by +`criteria_greater_than` is a closure whith `$min` argument closed by the value in the scope (given as an argument when +`criteria_greater_than` is called). -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. +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. Imagine a templating or input validation libraries, where closure is +defined to capture variables in scope 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. From 8986b9d1bc19c68fa114c913b8747736ffd244e7 Mon Sep 17 00:00:00 2001 From: Goran Rakic Date: Fri, 13 Jul 2012 21:11:09 +0200 Subject: [PATCH 5/8] Closures and variable scope --- _posts/03-02-01-Programming-Paradigms.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_posts/03-02-01-Programming-Paradigms.md b/_posts/03-02-01-Programming-Paradigms.md index 8fbee17..338de7c 100644 --- a/_posts/03-02-01-Programming-Paradigms.md +++ b/_posts/03-02-01-Programming-Paradigms.md @@ -53,7 +53,7 @@ print_r($output); 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. This is used to cross variable scope restrictions in a very clean way. +defined. Closures can work around variable scope restrictions in a clean way. In the next example we use closures to define a function returning a single filter function for `array_filter`, out of a family of filter functions. From 0e5950250103a24dbfcd6d0133b8ce8666ed7219 Mon Sep 17 00:00:00 2001 From: Goran Rakic Date: Fri, 13 Jul 2012 23:35:34 +0200 Subject: [PATCH 6/8] Move to a separate topic page --- _posts/03-02-01-Programming-Paradigms.md | 62 +----------------- pages/Functional-Programming.md | 82 ++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 61 deletions(-) create mode 100644 pages/Functional-Programming.md diff --git a/_posts/03-02-01-Programming-Paradigms.md b/_posts/03-02-01-Programming-Paradigms.md index 338de7c..692021f 100644 --- a/_posts/03-02-01-Programming-Paradigms.md +++ b/_posts/03-02-01-Programming-Paradigms.md @@ -27,70 +27,10 @@ on iteration. New anonymous functions (with support for closures) are present since PHP 5.3 (2009). -The most common usage of higher-order functions is when implementing a strategy pattern. 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. - -{% highlight php %} - $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 -{% endhighlight %} - -Each filter function in the family accepts only elements greater than some minimum value. Single filter returned by -`criteria_greater_than` is a closure whith `$min` argument closed by the value in the scope (given as an argument when -`criteria_greater_than` is called). - -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. Imagine a templating or input validation libraries, where closure is -defined to capture variables in scope 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. +* Continue reading on [Functional Programming in PHP](/pages/Functional-Programming.html) * [Read about Anonymous Functions][anonymous-functions] * [Read about the Closure class][closure-class] * [More details in the Closures RFC][closures-rfc] diff --git a/pages/Functional-Programming.md b/pages/Functional-Programming.md new file mode 100644 index 0000000..0823890 --- /dev/null +++ b/pages/Functional-Programming.md @@ -0,0 +1,82 @@ +--- +layout: page +title: Functional Programming in PHP +--- + +PHP supports first-class function, 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 (feature called Higher-order functions) and function can return other functions. + +Recursion, a feature that allows a function to call itself is supported by the language, but most of the PHP code focus +on iteration. + +New anonymous functions (with support for closures) are present since PHP 5.3 (2009). + +The most common usage of higher-order functions is when implementing a strategy pattern. 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. + +{% highlight php %} + $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 +{% endhighlight %} + +Each filter function in the family accepts only elements greater than some minimum value. Single filter returned by +`criteria_greater_than` is a closure whith `$min` argument closed by the value in the scope (given as an argument when +`criteria_greater_than` is called). + +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. Imagine a templating or input validation libraries, where closure is +defined to capture variables in scope and access them later when the anonymous function is evaluated. + +* [Read about Anonymous functions][anonymous-functions] +* [More details in the Closures RFC][closures-rfc] +* [Read about dynamically invoking functions with `call_user_func_array`][call-user-func-array] + +[anonymous-functions]: http://www.php.net/manual/en/functions.anonymous.php +[call-user-func-array]: http://php.net/manual/en/function.call-user-func-array.php +[closures-rfc]: https://wiki.php.net/rfc/closures From 13bfa8f88df9e0d6206705ff6eeee02c77e487a1 Mon Sep 17 00:00:00 2001 From: Goran Rakic Date: Mon, 16 Jul 2012 20:25:58 +0200 Subject: [PATCH 7/8] Add page title --- pages/Functional-Programming.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pages/Functional-Programming.md b/pages/Functional-Programming.md index 0823890..99d9660 100644 --- a/pages/Functional-Programming.md +++ b/pages/Functional-Programming.md @@ -3,6 +3,8 @@ layout: page title: Functional Programming in PHP --- +# Functional Programming in PHP + PHP supports first-class function, 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 (feature called Higher-order functions) and function can return other functions. From de6fb719d5bc241e769d8df490ee93a32e839591 Mon Sep 17 00:00:00 2001 From: Goran Rakic Date: Fri, 20 Jul 2012 02:40:04 +0300 Subject: [PATCH 8/8] Copy new paragraph by @jeremeamia from main page to content page --- pages/Functional-Programming.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pages/Functional-Programming.md b/pages/Functional-Programming.md index 99d9660..4ff3ae0 100644 --- a/pages/Functional-Programming.md +++ b/pages/Functional-Programming.md @@ -14,6 +14,9 @@ on iteration. New anonymous functions (with support for closures) are present since PHP 5.3 (2009). +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. + The most common usage of higher-order functions is when implementing a strategy pattern. 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.