<?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>Cowburn &#187; PHP</title>
	<atom:link href="http://cowburn.info/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://cowburn.info</link>
	<description>Online storage depot for Peter Cowburn</description>
	<lastBuildDate>Sat, 29 Oct 2011 11:14:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Voting on PHP development processes</title>
		<link>http://cowburn.info/2011/06/28/vote-php/</link>
		<comments>http://cowburn.info/2011/06/28/vote-php/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 22:15:32 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[internals]]></category>
		<category><![CDATA[RFC]]></category>
		<category><![CDATA[votes]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=462</guid>
		<description><![CDATA[This post is mostly going to be a (fairly) quick brain dump of my thoughts. Warning: they may not be particularly coherent or well explained. The topic of thought is my voting on two side-by-side votes centered on improving practices surrounding PHP version releases and additions to (or removals from) the language. For those with [...]]]></description>
			<content:encoded><![CDATA[<p>This post is mostly going to be a (fairly) quick brain dump of my thoughts. <strong>Warning:</strong> they may not be particularly coherent or well explained.  The topic of thought is my voting on two side-by-side votes centered on improving practices surrounding PHP version releases and additions to (or removals from) the language.<span id="more-462"></span></p>
<p>For those with some time to spare, the subjects were written up as RFCs for discussion on the <a href="http://news.php.net/php.internals">PHP internals mailing list</a> and were then put up for vote.  Those RFC pages are on the PHP wiki under <a href="https://wiki.php.net/rfc/voting">Voting on PHP features</a> (<a href="https://wiki.php.net/rfc/voting/votes">votes</a>) and <a href="https://wiki.php.net/rfc/releaseprocess">Release Process</a> (<a href="https://wiki.php.net/rfc/releaseprocess/votes">votes</a>).</p>
<p>Both votes are now closed and the tallies are as follows:</p>
<table>
<thead>
<tr>
<th>RFC</th>
<th>Yay</th>
<th>Nay</th>
</tr>
</thead>
<tbody>
<tr>
<td>Voting on PHP features</td>
<td>36</td>
<td>3</td>
</tr>
<tr>
<td>Release Process</td>
<td>26(+1)</td>
<td>5</td>
</tr>
</tbody>
</table>
<p>Now, on to the point of this post: I voted <strong>Nay</strong> to both of the RFCs.  As you will see (or not, if boredom sets in) neither are the strongest nay in the world but sitting on the fence would be a wasted opportunity.  </p>
<p><strong>Voting on PHP features</strong></p>
<p>It is beyond the scope of this post to detail what this RFC covers, go read it now. I&#8217;ll still be here when you&#8217;ve finished.  For those not wanting to bore themselves with the details, or as a reminder for whose who did, this particular topic boils down to: lets have more structured voting for changes to PHP, lets tag mailing list threads with [RFC] and [VOTE] as appropriate, lets move that voting off of the mailing list and on to the wiki, and finally lets put some minimum/guideline timeframes into place for the process.</p>
<p>All of the points are sensible and clearly are favourable to many over what we have (indeed, don&#8217;t have) at the moment.  But it all seems too lacking in substance and arbitrary for my taste.</p>
<p><em>Proposal initiation</em></p>
<p>Ideas for adoption by the community are mostly posted to internals for discussion anyway so the only real change on the discussion side of things is to make an RFC wiki page a mandatory feature.  However, requirements of and guidelines for RFC pages is completely glossed over (I&#8217;ll assume, because this is a <em>voting</em> RFC). Which proposals require an RFC, <em>every single one</em>? For minor changes, which for practical purposes an RFC might well be overkill, what place is there for a quick show of hands? Is that even to be considered/allowed at all?  What place is there for the &#8220;scratch an itch&#8221; changes that core developers love to slide in to PHP? (Aside: itch scratching is the main reason I got involved!)</p>
<p><em>Discussion period</em></p>
<p>The minimum amount of &#8220;thinking time&#8221; is stipulated as follows:</p>
<ul>
<li>1 week for RFCs that don&#8217;t &#8220;touch the language&#8221;</li>
<li>2 weeks for RFCs that do</li>
</ul>
<p>What exactly constitutes <em>touching</em> is not totally clear, but the example given in the next section is of adding new syntax (compared to, lets say, a new function or class).  Why those necessarily need double the amount of thinking time escapes me, as does why any RFC must require a minimum span of time between discussion on the mailing list (and elsewhere) and opening of the vote.  What <em>real</em> problems does this period solve? The numbers involved seem completely arbitrary and might as well have said 3 days, or 3 weeks.</p>
<p><em>Required Majority</em></p>
<p>Now, I&#8217;ll admit to having a love-hate relationship with this particular part of the RFC.  I love the idea of everyone having equal say from 15-year veteran of PHP to the brightest sparkly new face, to those who haven&#8217;t yet even ventured into the land of PHP internals discussions.  As an idea, it gives that warm, fuzzy feeling.</p>
<p>However, I also believe that we&#8217;re not all equal. My vote should <em>certainly not</em> carry the same weight as those who have many contributed orders of magnitude more to the language that I have or will, those who, in the past, may have vetoed proposals even with strong &#8220;yay&#8221; votes.  I like the idea of having people who can say, &#8220;no, that&#8217;s a crap idea and I won&#8217;t allow it.&#8221;  Of course everyone (everyone!) should be encouraged to give their own two pence, but if the likes of Rasmus, Zeev, Andi, or individual extension maintainers are dead against (or dead for) something then that has to mean more than, for example, my vote. Right? </p>
<p><em>Resurrecting Rejected Proposals</em></p>
<p>I don&#8217;t have much to say on this point. Giving previously rejected proposals another chance sounds fair enough to me, the wants and needs of <em>the community</em> does tend to sway over time. If a rejected proposal is improved enough to be &#8220;substantially&#8221; changed then it is likely not the same proposal at all so none of the old votes would be on the topic of the new, changed proposal anyway. </p>
<p><em>Who can vote</em></p>
<p>Everyone who uses PHP! Oh, no, wait, sorry that&#8217;s just impractical.  Everyone who has committed code to PHP! Oh, no, wait, sorry that&#8217;s just too few. Hmm, everyone who has written a framework! <em>*sideways glare*</em></p>
<p>I still have no idea of who we&#8217;re voting for being allowed to vote.  My conclusion is: whoever can be bothered signing up for a wiki account and following the required &#8220;hey, I&#8217;m not a robot&#8221; procedure.  </p>
<p><em>Conclusion</em></p>
<p>The aim of this RFC is to put in place a process to replace the old &#8220;+1&#8243; emails on the internals list.  That system had its problems, mostly in the shape of finding all of the votes to tally them! In the same way, I think that the proposed system also has problems which the RFC does not even attempt to provide direction or solutions, and also has solutions where there aren&#8217;t problems.</p>
<p><strong>Release Process</strong></p>
<p>As with the other RFC, I really do kind of agree with the sentiment of this proposal; it would be nice to have neat, tidy, regular releases (to <em>know</em> an addition will be publicly available within a year would be a nice incentive).  My main disagreement with this RFC is that we were expected to give a single yes/no vote on&#8230; well, multiple things.</p>
<p><em>Releases Cycle</em></p>
<p>I&#8217;m no expert on release cycles in open source projects, so comparison with what works elsewhere is beyond me.  The proposed yearly release cycle (for major versions) with 3-year lifetime seems super-duper short for PHP, as does a monthly (or less) cycle for minor releases, but if folks think it will work great then lets run with it.  I do worry however about having too many plates to keep spinning.  One of the illustrations shows 5 separate branches being actively developed concurrently.  I don&#8217;t know if that is a lot, or a drop in the ocean compared to other projects, but tackling just a couple of branches at any one time seems more than enough work for PHP at the moment.</p>
<p><em>Feature selection and development</em></p>
<p>There is not much to say for this section. The idea is that RFCs and wiki votes is the way forward.  However it does add to the list of questions that we&#8217;re supposed to provide a single vote for/against. The topic of who can vote should have been decided during discussion (and a previous vote!), and the proposal updated to reflect that.  We can&#8217;t say &#8220;yes&#8221; to, &#8220;<em>Who will be allowed to vote?</em>&#8221;</p>
<p><em>RMs Role</em></p>
<p>Apparently, they won&#8217;t be allowed to decided which non-bug-fix changes get to go into a release.  So much for, &#8220;hey, that&#8217;s still super-buggy lets keep working on it for a while.&#8221;  I believe a release manager should have the final say as to whether any individual feature is ready to go into a release. The RFC doesn&#8217;t make this clear, but it certainly sounds like once something has been committed, it <em>will</em> be in the next release; at least, there is no mention of anyone (community decision or otherwise) agreeing whether a feature is ready or not.</p>
<p><em>Release managers selection</em></p>
<p>The idea here is that pairs should put themselves forward as RMs, and we should vote them in.  Are there really so many pushing to become RMs?  I have not been around the core for too long but I&#8217;ve certainly not seen an over-abundance of volunteers for the role.  This particular proposal sounds like needless <a href="http://en.wiktionary.org/wiki/faff">faff</a>.</p>
<p><em>Feature(s) preview release, solving the experimental features</em></p>
<p>Harking back to an earlier section, the aim here is to provide &#8220;feature preview&#8221; releases so that new features can get some more thorough real-life testing/use by brave folks before hitting a normal release.  As with above, there is no scope for &#8220;whoa, this really isn&#8217;t ready yet&#8221; or even &#8220;what were we thinking?!&#8221; whatsoever. On the plus side, more testing is a <em>very good thing</em>.</p>
<p><em>Security Management</em></p>
<p>This section gets a big thumbs up from me, even though not being able to read the security-related bugs is a personal annoyance.  How this relates to the rest of the RFC in any way is questionable, but I like it.</p>
<p><em>Conclusion</em></p>
<p>As with the other RFC, the general theme is certainly something I will support.  However, the devil is in the details and in this case so is my disagreement with what is, and isn&#8217;t, being put on the table here. </p>
<p><strong>And finally</strong></p>
<p>The primary reason for this post is not to start some long-winded discussion on the above, merely to document my varied thoughts.  If any discussion does come of it, then that&#8217;s great but I will probably run away and hide if people start getting all outspoken. </p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2011/06/28/vote-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP 5.3.4 Released</title>
		<link>http://cowburn.info/2010/12/10/php-5-3-4-released/</link>
		<comments>http://cowburn.info/2010/12/10/php-5-3-4-released/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 07:50:16 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=452</guid>
		<description><![CDATA[Today, version 4 in the PHP 5.3 branch has been packaged and made available to everyone. As usual it contains security fixes, over a hundred bug fixes and feature requests, and lots of other additions &#038; improvements. Some highlights (my own choice) are below: Favourite changes Paths containing NULL (like /some/path\0foo.txt) are now considered invalid. [...]]]></description>
			<content:encoded><![CDATA[<p>Today, version 4 in the PHP 5.3 branch has been packaged and made available to everyone. As usual it contains security fixes, over a hundred bug fixes and feature requests, and lots of other additions &#038; improvements. Some highlights (my own choice) are below:</p>
<p><strong>Favourite changes</strong></p>
<ul>
<li>Paths containing <samp>NULL</samp> (like <samp>/some/path\0foo.txt</samp>) are now considered invalid.</li>
<li>Added <var>follow_location</var> (enabled by default) option for the http stream support.</li>
<li>Implemented FR #50692, not uploaded files don&#8217;t count towards <var>max_file_uploads</var> limit. As a side improvement, temporary files are not opened for empty uploads and, in debug mode, 0-length uploads.</li>
<li>Fixed bug #53409 (<code>sleep()</code> returns <samp>NULL</samp> on Windows).</li>
<li>Fixed covariance of return-by-ref constraints.</li>
<li>Fixed bug #50579 (<code>RegexIterator::REPLACE</code> doesn&#8217;t work).</li>
<li>Upgraded bundled PCRE to version 8.10.</li>
<li>Upgraded bundled Sqlite3 to version 3.7.3.</li>
</ul>
<p>Also, 5.2.15 was released yesterday, which marks the end of support for PHP 5.2. <img src='http://cowburn.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>For full information see the <a href="http://www.php.net/archive/2010.php#id2010-12-10-1">5.3.4</a> and <a href="http://www.php.net/archive/2010.php#id2010-12-09-1">5.2.15</a> announcements on php.net.</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/12/10/php-5-3-4-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5.3.3 released!</title>
		<link>http://cowburn.info/2010/07/22/php-5-3-3-released/</link>
		<comments>http://cowburn.info/2010/07/22/php-5-3-3-released/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 10:47:14 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php.net]]></category>
		<category><![CDATA[php5.3.3]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=440</guid>
		<description><![CDATA[Today, the latest version of PHP 5.3 (as well as the latest in the 5.2 branch, 5.2.14) has been released to the masses. The official announcement can found in the news archive with finer details in the changelog. A select few of the changes, of particular interest to me, are outlined below. Added support for [...]]]></description>
			<content:encoded><![CDATA[<p>Today, the latest version of PHP 5.3 (as well as the latest in the 5.2 branch, 5.2.14) has been released to the masses. The official announcement can found in the <a href="http://www.php.net/archive/2010.php#id2010-07-22-2" title="PHP: News Archive - 2010">news archive</a> with finer details in the <a href="http://www.php.net/ChangeLog-5.php#5.3.3" title="PHP 5.3.3 Changelog">changelog</a>.  A select few of the changes, of particular interest to me, are outlined below.<span id="more-440"></span></p>
<blockquote cite="http://www.php.net/ChangeLog-5.php#5.3.3">
<ul>
<li>Added support for copy to/from array/file for pdo_pgsql extension. (Denis Gasparin, Ilia)</li>
<li>Changed namespaced classes so that the ctor can only be named __construct now. (Stas)</li>
<li>Fixed bug #51590 (JSON_ERROR_UTF8 is undefined). (Felipe)</li>
<li>Fixed bug #49576 (FILTER_VALIDATE_EMAIL filter needs updating) (Rasmus)</li>
<li>Fixed bug #48361 (SplFileInfo::getPathInfo should return the parent dir). (Etienne)</li>
<li>And well over a hundred other bug and security fixes</li>
</ul>
</blockquote>
<p>The change to constructors in namespaced classes has already been the subject of &#8220;why doesn&#8217;t this work&#8221; questions. With 5.3.3 you <em>cannot</em> define class constructors like below.</p>
<div class="codecolorer-container text twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;code lang=&quot;php&quot;&gt;<br />
namespace Foo {<br />
&nbsp; &nbsp; class Bar {<br />
&nbsp; &nbsp; &nbsp; &nbsp; function Bar() {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // constructing \Foo\Bar class<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
}<br />
&lt;/code&gt;</div></div>
<p>Instead, you need to be using the right and proper <code lang="php">__construct</code> method. Note that this only applies to namespaced classes, so old code can continue (if they like) using the class-name-based constructor. This really only affects the (few?) folks who might be making use of, or adapting their old code to use, namespaces yet still keeping the old style of naming the constructor method.</p>
<p>Go, play!</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/07/22/php-5-3-3-released/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Happy 15th, PHP</title>
		<link>http://cowburn.info/2010/06/08/happy-15th-php/</link>
		<comments>http://cowburn.info/2010/06/08/happy-15th-php/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 07:58:57 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[birthday]]></category>
		<category><![CDATA[php.net]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=429</guid>
		<description><![CDATA[Today marks the 15th birthday* of PHP. I did have a nice, long post planned but lost it during the drafting process (maybe I should use WordPress which auto-saves?). So, in a new concise train of thought I&#8217;ll leave the rambling to other folks in the PHP community who will (hopefully) be providing their reflections [...]]]></description>
			<content:encoded><![CDATA[<p>Today marks the 15th birthday<a href="#fn1">*</a> of PHP. I did have a nice, long post planned but lost it during the drafting process (maybe I should use WordPress which auto-saves?).  So, in a new concise train of thought I&#8217;ll leave the rambling to other folks in the PHP community who will (hopefully) be providing their reflections over the last decade and a half. I&#8217;ll amend links to this post for those who don&#8217;t want to search around for birthday wishes.</p>
<p>Happy birthday, PHP! <img src='http://cowburn.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <span id="more-429"></span></p>
<p class="aside" id="fn1">* See <a href="http://groups.google.com/group/comp.infosystems.www.authoring.cgi/browse_thread/thread/3f2276e6067acfb0/cc7d43454d64d133" title="Announce: Personal Home Page Tools (PHP Tools) Options">Announce: Personal Home Page Tools (PHP Tools) Options</a> (8th June 1995)</p>
<h3>Elsewhere</h3>
<ul>
<li><a href="http://schlueters.de/blog/archives/136-15-years-of-PHP.html" title="15 years of PHP">15 years of PHP</a> from Johannes Schlüter</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/06/08/happy-15th-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Glob Patterns for File Matching in PHP</title>
		<link>http://cowburn.info/2010/04/30/glob-patterns/</link>
		<comments>http://cowburn.info/2010/04/30/glob-patterns/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 18:16:46 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[glob]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=390</guid>
		<description><![CDATA[This article has been on the cards for a while now with recent articles elsewhere1,2 prompting me to get this finished and up on the blog. The focus here is on the wildcard patterns that can be used with glob. Intro to the glob function To quote the succinct description on the PHP manual page [...]]]></description>
			<content:encoded><![CDATA[<p>This article has been on the cards for a while now with recent articles elsewhere<sup><a href="#fn1" title="php|architect - Putting glob() to the test">1</a>,<a href="#fn2" title="Nettuts+ - Loop Through Folders with PHP’s Glob()">2</a></sup> prompting me to get this finished and up on the blog. The focus here is on the wildcard patterns that can be used with <code>glob</code>.<span id="more-390"></span>  </p>
<h3>Intro to the <code>glob</code> function</h3>
<p>To quote the succinct description on the <a href="http://php.net/glob" title="PHP: Glob">PHP manual page</a> for <code>glob</code>:</p>
<blockquote cite="http://php.net/glob" datetime="2010-04-30T16:20:00Z"><p>The <code>glob()</code> function searches for all the pathnames matching <var>pattern</var> according to the rules used by the libc <code>glob()</code> function, which is similar to the rules used by common shells.<br />
<cite>http://php.net/glob</cite></p></blockquote>
<h3>What&#8217;s in a pattern?</h3>
<p>Most people who have already encountered <code>glob</code> know to make use of the <code>*</code> metacharacter to match <em>some characters</em>, and those digging a little deeper often discover that discrete alternatives can be globbed with braces (e.g. <code>image.{gif,jpg,png}</code>). However, there are more special characters and sequences that can be used to be more (or less, if we want) specific about what to find. </p>
<p class="small"><em>Aside</em>: please <strong>do not</strong> make the mistake of thinking that glob patterns are <em>regular expressions</em>, they&#8217;re just not. If you do want to use regular expressions to find paths/files then you are invited to use SPL&#8217;s <a href="http://php.net/regexiterator" title="PHP: SPL RegexIterator">RegexIterator</a>, which allows filtering of an Iterator based on a PCRE regex, in conjunction with a DirectoryIterator or FilesystemIterator (there are recursive flavours of the Regex- and DirectoryIterator if you need to delve into folders). For those SPL-ly inclined, also note the <a href="http://php.net/globiterator" title="PHP: SPL GlobIterator">GlobIterator</a> which combines the goodness of globbing with iteration. If that made entirely <em>no sense</em>, please read on! Globs are much less verbose…</p>
<p>So, here are the special doohickeys (technical term!) that we can use with <code>glob</code>:</p>
<dl>
<dt><code>*</code> (an asterisk)</dt>
<dd>Matches zero of more characters.</dd>
<dt><code>?</code></dt>
<dd>Matches exactly any one character.</dd>
<dt><code>[...]</code></dt>
<dd>Matches one character from a group.  A group can be a list of characters, e.g. <code>[afkp]</code>, or a range of characters, e.g. <code>[a-g]</code> which is the same as <code>[abcdefg]</code>.</dd>
<dt><code>[!...]</code></dt>
<dd>Matches any single character not in the group. <code>[!a-zA-Z0-9]</code> matches any character that is not alphanumeric.</dd>
<dt><code>\</code></dt>
<dd>Escapes the next character. For special characters, this causes them to not be treated as special. For example, <code>\[</code> matches a literal <samp>[</samp>. If <var>flags</var> includes <var>GLOB_NOESCAPE</var>, this quoting is disabled and <code>\</code> is handled as a simple character. </dd>
</dl>
<h3>Globbingly good glob examples</h3>
<p>Here are a few examples of what globs might look like alongside a brief description of the intended behaviour: if you have any suggestions please do make them in the comments as I'm running short on inspiration! </p>
<table class="light">
<thead>
<tr style="width:20%">
<th>pattern</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>*.txt</code></td>
<td>Get directory contents which have the extension of <samp>.txt</samp> (Note: a file could be named simply <samp>.txt</samp>!).</td>
</tr>
<tr>
<td><code>??</code></td>
<td>Get directory contents with names <em>exactly</em> two characters in length.</td>
</tr>
<tr>
<td><code>??*</code></td>
<td>Get directory contents with names <em>at least</em> two characters in length.</td>
</tr>
<tr>
<td><code>g?*</code></td>
<td>Get directory contents with names at least two characters in length and starting with the letter <samp>g</samp></td>
</tr>
<tr>
<td><code>*.{jpg,gif,png}</code></td>
<td>Get directory contents with an extension of <samp>.jpg</samp>, <samp>.gif</samp> or <samp>.png</samp>. Remember to use the <var>GLOB_BRACE</var> flag.</td>
</tr>
<tr>
<td><code>DN?????.dat</code></td>
<td>Get directory contents which start with the letters <samp>DN</samp>, followed by five characters, with an extension of <samp>.dat</samp>.</td>
</tr>
<tr>
<td><code>DN[0-9][0-9][0-9][0-9][0-9].dat</code></td>
<td>Get directory contents which start with the letters <samp>DN</samp>, followed by five <em>digits</em>, with an extension of <samp>.dat</samp>.</td>
</tr>
<tr>
<td><code>[!aeiou]*</code></td>
<td>Get directory contents which do not start with a vowel letter.</td>
</tr>
<tr>
<td><code>[!a-d]*</code></td>
<td>Get directory contents which do not start with <samp>a</samp>, <samp>b</samp>, <samp>c</samp> or <samp>d</samp>.</td>
</tr>
<tr>
<td><code>*\[[0-9]\].*</code></td>
<td>Get directory contents whose basename ends with a single digit enclosed in square braces. If <var>GLOB_NOESCAPE</var> is used, a single digit enclosed in <samp>\[</samp> and <samp>\]</samp> which would be a pretty weird name.</td>
</tr>
<tr>
<td><code>subdir/img*/th_?*</code></td>
<td>Get directory contents whose name starts with <samp>th_</samp> (with at least one character after that) within directories whose names start with <samp>img</samp> in the <samp>subdir</samp> directory.</td>
</tr>
</tbody>
</table>
<p>Well there we go, I&#8217;ve said what I came here to say so all that remains to be done is give some link love to those  two recent articles that prompted me to dust off this draft and click the &#8220;publish&#8221; button. </p>
<h6>With thanks</h6>
<ol>
<li id="fn1">php|architect&#8217;s <a href="http://www.phparch.com/2010/04/28/putting-glob-to-the-test/" title="Putting glob() to the test">Putting glob() to the test</a> focuses on execution times of <code>glob</code> and other techniques of getting a list of files.</li>
<li id="fn2">Marcus Schumann&#8217;s <a href="http://net.tutsplus.com/tutorials/php/quick-tip-loop-through-folders-with-phps-glob/" title="Loop Through Folders with PHP’s Glob()">Loop Through Folders with PHP’s Glob()</a> article on Nettuts+ gives a brief introduction for those who might not have been introduced to <code>glob</code>.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2010/04/30/glob-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using PHP Functions in XPath Expressions</title>
		<link>http://cowburn.info/2009/10/23/php-funcs-xpath/</link>
		<comments>http://cowburn.info/2009/10/23/php-funcs-xpath/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 20:41:13 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[XPath]]></category>
		<category><![CDATA[DOM]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=336</guid>
		<description><![CDATA[Disclaimer: this article expects familiarity with using the DOM1 extension and XPath2 expressions. The (currently undocumentednow documented3) DOMXPath::registerPHPFunctions method is available as of PHP 5.3.0 (it was added to the code base back in December 2006) and allows the use of PHP functions (and static methods) within XPath queries to complement the normal set of [...]]]></description>
			<content:encoded><![CDATA[<p>Disclaimer: this article expects familiarity with using the DOM<sup><a href="#fn1" title="footnote 1">1</a></sup> extension and XPath<sup><a href="#fn2" title="footnote 2">2</a></sup> expressions. </p>
<p>The (<del datetime="2009-10-26T14:15:44Z">currently undocumented</del><ins datetime="2009-10-26T14:15:44Z">now documented</ins><sup><a href="#fn3" title="footnote 3">3</a></sup>) <code>DOMXPath::registerPHPFunctions</code> method is available as of PHP 5.3.0 (it was added to the code base back in December 2006) and allows the use of PHP functions (and static methods) within XPath queries to complement the normal set of XPath functions<sup><a href="#fn2" title="footnote 2">2</a></sup>.<span id="more-336"></span></p>
<h3>Description</h3>
<p><code>void <strong>DOMXPath::registerPHPFunctions</strong>  ([ string|array <var>$restrict</var>] )</code></p>
<p>Enables the use of PHP functions as XPath functions.</p>
<h3>Parameters</h3>
<dl>
<dt>restrict</dt>
<dd>
Use this parameter to only allow certain functions to be called from XPath; it can be either a string (a function name) or an array of function names.
</dd>
</dl>
<h3>Return Values</h3>
<p>No value is returned.</p>
<h3>Examples</h3>
<p><strong>Note:</strong> The following examples load a sample XML document called <kbd>book.xml</kbd> with the following contents:</p>
<div class="codecolorer-container xml twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;books<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PHP Basics<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jim Smith<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jane Smith<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>PHP Secrets<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Jenny Smythe<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>XML basics<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Joe Black<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/books<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></div>
<h3 id="ex1">Example #1: Call a PHP function in XPath with <code>php:functionString</code></h3>
<p>This example demonstrates the basic use of <code>DOMXPath::registerPHPFunctions</code> by replicating the <code>substring</code> XPath function. The first thing that needs to be done is to register the <samp>php</samp> namespace with the associated URI <samp>http://php.net/xpath</samp>. Don&#8217;t question it, it just needs to be done!</p>
<p>Next, we call <code>DOMXPath::registerPHPFunctions</code> on our object. If no arguments are used, as in this example, then the range of functions allowed to be called is not restricted—you can call any<sup><a href="#fn4" title="footnote 4">4</a></sup> function from XPath. I would always advise restricting the functions which can be called, see <a href="#ex2" title="Example 2">example 2</a>.</p>
<p>Within our XPath query, we use <code>php:functionString</code> which allows us to name a function and provide some parameters (or indeed, no parameters) to be passed to that function. There are two flavours which can be used here: <code>php:functionString</code> which passes an XML node/attribute as a string and <code>php:function</code> (see <a href="#ex3" title="Example 3">example 3</a>) which passes an array of XML node/attribute objects (in <code>DOMElement</code> / <code>DOMAttr</code> / etc. form) to the function .  In this example the PHP function <a href="http://php.net/substr"><code>substr</code></a> is called, passing along the book&#8217;s title (in string form), an offset of <samp>0</samp> and length of <samp>3</samp>. This returns the first 3 characters of the book&#8217;s title which is then compared to the string <kbd>PHP</kbd> in order to filter our list of books down to those having titles starting with <kbd>PHP</kbd>.</p>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span><br />
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Register the php: namespace (required)</span><br />
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://php.net/xpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Register PHP functions (no restrictions)</span><br />
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPHPFunctions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Call substr function on the book title</span><br />
<span style="color: #000088;">$nodes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'//book[php:functionString(&quot;substr&quot;, title, 0, 3) = &quot;PHP&quot;]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Found <span style="color: #006699; font-weight: bold;">{$nodes-&gt;length}</span> books starting with 'PHP':<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$title</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$author</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;author&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$title</span> by <span style="color: #006699; font-weight: bold;">$author</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>The example will output something like:</p>
<div class="codecolorer-container plain twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="plain codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Found 2 books starting with 'PHP':<br />
PHP Basics by Jim Smith<br />
PHP Secrets by Jenny Smythe</div></div>
<h3 id="ex2">Example #2: Restricting the functions available to XPath</h3>
<p>To restrict the functions made available to XPath, provide either a string containing the name of the single function that you wish to allow or an array of strings containing function names as the <var>restrict</var> parameter (note: static methods can also be used, e.g. &#8220;<samp>Classname::method</samp>&#8220;).  If functions were added with <var>restrict</var> and a function is called in XPath which is not one of them, an <var>E_WARNING</var> will be raised stating <samp>Not allowed to call handler &#8216;<var>function</var>()&#8217;</samp> (where <var>function</var> is the name of the function that cannot be called).</p>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span><br />
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Register the php: namespace (required)</span><br />
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://php.net/xpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Register PHP functions (no restrictions)</span><br />
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPHPFunctions</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;strtoupper&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Get first book's title in uppercase</span><br />
<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">evaluate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php:functionString(&quot;strtoupper&quot;, //book[1]/title)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Try a function not in our restrictions list</span><br />
<span style="color: #000088;">$fail</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">evaluate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php:functionString(&quot;strtolower&quot;, //book[1]/title)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>The example will output something like:</p>
<div class="codecolorer-container plain twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="plain codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PHP BASICS&lt;br /&gt;<br />
&lt;b&gt;Warning&lt;/b&gt;: &nbsp;DOMXPath::evaluate() [&lt;a href='domxpath.evaluate'&gt;domxpath.evaluate&lt;/a&gt;]: Not allowed to call handler 'strtolower()'. in &lt;b&gt;example2.php&lt;/b&gt; on line &lt;b&gt;18&lt;/b&gt;&lt;br /&gt;</div></div>
<h3 id="ex3">Example #3: Passing DOM objects using <code>php:function</code></h3>
<p>Up to now, the examples have both used <code>php:functionString</code>. As mentioned above, instead of passing a string value to the PHP function it is possible to pass along an array of DOM* objects to manipulate them as you please by using <code>php:function</code>.</p>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span><br />
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$xpath</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMXPath<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Register the php: namespace (required)</span><br />
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://php.net/xpath&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Register PHP functions (no restrictions)</span><br />
<span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerPHPFunctions</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;example3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> example3<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Return true if more than one author</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nodes</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">// Filter books with multiple authors</span><br />
<span style="color: #000088;">$books</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xpath</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'//book[php:function(&quot;example3&quot;, author)]'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Books with multiple authors:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$book</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>The example will output something like:</p>
<div class="codecolorer-container plain twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="plain codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Books with multiple authors:<br />
PHP Basics</div></div>
<h3>Summary</h3>
<p>Just to quickly summarise everything, here is a quick run-down. In PHP, make sure to register the <kbd>php</kbd> namespace (with the URI <kbd>http://php.net/xpath</kbd>) and then register your PHP functions (whether core, extensions or user-defined) or static methods with <code>DOMXPath::registerPHPFunctions</code>. In XPath, use <code>php:functionString</code> or <code>php:function</code> to call the PHP function.</p>
<p>If you have made use of this feature, or want to know more, then do feel free to comment. Thanks for reading.</p>
<h4>Footnotes</h4>
<ol>
<li id="fn1"><a href="http://php.net/dom" title="PHP: DOM">http://php.net/dom</a></li>
<li id="fn2"><a href="http://schlitt.info/opensource/blog/0704_xpath.html" title="XPath overview">http://schlitt.info/opensource/blog/0704_xpath.html</a>
<li id="fn3">The documentation page, <del datetime="2009-10-26T14:15:44Z">when it gets written, will be</del><ins datetime="2009-10-26T14:15:44Z">is</ins> available at <a href="http://php.net/domxpath.registerphpfunctions" title="PHP: DOMXPath::registerPHPFunctions">http://php.net/domxpath.registerphpfunctions</a></li>
<li id="fn4">In truth, some functions are not suitable such as those that return non-scalar values (and cannot be cast to one) which XPath will not understand.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/10/23/php-funcs-xpath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The InfiniteIterator in PHP</title>
		<link>http://cowburn.info/2009/09/25/infiniteiterator/</link>
		<comments>http://cowburn.info/2009/09/25/infiniteiterator/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 12:49:28 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[infiniteiterator]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=316</guid>
		<description><![CDATA[In an article back in July (Anonymous Functions and Closures (as of PHP 5.3)), I gave an example of looping over a series of values repeatedly. Whilst that example does the job (and introduces the concept of closures) it&#8217;s hardly the most convenient method of repeatedly iterating over a series of values. Introducing the InfiniteIterator [...]]]></description>
			<content:encoded><![CDATA[<p>In an article back in July (<a href="http://cowburn.info/2009/07/08/anonymous-closures/" title="Read Anonymous Functions and Closures (as of PHP 5.3)">Anonymous Functions and Closures (as of PHP 5.3)</a>), I gave <a href="http://cowburn.info/2009/07/08/anonymous-closures/#fig5" title="See example: Cycling with Closures">an example</a> of looping over a series of values repeatedly.  Whilst that example does the job (and introduces the concept of closures) it&#8217;s hardly the most convenient method of repeatedly iterating over a series of values.  Introducing the <a href="http://php.net/InfiniteIterator" title="PHP Manual: InfiniteIterator"><code>InfiniteIterator</code></a> which is part of the <a href="http://php.net/spl" title="Standard PHP Library">Standard PHP Library</a> (SPL).<span id="more-316"></span></p>
<p>On the plus side, for those who have yet to take the plunge into using PHP 5.3, this iterator has been part of the core of PHP as of 5.1.0 so there is a much greater chance of actually being able to use it right now.  Bear in mind that the SPL is supposed to work together with its component parts so if <a href="http://php.net/iterator" title="PHP: Object Iteration">iterators</a> are a foreign concept, some of this post might be a little unclear but it is not the purpose of this post to outline iterators in PHP.</p>
<p>As eluded to above, the <code>InfiniteIterator</code> comes in useful when you have an existing <code>Iterator</code> (anything that implements that interface; a directory listing (<code>DirectoryIterator</code>), a file (<code>SplFileObject</code>), an array (with <code>ArrayIterator</code>), and so on) and wish to iterate over its contents again and again. </p>
<h3>An <code>InfiniteIterator</code> Example</h3>
<p>Here is a basic example that I wrote for the <a href="http://php.net/InfiniteIterator.construct" title="PHP Manual: InfiniteIterator::__construct">documentation</a>. Note that a <a href="http://php.net/LimitIterator" title="The LimitIterator class"><code>LimitIterator</code></a> is used to restrict the values which are iterated over (otherwise the loop would go on forever!).</p>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$arrayit</span> &nbsp;<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayIterator<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cat'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'dog'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$infinite</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InfiniteIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arrayit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$limit</span> &nbsp; &nbsp;<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LimitIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$infinite</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$limit</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$value</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>This example outputs something along the lines of:</p>
<div class="codecolorer-container plain twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="plain codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">cat<br />
dog<br />
cat<br />
dog<br />
cat<br />
dog<br />
cat</div></div>
<p>Can you think of any more useful instances where the <code>InfiniteIterator</code> might come in useful?</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/09/25/infiniteiterator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What Firefly/Serenity Can Teach Us About PHP (external)</title>
		<link>http://cowburn.info/2009/08/11/firefly-php/</link>
		<comments>http://cowburn.info/2009/08/11/firefly-php/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 14:36:12 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[external]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=303</guid>
		<description><![CDATA[This update is just really linking through to What Firefly/Serenity Can Teach Us About PHP on PHP Developer where the author makes some comparison between the awesome sci-fi series Firefly and PHP. Worth a few minutes at least: go read. Here&#8217;s the summary Just because it’s not the latest and greatest version, it doesn’t mean [...]]]></description>
			<content:encoded><![CDATA[<p>This update is just really linking through to <a href="http://blog.phpdeveloper.org/?p=158">What Firefly/Serenity Can Teach Us About PHP</a> on <a href="http://blog.phpdeveloper.org/">PHP Developer</a> where the author makes some comparison between the awesome sci-fi series Firefly and PHP. Worth a few minutes at least: <a href="http://blog.phpdeveloper.org/?p=158">go read</a>.<span id="more-303"></span></p>
<p>Here&#8217;s the summary</p>
<ul>
<li>Just because it’s not the latest and greatest version, it doesn’t mean it’s not good</li>
<li>Just because you don’t understand something, it doesn’t mean you shouldn’t embrace it</li>
<li>There’s nothing stronger than a bond between those who care</li>
<li>It takes all kinds to make the &#8216;verse go &#8217;round</li>
<li>Use only what you need</li>
<li>A good team is nothing without good leaders</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/08/11/firefly-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Anonymous Functions and Closures (as of PHP 5.3)</title>
		<link>http://cowburn.info/2009/07/08/anonymous-closures/</link>
		<comments>http://cowburn.info/2009/07/08/anonymous-closures/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 14:41:38 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[anonymous functions]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=234</guid>
		<description><![CDATA[There has been much discussion, especially of late considering the release of PHP 5.3, on the subject of anonymous functions and closures (both terms appear to be used interchangeably in the documentation). They are not the same, though many folks are introduced to them at the same time. Anonymous Functions Put plainly, an anonymous function [...]]]></description>
			<content:encoded><![CDATA[<p>There has been much discussion, especially of late considering the release of PHP 5.3, on the subject of anonymous functions and closures (both terms appear to be used interchangeably in the <a href="http://php.net/closures" title="Closures in the PHP Manual">documentation</a>).  They are not the same, though many folks are introduced to them at the same time.<span id="more-234"></span></p>
<h3>Anonymous Functions</h3>
<p>Put plainly, an anonymous function is a function without a name. That&#8217;s all, there&#8217;s nothing magic or particularly technical about it.  There are a few ways to create an anonymous function in PHP; up until PHP 5.3 came around the usual way was to use <a href="http://php.net/create_function" title="create_function documentation on php.net"><code>create_function</code></a> (see <a href="#fig1" title="View figure 1: Anonymous function with create_function">fig. 1</a>) which takes a couple of arguments as strings and spits out a function, in PHP 5.3 we&#8217;re allowed to create anonymous functions looking much more like normal function definitions (see <a href="#fig2" title="View figure 2: Anonymous function in PHP 5.3">fig. 2</a>).</p>
<h4 id="fig1">Fig. 1: Anonymous function with create_function</h4>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$square</span> <span style="color: #339933;">=</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$x'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return $x*$x;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$square</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 4</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<h4 id="fig2">Fig 2: Anonymous function as of PHP 5.3</h4>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$square</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$square</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 4</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>I hope you&#8217;ll agree that the latter is a much nicer way (visually) of defining an anonymous function, quite apart from the more logical and performance-related (see <a href="http://fabien.potencier.org/article/17/on-php-5-3-lambda-functions-and-closures#comments" title="Comments on 'On PHP 5.3, Lambda Functions, and Closures'">comment #4</a>) aspects of using it.</p>
<h3>Closures</h3>
<p>A closure is a (anonymous) function that is aware of its context. For all intents and purposes, think of them being anonymous functions which know about some variables which weren&#8217;t defined within them. In geeky language, they are functions which close over (hence &#8220;closure&#8221;) free variables, the latter being variables which are not local nor arguments to the function.  It is easier to see with an example, I think.</p>
<h4 id="fig3">Without Closure</h4>
<p>Lets say that we need a function to add three to a number (overly basic, I know). That&#8217;s easy.</p>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$add_3</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_3</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 13</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Now if we wanted another function to behave similarly but instead it should add 4 to the supplied number, we could just as easily create a new anonymous function to do the job.</p>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$add_4</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_4</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 14</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>If, however, you wanted to do that for a series of different additions then things could get very repetitive, very quickly!  If only we could dynamically create an anonymous function to do what we wanted: enter the closure.</p>
<h4 id="fig4">With Closure</h4>
<p>We can define an anonymous function (it needn&#8217;t be anonymous but lets keep on theme) which returns anonymous function based on arguments supplied to it. Remember our definition of a closure above, specifically making use of &#8216;free&#8217; variables, that is what our <code>$value_to_add</code> is in the example below.</p>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$add</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_to_add</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <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;">$x</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;">$value_to_add</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$value_to_add</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$add_3</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$add_4</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_3</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 13</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add_4</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 14</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<h3>A more practical example</h3>
<p>As fun as silly adding examples are, there will be undoubtedly someone who has been reading everything above (good job getting this far!) and cannot see a practical use for all of this. Since <a href="http://www.letour.fr/" title="Site officiel du tour de France">le Tour de France</a> is on, lets have a play on words and have an example on cycling (over a series of values).</p>
<h4 id="fig5">Cycling with Closures</h4>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #666666; font-style: italic;">// Cycle factory: takes a series of arguments</span><br />
<span style="color: #666666; font-style: italic;">// for the closure to cycle over.</span><br />
<span style="color: #000000; font-weight: bold;">function</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">func_get_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create closure using the supplied list of arguments</span><br />
&nbsp; &nbsp; <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: #339933;">&amp;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get current item</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Rewind if at the end of the list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$current</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Return the current item</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000088;">$current</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Create our list of CSS classes to cycle over</span><br />
<span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'odd'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'even'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">// List of adjectives, why not</span><br />
<span style="color: #000088;">$taste</span> <span style="color: #339933;">=</span> cycle<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'vile'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yummy'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'delicious'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Our list of items to be formatted into a pretty HTML list</span><br />
<span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'apple'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cherry'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'date'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'elderberry'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'figs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&lt;ul&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;li class=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> are <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$taste</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/li&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&lt;/ul&gt;</div></div>
<p>That example would provide the following output:</p>
<div class="codecolorer-container html4strict twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;odd&quot;</span>&gt;</span>apples are vile<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;even&quot;</span>&gt;</span>bananas are yummy<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;odd&quot;</span>&gt;</span>cherries are delicious<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;even&quot;</span>&gt;</span>dates are vile<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;odd&quot;</span>&gt;</span>elderberries are yummy<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;even&quot;</span>&gt;</span>figs are delicious<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span></div></div>
<p>Well, it&#8217;s a little bit more practical anyway!  Have you been playing with closures and/or anonymous functions too?</p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/07/08/anonymous-closures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP 5.3.0 Released</title>
		<link>http://cowburn.info/2009/06/30/php-530/</link>
		<comments>http://cowburn.info/2009/06/30/php-530/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 13:12:45 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[php.net]]></category>
		<category><![CDATA[php5.3.0]]></category>

		<guid isPermaLink="false">http://cowburn.info/?p=224</guid>
		<description><![CDATA[Today the latest stable version of PHP was announced, PHP 5.3.0. Go download and play with the new features available such as namespaces, LSB, anonymous functions and closures along with many more bug fixes, additions and improvements. My favourite addition at the moment is the improvements made to the DateTime extension with new classes such [...]]]></description>
			<content:encoded><![CDATA[<p>Today the latest stable version of PHP was announced, <a href="http://php.net/releases/5_3_0.php" rel="external">PHP 5.3.0</a>.  </p>
<p>Go download and play with the new features available such as <a href="http://php.net/namespaces">namespaces</a>, <a href="http://php.net/lsb" title="late static binding">LSB</a>, <a href="http://php.net/closures">anonymous functions and closures</a> along with many more bug fixes, additions and improvements.<span id="more-224"></span></p>
<p>My favourite addition at the moment is the improvements made to the <a href="http://php.net/datetime">DateTime</a> extension with new classes such as <a href="http://php.net/dateperiod">DatePeriod</a> (for iterating over dates/times) and <a href="http://php.net/dateinterval">DateInterval</a> and additions to the DateTime class such as <a href="http://php.net/datetime.createFromFormat">DateTime::createFromFormat</a> and <a href="http://php.net/datetime.add">DateTime::add</a> (and sub).</p>
<p>The anonymous functions will also be very useful where one might previously use <a href="http://php.net/create_function">create_function</a> (or a normal non-anonymous function) in particular for callbacks to other functions/methods.</p>
<div class="codecolorer-container php twitlight moo-highlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">preg_replace_callback</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[A-Z]/'</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;">$match</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #990000;">str_rot13</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Uello Jorld!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>So, download it and have an explore. <img src='http://cowburn.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cowburn.info/2009/06/30/php-530/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

