PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Gonzalo Ayuso's Blog: Real-life example of Closure usage with PHP5.3

Note: This article was originally published at PHPDeveloper on 8 December 2010.
PHPDeveloper

Gonzalo Ayuso has posted his real-life example of closures in a PHP 5.3 application rather than some of the syntax-only based examples from several other sites around the web.

One of the new improvements in new PHP5.3 version are the Closures. Here you are a real-life example where closures are really useful for me.

His example shows how to work with a nested array of data from a SQL query and walk through it using the array_walk function and a closure as a call back. Using the "use" keyword allows it to pull in values from outside the closure's scope and process the quantity, amount and price values to calculate totals.