<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP in Action &#187; Ruby</title>
	<atom:link href="http://blog.agilephp.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agilephp.com</link>
	<description>Dagfinn Reiersøl on PHP, agile development, Ruby and other addictive substances</description>
	<lastBuildDate>Mon, 28 Sep 2009 14:35:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Real programming with PHP 5.3 (part 1): array processing</title>
		<link>http://blog.agilephp.com/2009/03/19/real-programming-with-php-53-part-1-array-processing/</link>
		<comments>http://blog.agilephp.com/2009/03/19/real-programming-with-php-53-part-1-array-processing/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 09:48:33 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.agilephp.com/?p=1513</guid>
		<description><![CDATA[You may have heard of the new features that are scheduled for PHP 5.3, but who knows what they can be used for in real programming? I took the time for some experimental research, and came up with a few examples.
At the PHP UK conference, I saw Sebastian Bergmann&#8217;s presentation on lambda functions, closures and [...]]]></description>
			<content:encoded><![CDATA[<p>You may have heard of the new features that are scheduled for PHP 5.3, but who knows what they can be used for in real programming? I took the time for some experimental research, and came up with a few examples.</p>
<p>At the PHP UK conference, I saw <a href="http://www.slideshare.net/sebastian_bergmann/of-lambda-functions-closures-and-traits">Sebastian Bergmann&#8217;s presentation on lambda functions, closures and traits</a>. Lambda functions and closures are scheduled for 5.3  (or fivethree, as I&#8217;ve affectionately named the binary), or later. The presentation was intriguing, but has only simplistic examples, sufficient to illustrate the mechanical aspects. It left me curious as to what these features could be used for in real programs.</p>
<p>Before we start playing, let&#8217;s get the basic terminology clear. Lambda functions and closures are two fancy names for (variations on) anonymous functions. These are functions that are more flexible than ordinary functions and can be stored in variables or defined just where they are needed.</p>
<p>Anonymous functions are possible even in pre-5.3 PHP. They can be created using <code>create_function()</code>. But this is cumbersome, ugly, error-prone and possibly insecure. PHP 5.3 lets you do this in a much more natural and pleasant way.</p>
<p>The most basic and obvious case is sorting. Here is an example from Sebastian&#8217;s presentation:</p>
<pre class="brush: php">

&lt;?php

usort(
    $list,
    function ($a, $b) {
        if ($a == $b) return 0;
        return ($a &lt; $b) ? -1 : 1;
    }
);
</pre>
<p>In PHP before 5.3, this must be done either by using <code>create_function()</code> or by defining the function somewhere else, away from where it&#8217;s actually used. The above is clearly much better.</p>
<p>What else can we do with anonymous functions? In Ruby, they are the standard way of processing arrays. For example, if we want to process a set of files, adding a directory name at the beginning, we can do this:</p>
<pre class="brush: ruby">
files = [&#039;pdo.a&#039;,&#039;pdo.la&#039;,&#039;pdo.so&#039;,&#039;sqlite.a&#039;,&#039;sqlite.la&#039;,&#039;sqlite.so&#039;];
files_with_dir = files. map  { |f| &quot;modules/#{f}&quot; }
</pre>
<p>The snippet inside the braces is a function. <code>map</code> is the Ruby equivalent of PHP&#8217;s <code>array_map()</code>, except that Ruby is built to do this particular trick. But let&#8217;s try the equivalent with PHP 5.3. First we define the array:</p>
<pre class="brush: php">
$files = array(&#039;pdo.a&#039;,&#039;pdo.la&#039;,&#039;pdo.so&#039;,&#039;sqlite.a&#039;,&#039;sqlite.la&#039;,&#039;sqlite.so&#039;);
</pre>
<p>And then we process it:</p>
<pre class="brush: php">
$filesWithDir = array_map(
    function($f) { return &quot;modules/$f&quot; ; },
    $files
);
</pre>
<p>Interesting, but not necessarily better than the conventional alternative:</p>
<pre class="brush: php">
$filesWithDir = array();
foreach ($files as $file) {
    $filesWithDir[] = &quot;modules/$file&quot;;
}
</pre>
<p>Using array_map() seems like a realistic option, at least for some cases. It reads differently because it&#8217;s conceptually different, focusing on processing the entire array rather than one element at a time. You might find it cleaner. And in some cases, it will be clearly superior. If you have a slightly more complex structure than a simple one-dimensional array, you can use <code>array_map_recursive()</code> to simplify the code dramatically.</p>
<p>In <a href="http://blog.agilephp.com/2009/03/31/real-programming-with-php-53-part-2-javascript-style-classes/">part 2</a>, I&#8217;ll present an example of a JavaScript-style class composed of closures.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/fcdb80d8-75b2-45a3-8655-b9e16459cab8/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_a.png?x-id=fcdb80d8-75b2-45a3-8655-b9e16459cab8" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Real%20programming%20with%20PHP%205.3%20%28part%201%29%3A%20array%20processing&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2009%2F03%2F19%2Freal-programming-with-php-53-part-1-array-processing%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2009/03/19/real-programming-with-php-53-part-1-array-processing/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
