<?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>Anis Berejeb &#187; PHP 5.3</title>
	<atom:link href="http://www.berejeb.com/tag/php-5-3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.berejeb.com</link>
	<description>Actualites et nouveautes du developpement web, PHP, MySQL, HTTP, JavaScript, Performance</description>
	<lastBuildDate>Sat, 17 Sep 2011 16:38:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP 5.3 : Les Lambda functions et les Closures par l&#8217;exemple</title>
		<link>http://www.berejeb.com/2009/12/php-5-3-les-lambda-functions-et-les-closures-par-lexemple/</link>
		<comments>http://www.berejeb.com/2009/12/php-5-3-les-lambda-functions-et-les-closures-par-lexemple/#comments</comments>
		<pubDate>Sun, 06 Dec 2009 19:12:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[fonctions anonymes]]></category>
		<category><![CDATA[lambda functions]]></category>
		<category><![CDATA[php 5]]></category>
		<category><![CDATA[PHP 5.3]]></category>

		<guid isPermaLink="false">http://www.berejeb.com/?p=1245</guid>
		<description><![CDATA[L&#8217;une des fonctionnalités que présente la version PHP 5.3 sont les Lambda functions et les closures. Une fonction lambda est une fonction PHP anonyme (déclarée a la volée, sans nom, un peu a la Java Script) qui peut être stockée dans une variable et passée comme argument a d&#8217;autres fonctions ou méthodes. Une Closure est [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-203" href="http://www.berejeb.com/2009/07/sortie-php-5-3-0-fonctionnalites/php/"><img class="alignnone size-full wp-image-203" title="php" src="http://www.berejeb.com/wp-content/uploads/2009/07/php.gif" alt="php" width="120" height="67" /></a></p>
<p>L&#8217;une des fonctionnalités que présente la version PHP 5.3 sont les Lambda functions et les closures. Une fonction lambda est une fonction PHP anonyme (déclarée a la volée, sans nom, un peu a la Java Script) qui peut être stockée dans une variable et passée comme argument a d&#8217;autres fonctions ou méthodes. Une Closure est une fonction lambda qui est &laquo;&nbsp;consciente&nbsp;&raquo; de son contexte.</p>
<p>Illustrons ceci via des exemples :</p>
<h3>Utilisations avec les fonctions array_map(), array_reduce() et array_filter()</h3>
<p>L&#8217;exemple suivant filtre le tableau $input en enlevant toutes les valeurs supérieures a 5</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code14'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124514"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1245code14"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_filter"><span style="color: #990000;">array_filter</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&laquo;&nbsp;function ($v) { return $value &gt; 2; }&nbsp;&raquo; est une fonction lambda, qui peut être stockée dans une variable pour etre rutilisee :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code15'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124515"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1245code15"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$comparator</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_filter"><span style="color: #990000;">array_filter</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #000088;">$comparator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Supposons maintenant que je veuille changer le nombre permis dans le tableau filtré? Je pourrais éventuellement créer une autre fonction lambda ou utiliser une closure :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code16'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124516"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p1245code16"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$comparator</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$max</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$max</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_filter"><span style="color: #990000;">array_filter</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #000088;">$max_comparator</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>La fonction $comparator prend en paramètre le nombre maximum permis et retourne une fonction différente selon ce nombre. Bien que l&#8217;exemple est simple, vous imaginez déjà le grand pouvoir.</p>
<h3>passage de variables par reference</h3>
<p>Prenons cet exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code17'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124517"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p1245code17"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>
<span style="color: #000088;">$closure</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">.</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #000088;">$closure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">.</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #000088;">$closure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">.</span> PHP_EOL<span style="color: #339933;">;</span>
&nbsp;
Output<span style="color: #339933;">:</span>
<span style="color: #cc66cc;">1</span>
<span style="color: #cc66cc;">2</span>
<span style="color: #cc66cc;">3</span></pre></td></tr></table></div>

<p>Nous pouvons voir que la closure utilise une variable externe $x et l&#8217;incrémente a chaque appel. Nous pouvons mixer des variables passes avec et sans référence avec la clause &laquo;&nbsp;use&nbsp;&raquo;. Nous pouvons aussi avoir des fonctions qui retournent des closures. Dans ce cas, la durée de vie des closures est plus longue que la méthode qui les a définis. Par exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code18'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124518"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p1245code18"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getAppender<span style="color: #009900;">&#40;</span><span style="color: #000088;">$baseString</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$appendString</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$baseString</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$baseString</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$appendString</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Les closures et les objets</h2>
<p>Les closures fournissent un outil intéressant dans le contexte objet. Exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code19'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124519"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" id="p1245code19"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Chien
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_nom</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_couleur</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nom</span><span style="color: #339933;">,</span> <span style="color: #000088;">$couleur</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>_nom <span style="color: #339933;">=</span> <span style="color: #000088;">$nom</span><span style="color: #339933;">;</span>
         <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>_couleur <span style="color: #339933;">=</span> <span style="color: #000088;">$couleur</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bonjour<span style="color: #009900;">&#40;</span><span style="color: #000088;">$salutation</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	 <span style="color: #000088;">$self</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
         <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$self</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salutation</span><span style="color: #009900;">&#41;</span>
	 <span style="color: #009900;">&#123;</span>
             <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$salutation</span>, je m'appelle {<span style="color: #006699; font-weight: bold;">$self</span>-&amp;gt;_nom} et je suis un chien {<span style="color: #006699; font-weight: bold;">$self</span>-&amp;gt;_couleur}.&quot;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$chien</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Chien<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Georges&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;rouge&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$chien</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>bonjour<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bonjour&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Resultat<span style="color: #339933;">:</span>
Bonjour<span style="color: #339933;">,</span> je m<span style="color: #0000ff;">'appelle Georges et je suis un chien rouge.</span></pre></td></tr></table></div>

<p>Les closures peuvent aussi utilisées dans un contexte statique :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code20'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124520"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p1245code20"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Maison
<span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> peint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$couleur</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">return</span> static <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">use</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$couleur</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Peindre la maison en <span style="color: #006699; font-weight: bold;">$couleur</span> ....&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$maison</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Maison<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$maison</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>peint<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rouge'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Resultat<span style="color: #339933;">:</span>
Peindre la maison en rouge <span style="color: #339933;">....</span></pre></td></tr></table></div>

<p>La closure ne charge pas l&#8217;objet dans ce cas, ce qui fait que la méthode peint ne soit pas gourmande en mémoire.</p>
<h3>La methode __invoke()</h3>
<p>La méthode magique __invoke() permet a un objet de se faire appeler comme une closure. par exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code21'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124521"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p1245code21"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Chien
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __invoke<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Je suis un chien!&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$dog</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Chien<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Resultat <span style="color: #339933;">:</span>
Je suis un chien<span style="color: #339933;">!</span></pre></td></tr></table></div>

<p>En appelant $dog en tant que fonction ($dog()), la methode __invoke() est automatiquement appelee transformant la classe en une closure.</p>
<h2>La reflection et les closures</h2>
<p>L&#8217;API de reflection de PHP permet de faire un reverse-engineer sur les classes, interfaces, fonctions, méthodes etc. Puisque les closures sont des fonctions anonymes, ils n&#8217;apparaîtront alors pas via les appels de reflection. Par contre, une nouvelle méthode getClosure() a été ajoutée aux classes ReflectionMethod et ReflectionFunction afin de créer une closure depuis une fonction ou une méthode spécifique. Voici un exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code22'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124522"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p1245code22"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Compteur
<span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
           <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> incremente<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
           <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> valeurCourante<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
           <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>x <span style="color: #339933;">.</span> PHP_EOL<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$classe</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReflectionClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Compteur'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$methode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$classe</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getMethod<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'valeurCourante'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$closure</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$methode</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getClosure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$closure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$classe</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>incremente<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$closure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Resultat <span style="color: #339933;">:</span>
<span style="color: #cc66cc;">0</span>
<span style="color: #cc66cc;">1</span></pre></td></tr></table></div>

<p>Notez ici qu&#8217;il y a un effet de bord : la méthode getClosure nous permet d&#8217;accéder aux membres private et protected, ceci peut être intéressant dans des scénarios de tests unitaires. Exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code23'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124523"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1245code23"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Exemple
<span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> secret<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Je suis une methode secrete!&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #000088;">$classe</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReflectionClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Exemple'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$methode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$classe</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getMethod<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'secret'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$closure</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$methode</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getClosure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$closure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Resultat <span style="color: #339933;">:</span>
Je suis une methode secrete<span style="color: #339933;">!</span></pre></td></tr></table></div>

<p>Nous pouvons bien évidemment utiliser l&#8217;API de reflection pour inspecter la closure elle meme en passant tout simplement la closure au constructeur de la classe ReflectionMethod :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code24'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124524"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1245code24"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$closure</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$methodeClosure</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ReflectionMethod<span style="color: #009900;">&#40;</span><span style="color: #000088;">$closure</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Reflection<span style="color: #339933;">::</span><span style="color: #004000;">export</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$methodeClosure</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Output<span style="color: #339933;">:</span>
Method <span style="color: #009900;">&#91;</span>  <span style="color: #000000; font-weight: bold;">public</span> method __invoke <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #339933;">-</span> Parameters <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    Parameter <span style="color: #666666; font-style: italic;">#0 [  $x ]
</span>    Parameter <span style="color: #666666; font-style: italic;">#1 [  $y ]
</span>  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Utilite des closures ?</h2>
<p>Comme nous avons vu, l&#8217;un des utilisations les plus communes des closures est dans les quelques fonctions qui acceptent une fonction de callback comme paramètres. En effet, les closures peuvent être extrêmement utiles dans n&#8217;importe quel contexte la ou on veut encapsuler une logique a l&#8217;intérieur de son scope même. En voici un exemple plus concret :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code25'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124525"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1245code25"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysqli_connect"><span style="color: #990000;">mysqli_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;server&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Logger<span style="color: #339933;">::</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'database'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Connected to database'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'insert into mytable (title, description) values ('</span>Closures<span style="color: #0000ff;">','</span>tres interessant<span style="color: #0000ff;">');
Logger::log('</span>debug<span style="color: #0000ff;">','</span>database<span style="color: #0000ff;">','</span>Inserted Closures in mytable<span style="color: #0000ff;">');
$db-&amp;gt;query('</span>insert into mytable <span style="color: #009900;">&#40;</span>title<span style="color: #339933;">,</span> description<span style="color: #009900;">&#41;</span> values <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Lambda'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'aussi interessant'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Logger<span style="color: #339933;">::</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'database'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Inserted lambda in mytable'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'insert into mytable (title, description) values ('</span>Anonym functions<span style="color: #0000ff;">','</span>like JavaScript<span style="color: #0000ff;">');
Logger::log('</span>debug<span style="color: #0000ff;">','</span>database<span style="color: #0000ff;">','</span>Inserted Anonym Functions in mytable<span style="color: #0000ff;">');</span></pre></td></tr></table></div>

<p>Notez ici que chaque appel a Logger::log() passe toujours les mêmes deux premiers paramètres. Nous pouvons améliorer ceci en poussant cet appel dans une closure :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1245code26'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p124526"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p1245code26"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$logdb</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> Logger<span style="color: #339933;">::</span><a href="http://www.php.net/log"><span style="color: #990000;">log</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'database'</span><span style="color: #339933;">,</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysqli_connect"><span style="color: #990000;">mysqli_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;server&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$logdb</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Connected to database'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000088;">$db</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'insert into mytable (title, description) values ('</span>Closures<span style="color: #0000ff;">','</span>tres interessant<span style="color: #0000ff;">');
$logdb('</span>Inserted Closures in mytable<span style="color: #0000ff;">');
$db-&amp;gt;query('</span>insert into mytable <span style="color: #009900;">&#40;</span>title<span style="color: #339933;">,</span> description<span style="color: #009900;">&#41;</span> values <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Lambda'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'aussi interessant'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$logdb</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Inserted lambda in mytable'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'insert into mytable (title, description) values ('</span>Anonym functions<span style="color: #0000ff;">','</span>like JavaScript<span style="color: #0000ff;">');
$logdb('</span>Inserted Anonym Functions in mytable<span style="color: #0000ff;">');</span></pre></td></tr></table></div>

<p>L&#8217;utilisation des closures presente plusieurs avantages dans cet exemple : notamment le code est plus lisible et clair, mais aussi, il est plus facile de changer le niveau du log puisqu&#8217;il est implemente a une place unique (la closure).</p>

<div class="sociable">
<div class="sociable_tagline">
Partager :
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;partner=sociable" title="Print"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;title=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple&amp;bodytext=%0D%0A%0D%0AL%27une%20des%20fonctionnalit%C3%A9s%20que%20pr%C3%A9sente%20la%20version%20PHP%205.3%20sont%20les%20Lambda%20functions%20et%20les%20closures.%20Une%20fonction%20lambda%20est%20une%20fonction%20PHP%20anonyme%20%28d%C3%A9clar%C3%A9e%20a%20la%20vol%C3%A9e%2C%20sans%20nom%2C%20un%20peu%20a%20la%20Java%20Script%29%20qui%20peut%20%C3%AAtre%20stock%C3%A9e%20dans%20une%20v" title="Digg"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;title=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple&amp;notes=%0D%0A%0D%0AL%27une%20des%20fonctionnalit%C3%A9s%20que%20pr%C3%A9sente%20la%20version%20PHP%205.3%20sont%20les%20Lambda%20functions%20et%20les%20closures.%20Une%20fonction%20lambda%20est%20une%20fonction%20PHP%20anonyme%20%28d%C3%A9clar%C3%A9e%20a%20la%20vol%C3%A9e%2C%20sans%20nom%2C%20un%20peu%20a%20la%20Java%20Script%29%20qui%20peut%20%C3%AAtre%20stock%C3%A9e%20dans%20une%20v" title="del.icio.us"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;t=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple" title="Facebook"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;title=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple&amp;annotation=%0D%0A%0D%0AL%27une%20des%20fonctionnalit%C3%A9s%20que%20pr%C3%A9sente%20la%20version%20PHP%205.3%20sont%20les%20Lambda%20functions%20et%20les%20closures.%20Une%20fonction%20lambda%20est%20une%20fonction%20PHP%20anonyme%20%28d%C3%A9clar%C3%A9e%20a%20la%20vol%C3%A9e%2C%20sans%20nom%2C%20un%20peu%20a%20la%20Java%20Script%29%20qui%20peut%20%C3%AAtre%20stock%C3%A9e%20dans%20une%20v" title="Google Bookmarks"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;title=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple" title="Live"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://reporter.nl.msn.com/?fn=contribute&amp;Title=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple&amp;URL=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;cat_id=6&amp;tag_id=31&amp;Remark=%0D%0A%0D%0AL%27une%20des%20fonctionnalit%C3%A9s%20que%20pr%C3%A9sente%20la%20version%20PHP%205.3%20sont%20les%20Lambda%20functions%20et%20les%20closures.%20Une%20fonction%20lambda%20est%20une%20fonction%20PHP%20anonyme%20%28d%C3%A9clar%C3%A9e%20a%20la%20vol%C3%A9e%2C%20sans%20nom%2C%20un%20peu%20a%20la%20Java%20Script%29%20qui%20peut%20%C3%AAtre%20stock%C3%A9e%20dans%20une%20v" title="MSN Reporter"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/msnreporter.png" title="MSN Reporter" alt="MSN Reporter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple&amp;url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F" title="Netvibes"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F" title="Technorati"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple%20-%20http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F" title="Twitter"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;t=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple&opener=bm&amp;ei=UTF-8&amp;d=%0D%0A%0D%0AL%27une%20des%20fonctionnalit%C3%A9s%20que%20pr%C3%A9sente%20la%20version%20PHP%205.3%20sont%20les%20Lambda%20functions%20et%20les%20closures.%20Une%20fonction%20lambda%20est%20une%20fonction%20PHP%20anonyme%20%28d%C3%A9clar%C3%A9e%20a%20la%20vol%C3%A9e%2C%20sans%20nom%2C%20un%20peu%20a%20la%20Java%20Script%29%20qui%20peut%20%C3%AAtre%20stock%C3%A9e%20dans%20une%20v" title="Yahoo! Bookmarks"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;title=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple&amp;source=Anis+Berejeb+Actualites+et+nouveautes+du+developpement+web%2C+PHP%2C+MySQL%2C+HTTP%2C+JavaScript%2C+Performance&amp;summary=%0D%0A%0D%0AL%27une%20des%20fonctionnalit%C3%A9s%20que%20pr%C3%A9sente%20la%20version%20PHP%205.3%20sont%20les%20Lambda%20functions%20et%20les%20closures.%20Une%20fonction%20lambda%20est%20une%20fonction%20PHP%20anonyme%20%28d%C3%A9clar%C3%A9e%20a%20la%20vol%C3%A9e%2C%20sans%20nom%2C%20un%20peu%20a%20la%20Java%20Script%29%20qui%20peut%20%C3%AAtre%20stock%C3%A9e%20dans%20une%20v" title="LinkedIn"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&amp;title=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple" title="DZone"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  target="_blank" href="http://www.viadeo.com/shareit/share/?url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F12%2Fphp-5-3-les-lambda-functions-et-les-closures-par-lexemple%2F&title=PHP%205.3%20%3A%20Les%20Lambda%20functions%20et%20les%20Closures%20par%20l%27exemple&urllanguage=fr" title="viadeo FR"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/viadeo.png" title="viadeo FR" alt="viadeo FR" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.berejeb.com/2009/12/php-5-3-les-lambda-functions-et-les-closures-par-lexemple/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ZendCon2009 : Slides PHP5.3</title>
		<link>http://www.berejeb.com/2009/10/zendcon2009-slides-php5-3/</link>
		<comments>http://www.berejeb.com/2009/10/zendcon2009-slides-php5-3/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 01:13:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[featured]]></category>
		<category><![CDATA[PHP 5.3]]></category>
		<category><![CDATA[Slides]]></category>
		<category><![CDATA[ZendCon 2009]]></category>

		<guid isPermaLink="false">http://www.berejeb.com/?p=1061</guid>
		<description><![CDATA[Une information toute fraiche de la ZendCon 2009! Ilia Alshanesky met en ligne les &#171;&#160;Slides&#160;&#187; de sa session qui presente les fonctionnalites et nouveautes de la version 5.3 de PHP. Il parle essentiellement : des Espaces de noms des ameliorations de performance des nouveautés du langage (constructs, garbage collector, nowdoc, E_DEPRECATED etc) des closures, callStatic, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://ilia.ws/files/ZendCon2009_PHP53_Intro.pdf" target="_blank"><img class="alignnone size-full wp-image-1062" title="Screenshot-ZendCon2009_PHP53_Intro" src="http://www.berejeb.com/wp-content/uploads/2009/10/Screenshot-ZendCon2009_PHP53_Intro.png" alt="Screenshot-ZendCon2009_PHP53_Intro" width="400" height="150" /></a></p>
<p>Une information toute fraiche de la ZendCon 2009! Ilia Alshanesky met en ligne les &laquo;&nbsp;Slides&nbsp;&raquo; de sa session qui presente les fonctionnalites et nouveautes de la version 5.3 de PHP. Il parle essentiellement :</p>
<ul>
<li>des Espaces de noms</li>
<li>des ameliorations de performance</li>
<li>des nouveautés du langage (constructs, garbage collector, nowdoc, E_DEPRECATED etc)</li>
<li>des closures, callStatic, late static binding etc.</li>
<li>de MySQLnd</li>
<li>des Ameliorations dans SPL</li>
<li>des nouvelles fonctions de Date, XSL, SSL etc.</li>
</ul>
<p><a href="http://ilia.ws/files/ZendCon2009_PHP53_Intro.pdf" target="_blank">Interessant ! Telecharger les slides ICI.</a></p>

<div class="sociable">
<div class="sociable_tagline">
Partager :
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;partner=sociable" title="Print"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;title=ZendCon2009%20%3A%20Slides%20PHP5.3&amp;bodytext=%0D%0A%0D%0AUne%20information%20toute%20fraiche%20de%20la%20ZendCon%202009%21%20Ilia%20Alshanesky%20met%20en%20ligne%20les%20%22Slides%22%20de%20sa%20session%20qui%20presente%20les%20fonctionnalites%20et%20nouveautes%20de%20la%20version%205.3%20de%20PHP.%20Il%20parle%20essentiellement%20%3A%0D%0A%0D%0A%09des%20Espaces%20de%20noms%0D%0A%09des%20ameliorati" title="Digg"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;title=ZendCon2009%20%3A%20Slides%20PHP5.3&amp;notes=%0D%0A%0D%0AUne%20information%20toute%20fraiche%20de%20la%20ZendCon%202009%21%20Ilia%20Alshanesky%20met%20en%20ligne%20les%20%22Slides%22%20de%20sa%20session%20qui%20presente%20les%20fonctionnalites%20et%20nouveautes%20de%20la%20version%205.3%20de%20PHP.%20Il%20parle%20essentiellement%20%3A%0D%0A%0D%0A%09des%20Espaces%20de%20noms%0D%0A%09des%20ameliorati" title="del.icio.us"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;t=ZendCon2009%20%3A%20Slides%20PHP5.3" title="Facebook"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;title=ZendCon2009%20%3A%20Slides%20PHP5.3&amp;annotation=%0D%0A%0D%0AUne%20information%20toute%20fraiche%20de%20la%20ZendCon%202009%21%20Ilia%20Alshanesky%20met%20en%20ligne%20les%20%22Slides%22%20de%20sa%20session%20qui%20presente%20les%20fonctionnalites%20et%20nouveautes%20de%20la%20version%205.3%20de%20PHP.%20Il%20parle%20essentiellement%20%3A%0D%0A%0D%0A%09des%20Espaces%20de%20noms%0D%0A%09des%20ameliorati" title="Google Bookmarks"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;title=ZendCon2009%20%3A%20Slides%20PHP5.3" title="Live"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://reporter.nl.msn.com/?fn=contribute&amp;Title=ZendCon2009%20%3A%20Slides%20PHP5.3&amp;URL=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;cat_id=6&amp;tag_id=31&amp;Remark=%0D%0A%0D%0AUne%20information%20toute%20fraiche%20de%20la%20ZendCon%202009%21%20Ilia%20Alshanesky%20met%20en%20ligne%20les%20%22Slides%22%20de%20sa%20session%20qui%20presente%20les%20fonctionnalites%20et%20nouveautes%20de%20la%20version%205.3%20de%20PHP.%20Il%20parle%20essentiellement%20%3A%0D%0A%0D%0A%09des%20Espaces%20de%20noms%0D%0A%09des%20ameliorati" title="MSN Reporter"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/msnreporter.png" title="MSN Reporter" alt="MSN Reporter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=ZendCon2009%20%3A%20Slides%20PHP5.3&amp;url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F" title="Netvibes"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F" title="Technorati"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=ZendCon2009%20%3A%20Slides%20PHP5.3%20-%20http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F" title="Twitter"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;t=ZendCon2009%20%3A%20Slides%20PHP5.3&opener=bm&amp;ei=UTF-8&amp;d=%0D%0A%0D%0AUne%20information%20toute%20fraiche%20de%20la%20ZendCon%202009%21%20Ilia%20Alshanesky%20met%20en%20ligne%20les%20%22Slides%22%20de%20sa%20session%20qui%20presente%20les%20fonctionnalites%20et%20nouveautes%20de%20la%20version%205.3%20de%20PHP.%20Il%20parle%20essentiellement%20%3A%0D%0A%0D%0A%09des%20Espaces%20de%20noms%0D%0A%09des%20ameliorati" title="Yahoo! Bookmarks"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;title=ZendCon2009%20%3A%20Slides%20PHP5.3&amp;source=Anis+Berejeb+Actualites+et+nouveautes+du+developpement+web%2C+PHP%2C+MySQL%2C+HTTP%2C+JavaScript%2C+Performance&amp;summary=%0D%0A%0D%0AUne%20information%20toute%20fraiche%20de%20la%20ZendCon%202009%21%20Ilia%20Alshanesky%20met%20en%20ligne%20les%20%22Slides%22%20de%20sa%20session%20qui%20presente%20les%20fonctionnalites%20et%20nouveautes%20de%20la%20version%205.3%20de%20PHP.%20Il%20parle%20essentiellement%20%3A%0D%0A%0D%0A%09des%20Espaces%20de%20noms%0D%0A%09des%20ameliorati" title="LinkedIn"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  target="_blank" href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&amp;title=ZendCon2009%20%3A%20Slides%20PHP5.3" title="DZone"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  target="_blank" href="http://www.viadeo.com/shareit/share/?url=http%3A%2F%2Fwww.berejeb.com%2F2009%2F10%2Fzendcon2009-slides-php5-3%2F&title=ZendCon2009%20%3A%20Slides%20PHP5.3&urllanguage=fr" title="viadeo FR"><img src="http://www.berejeb.com/wp-content/plugins/sociable/images/viadeo.png" title="viadeo FR" alt="viadeo FR" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.berejeb.com/2009/10/zendcon2009-slides-php5-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

