<?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"
	>

<channel>
	<title>Håvard Eide</title>
	<atom:link href="http://eide.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://eide.org</link>
	<description>eide.org</description>
	<pubDate>Fri, 10 Oct 2008 11:33:02 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>elePHPant</title>
		<link>http://eide.org/2008/10/09/elephpant/</link>
		<comments>http://eide.org/2008/10/09/elephpant/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 18:40:24 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://eide.org/?p=108</guid>
		<description><![CDATA[I got a elePHPant after the testfest, and just in time for a roadtrip to Stavanger ( 1000km roundtrip ), I went over with a car to move my sister and her boyfriend ( with all their stuff ) to Oslo. I brought the elePHPant with me and got some shots of it.

Make sure to [...]]]></description>
			<content:encoded><![CDATA[<p>I got a <a href="http://www.nexen.net/elephpant.php">elePHPant</a> after the <a href="http://qa.php.net/testfest.php">testfest</a>, and just in time for a roadtrip to <a href="http://en.wikipedia.org/wiki/Stavanger">Stavanger</a> ( 1000km roundtrip ), I went over with a car to move my sister and her boyfriend ( with all their stuff ) to Oslo. I brought the elePHPant with me and got some shots of it.<br />
<span id="more-108"></span><br />
<div id="attachment_109" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/10/php_2008_1.jpg"><img src="http://eide.org/wp-content/uploads/2008/10/php_2008_1-300x199.jpg" alt="In stavanger" title="php_2008_1" width="300" height="199" class="size-medium wp-image-109" /></a><p class="wp-caption-text">In stavanger</p></div><div id="attachment_110" class="wp-caption alignnone" style="width: 209px"><a href="http://eide.org/wp-content/uploads/2008/10/php_2008_2.jpg"><img src="http://eide.org/wp-content/uploads/2008/10/php_2008_2-199x300.jpg" alt="taking a look at the local dance scene" title="php_2008_2" width="199" height="300" class="size-medium wp-image-110" /></a><p class="wp-caption-text">taking a look at the local dance scene</p></div><div id="attachment_111" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/10/php_2008_3.jpg"><img src="http://eide.org/wp-content/uploads/2008/10/php_2008_3-300x199.jpg" alt="a view to Stavanger town" title="php_2008_3" width="300" height="199" class="size-medium wp-image-111" /></a><p class="wp-caption-text">a view to Stavanger town</p></div><div id="attachment_112" class="wp-caption alignnone" style="width: 209px"><a href="http://eide.org/wp-content/uploads/2008/10/php_2008_4.jpg"><img src="http://eide.org/wp-content/uploads/2008/10/php_2008_4-199x300.jpg" alt="checking out a oil drill" title="php_2008_4" width="199" height="300" class="size-medium wp-image-112" /></a><p class="wp-caption-text">checking out a oil drill</p></div><div id="attachment_113" class="wp-caption alignnone" style="width: 209px"><a href="http://eide.org/wp-content/uploads/2008/10/php_2008_5.jpg"><img src="http://eide.org/wp-content/uploads/2008/10/php_2008_5-199x300.jpg" alt="on the road back to Oslo" title="php_2008_5" width="199" height="300" class="size-medium wp-image-113" /></a><p class="wp-caption-text">on the way back to Oslo, checking out the scenery </p></div><div id="attachment_114" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/10/php_2008_6.jpg"><img src="http://eide.org/wp-content/uploads/2008/10/php_2008_6-300x200.jpg" alt="my sisters boyfriend telling the elePHPant to take it easy" title="php_2008_6" width="300" height="200" class="size-medium wp-image-114" /></a><p class="wp-caption-text">my sisters boyfriend telling the elePHPant to take it easy</p></div><div id="attachment_115" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/10/php_2008_7.jpg"><img src="http://eide.org/wp-content/uploads/2008/10/php_2008_7-300x200.jpg" alt="elePHPant checking out the scenery on the top of the mountain" title="php_2008_7" width="300" height="200" class="size-medium wp-image-115" /></a><p class="wp-caption-text">taking in the view</p></div><div id="attachment_116" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/10/php_2008_8.jpg"><img src="http://eide.org/wp-content/uploads/2008/10/php_2008_8-300x200.jpg" alt="the little know migration path for elePHPants" title="php_2008_8" width="300" height="200" class="size-medium wp-image-116" /></a><p class="wp-caption-text">the little know migration path for elePHPants</p></div></p>
<p>Make sure to check out <a href="http://www.elephpantworldtour.com/">pictures of elePHPant</a> around the world.</p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/10/09/elephpant/feed/</wfw:commentRss>
		</item>
		<item>
		<title>National Rap Show</title>
		<link>http://eide.org/2008/10/03/national-rap-show/</link>
		<comments>http://eide.org/2008/10/03/national-rap-show/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 19:19:24 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[hiphop]]></category>

		<category><![CDATA[music]]></category>

		<guid isPermaLink="false">http://eide.org/?p=105</guid>
		<description><![CDATA[In Norway we have a rap show on the radio that have been going on for 15 years around these days, and tonight the first of several celebration shows is on air! Lots of classics, make sure to check out the website and tune in to the playlist when that comes online.
That said: nrk.no is [...]]]></description>
			<content:encoded><![CDATA[<p>In Norway we have a rap show on the radio that have been going on for 15 years around these days, and tonight the first of several celebration shows is on air! Lots of classics, make sure to check out the <a href="http://nrk.no/p3/program/national_rap_show/">website</a> and tune in to the <a href="http://www.nrk.no/spillelister/national_rap_show.splist">playlist</a> when that comes online.</p>
<p>That said: <a href="http://nrk.no">nrk.no</a> is a national broadcast company that requires you to install Silverlight in order to listen in to the old shows&#8230;. so much for my tax-money working in my advantage&#8230; They should seriously re-think their strategy, maybe talk to <a href="http://nrkbeta.no/">nrkbeta.no</a>, which is their own company, experimenting with torrents ( they put some of their shows online for free there ). Make them come up with a player, or just a good &#8216;ol stream that can be played from wherever.</p>
<p>Anyway, not going to rant tonight: congrats to the national rap show, and let&#8217;s hope they continue for another 15 years!</p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/10/03/national-rap-show/feed/</wfw:commentRss>
		</item>
		<item>
		<title>whisky tasted</title>
		<link>http://eide.org/2008/08/26/whisky-tasted/</link>
		<comments>http://eide.org/2008/08/26/whisky-tasted/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 19:03:54 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[whisky]]></category>

		<guid isPermaLink="false">http://eide.org/?p=101</guid>
		<description><![CDATA[The people that know me have over the years *slowly* figured out that I&#8217;m a big whisky fan: I&#8217;m a member of the norwegian malt whisky association, me and some friends come together to taste whiskies and enjoy the flavors that it brings. If I have to choose I&#8217;d go for some Islay whisky, but [...]]]></description>
			<content:encoded><![CDATA[<p>The people that know me have over the years *slowly* figured out that I&#8217;m a big whisky fan: I&#8217;m a member of the <a href="http://nmwl.no/">norwegian malt whisky association</a>, me and some friends come together to taste whiskies and enjoy the flavors that it brings. If I have to choose I&#8217;d go for some <a href="http://en.wikipedia.org/wiki/Islay_whisky">Islay whisky</a>, but tasting is fun, so every now and then I&#8217;ll gather up some notes when I taste something new or have had a tasting night with the nmwl or my friends.<br />
<span id="more-101"></span><br />
Today I&#8217;ll go through a couple of tasting sessions from this summer and up until now.</p>
<h3>The arran tasting</h3>
<p> <div id="attachment_102" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/08/arran_tasting.jpg"><img src="http://eide.org/wp-content/uploads/2008/08/arran_tasting-300x200.jpg" alt="arran" title="arran_tasting" width="300" height="200" class="size-medium wp-image-102" /></a><p class="wp-caption-text">arran</p></div> <a href="http://www.arranwhisky.com/">Arran</a> is a distillery that I&#8217;ve had some experience with over the last couple of years, and earlier this summer we got together to taste no fewer than 6 arran bottles that we collectively had ( or bought for the tasting ), in the order we tasted it</p>
<ul>
<li>10yo<br />This is the standard Arran bottle that you&#8217;d get in Norway, a typical summer whisky for my part: some honey, apples, citrus and a fair bit of malt and the oak cask it spent a decade on. This is a bottle that I try to have at home: summery and affordable, great buy!</li>
<li>7yo<br/>This is a special bottling for <a href="http://www.jonbertelsen.com/">Jon Bertelsen</a> ( mostly know for his Cognac bottlings ) that has aged 7 years: this was the &#8220;worst&#8221; of the bottlings we had: it shines through that it&#8217;s just 7 years, but you can absolutely see where it&#8217;s going compared to the 10yo. Some fruit and vanilla when nosing it, but it was a bit raw on the taste.</li>
<li>Single cask #1038<br />Bottle 220 of 240 from this bourbon cask. The single casks from Arran are beautiful bottles, we&#8217;ve had some arrive in Norway that are just stunning, and this is one of them: smelling of caramell and vanilla, taste: you could really feel the cask here, some liquorish in-between and a splash of water got me some peach. At this time we were sitting 4 people around the table just smiling, and we had still 3 more bottles to taste!</li>
<li>Single cask #2131<br/>Bottle 23 of 196. This cask reached legendary status in Norway last year when it came out: a bourbon cask so dark you wouldn&#8217;t believe it! This is one of the better bottles that I&#8217;ve ever had in my possession, on the nose there&#8217;s a explosion of smells: rum, vanilla, cocoa and the taste is smooth, so smooth that the entire group went quiet and all you could hear was the people enjoying the taste: dark vanilla, liquorish, cocoa and the longest finish I think I&#8217;ve had in a bottle of whisky! Winner of the night!</li>
<li>Single cask #24<br/>Bottle 364 of 669 from a sherry cask. Here the sherry cask came out and the summer associations that I always have with the 10yo came back, just a lot smoother.</li>
<li>Cask strength<br />The last one of the evening: vanilla, spices, liquorish, maybe a bit harsh on the tongue, but a little water and it went down very well.</li>
</ul>
<p>We ended up discussing the arrans over a Macallan 18yo long into the night. A very special tasting!</p>
<h3>Balsam</h3>
<p>Me and some friends got together at <a href="http://vibb.no/Company.asp?CompanyID=8885">Balsam</a> in Oslo one night and tasted:</p>
<ul>
<li>Glengoyne 10yo<br />Not the big taste experience, quite dull actually</li>
<li>Glengoyne 18yo Billy&#8217;s Choice<br />This one came from a Amontillado Hogshead, nosing it there was a lot of chocolate and orange, but then it came to the taste: cask, and a lot of it! This was not a very plesant experience, I could taste some chocolate in-between, but in my opinion it was ruined by the cask. Maybe some like it ( actually: at first I could not figure out if it was a whisky or a cognac ), but this was too sweet and colored by the cask</li>
</ul>
<h3>nmwl meeting</h3>
<p>Last week me and a friend went to the nmwl meeting where there was a competition: 5 whiskies tasted blind, question: what cask was it stored on? This one was hard, really hard! But interesting, we sat down on teams of 5 and tasted and discussed the following bottles:</p>
<ul>
<li>Glen Douglas 2001 Unpeated American Oak #333 45%</li>
<li>Springbank 1991 16yo Rhum Wood 54,2%</li>
<li>Strathisla 1987 21yo Port Wood 57,1% (Cadenhead&#8217;s)</li>
<li>Inchmurrin 2002 Unpeated Madeira Puncheon #3 45%</li>
<li>Croftenga 2003 Heavily Peated Sherry Butt #1 45%</li>
</ul>
<p>Some very special casks there ( me drinking a lot of islay whisky ) and the taste on a couple of them was&#8230;.. not interesting, just bad. But it gave us a opportunity to taste some different casks.</p>
<p>Before the tasting I also got to taste a 1991 <a href="http://bruichladdich.com/">Bruichladdich</a> ( Berrys own ) that was very good, and a 2001 bottling from Bruichladdich that was specially bottled for the norwegian market ( and marks the start of the new batch after they got acquired.</p>
<h3>Dr.Jekylls</h3>
<p>At <a href="http://www.jekylls.no/">Dr.Jekylls</a> in Oslo I got the chance to taste the brand new <a href="http://www.ardbeg.com/">Ardbeg</a> Renaissance that is the first 10yo from the new batch: this is one to buy if you ever have been into Ardbeg of Islay whisky! The peat, honey and the loooong aftertaste was absolutely worth it! If you are ever in Oslo and want to taste some good whisky ( even though they are a bit pricy at times ): drop by this pub, they have a excellent sellection and a very good staff!</p>
<p>That&#8217;s it, a couple of whiskies tasted this summer, think I&#8217;ll blog a bit more often about this ( so that I can remember more on the taste! ) until then: slainte!</p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/08/26/whisky-tasted/feed/</wfw:commentRss>
		</item>
		<item>
		<title>RIP PHP4</title>
		<link>http://eide.org/2008/08/08/rip-php4/</link>
		<comments>http://eide.org/2008/08/08/rip-php4/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 06:12:01 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://eide.org/?p=94</guid>
		<description><![CDATA[With the release of 4.4.9 PHP4 is now officially dead. What can I say? Nothing much really: it should die silently and we&#8217;ll move along with PHP5/6 and be happy!
]]></description>
			<content:encoded><![CDATA[<p>With the release of <a href="http://www.php.net/archive/2008.php#id2008-08-07-1">4.4.9</a> PHP4 is now officially dead. What can I say? Nothing much really: it should die silently and we&#8217;ll move along with PHP5/6 and be happy!</p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/08/08/rip-php4/feed/</wfw:commentRss>
		</item>
		<item>
		<title>iterators</title>
		<link>http://eide.org/2008/08/05/iterators/</link>
		<comments>http://eide.org/2008/08/05/iterators/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 17:38:59 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://eide.org/?p=87</guid>
		<description><![CDATA[Today I&#8217;ll look at some iterators: a set of classes in the SPL that implements various iterating patterns: ArrayIterator, AppendIterator, FilterIterator, LimitIterator and NoRewindIterator. Hopefully you&#8217;ll get a idea of what these are capable of and that you can get some new ideas for your day-to-day tasks.

ArrayIterator
The ArrayIterator does exactly what it tells you: it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ll look at some <a href="http://en.wikipedia.org/wiki/Iterator">iterators</a>: a set of classes in the SPL that implements various iterating patterns: ArrayIterator, AppendIterator, FilterIterator, LimitIterator and NoRewindIterator. Hopefully you&#8217;ll get a idea of what these are capable of and that you can get some new ideas for your day-to-day tasks.<br />
<span id="more-87"></span></p>
<h3>ArrayIterator</h3>
<p>The <em>ArrayIterator</em> does exactly what it tells you: it&#8217;s used to iterate over an array of values, with it you can easily iterate over values like you would any other collection with a normal iterator. It has the ability to seek and rewind on the array in question and get the current key and value.</p>
<p>Let&#8217;s start with a simple example that shows the bare bones of what a ArrayIterator looks like:</p>

<div class="wp_syntax"><table><tr><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"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Array of php sites:</span>
<span style="color: #000033;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>  
    <span style="color: #0000ff;">&quot;php&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;php.net&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;pecl&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;pecl.php.net&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;pear&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;pear.php.net&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;bugs&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;bugs.php.net&quot;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ArrayObject wraps a array and gives you OOP</span>
<span style="color: #666666; font-style: italic;">// access to it for manipulation:</span>
<span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$array</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Add a new site to the ArrayObject:</span>
<span style="color: #000033;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">offsetSet</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;cvs&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;cvs.php.net&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// How many items are we iterating over?</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Iterating over: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000033;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; values<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// The foreach construct will call $obj-&gt;getIterator() which returns</span>
<span style="color: #666666; font-style: italic;">// an ArrayIterator to travers itself. That ArrayIterator implements </span>
<span style="color: #666666; font-style: italic;">// interface Iterator and can therefore be used in foreach.</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$value</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;Key=$key, value=$value<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>As you can see: the ArrayIterator is not really used directly, but implicit with the foreach construct. But you don&#8217;t have to use the builtin ArrayObject to use the ArrayIterator, in fact: you can add a array directly to the ArrayIterator and iterate over it, but here&#8217;s a example that use a custom class that implement the IteratorAggregate interface. The IteratorAggregate interface has a single function to implement <em>getIterator()</em>, this is the &#8220;magic&#8221; method called in the foreach construct in the previous example.</p>

<div class="wp_syntax"><table><tr><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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/**
 * Custom array class
 * It really doesn't do much functionality, all it
 * does is to check that the array elements we
 * are adding doesn't have a key and a value that
 * is the same.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> MyArray implements IteratorAggregate <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Collection of items</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$arr</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * Constructor, initialize a empty array   
	 */</span>
 	<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: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * Add a new array element to MyArray.
	 * Checks to see if the $key and the $value is
	 * the same before adding it to the internal array.
	 * ( yes: a really bogus example, but this is just for
	 * showing some functionality )
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> add<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$key</span><span style="color: #339933;">,</span> <span style="color: #000033;">$value</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">check</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$key</span><span style="color: #339933;">,</span> <span style="color: #000033;">$value</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">arr</span><span style="color: #009900;">&#91;</span><span style="color: #000033;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$value</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * Private function that checks if $key and $value
	 * are the same.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> check<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$key</span><span style="color: #339933;">,</span> <span style="color: #000033;">$value</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$key</span> <span style="color: #339933;">==</span> <span style="color: #000033;">$value</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;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/**
	 * The method that a class implementing the
	 * IteratorAggregate interface needs to implement.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIterator<span style="color: #009900;">&#40;</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;">new</span> ArrayIterator<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">arr</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>       
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This class can be used like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php"><span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</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;php.net&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Try to add a element where both key and value</span>
<span style="color: #666666; font-style: italic;">// are the same:</span>
<span style="color: #000033;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</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;php&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Here the getIterator() function will be called on the</span>
<span style="color: #666666; font-style: italic;">// MyArray object</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$value</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;key=$key, value=$value<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And the output will be ( note that the second element we tried to add isn&#8217;t here ):</p>
<pre>
key=php, value=php.net
</pre>
<p>So here we&#8217;ve looked at how ArrayObject uses the ArrayIterator and how we can create a custom class that can be used to iterate with foreach.</p>
<h3>AppendIterator</h3>
<p>In the example above we looked at how we can iterate over a single ArrayObject and how the IteratorAggregate interface works, but: if you have multiple elements you might want to iterate over all of these together, this is where the <em>AppendIterator</em> comes to help you. </p>
<p>The following example creates two ArrayObjects and a AppendIterator, there is only a single method here that you need to think about, the <em>append()</em> function that takes a Iterator as argument. The point of the iterator is to collect several collections and iterate over them in one go:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000033;">$sites</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;wiki.cc&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;planet-php.net&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$othersites</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;slashdot.org&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;maclash.org&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$one</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$sites</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$two</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$othersites</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$iterator</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AppendIterator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">append</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$one</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">append</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$two</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$iterator</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$site</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #000033;">$site</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>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will give you:</p>
<pre>
wiki.cc
planet-php.net
slashdot.org,
maclash.org
</pre>
<p>As you can see: with the AppendIterator you can collect up elements from several sources (objects) and display them all in one go, let&#8217;s look at a better example: we have a &#8216;CMS&#8217; where each type extends a Element class ( all elements in our CMS must be of the same type) , there are also element collections that collects up N-elements and provides us with a Iterator to iterate over all the module elements currently loaded.</p>
<p>To display all links and articles in our system we can come up with the following code</p>

<div class="wp_syntax"><table><tr><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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> Element <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000033;">$title</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #990000;">Link</span> <span style="color: #000000; font-weight: bold;">extends</span> Element <span style="color: #009900;">&#123;</span>
    <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: #000033;">$title</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Article <span style="color: #000000; font-weight: bold;">extends</span> Element <span style="color: #009900;">&#123;</span>
    <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: #000033;">$title</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> ElementCollection implements IteratorAggregate <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$elements</span><span style="color: #339933;">;</span>
    <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: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elements</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addElement<span style="color: #009900;">&#40;</span> Element <span style="color: #000033;">$module</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elements</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">append</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$module</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getIterator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">elements</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> ArticleCollection <span style="color: #000000; font-weight: bold;">extends</span> ElementCollection <span style="color: #009900;">&#123;</span>
    <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>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">collectArticles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> collectArticles<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Do some fancy DB stuff</span>
        <span style="color: #000033;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Article<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Article one&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000033;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Article<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Article two&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$a</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$b</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> LinkCollection <span style="color: #000000; font-weight: bold;">extends</span> ElementCollection <span style="color: #009900;">&#123;</span>
    <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>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">collectLinks</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> collectLinks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Do some fancy DB stuff</span>
        <span style="color: #000033;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">Link</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Link one&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000033;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">Link</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Link two&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$a</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$b</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/*
 * Now for the magic: we create two collections and append
 * the iterator for each collection to a single AppendIterator.
 * We can now iterate over all elements in one go and display
 * the module title.
 */</span>
<span style="color: #000033;">$article</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArticleCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$link</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$it</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AppendIterator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$it</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">append</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$article</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$it</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">append</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$link</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$it</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$module</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #000033;">$module</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</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>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will give you:</p>
<pre>
Article one
Article two
Link one
Link two
</pre>
<p>and you have successfully collected elements together to iterate over in one foreach loop.</p>
<h3>FilterIterator</h3>
<p>There are many ways to iterate over a collection of items, but you might not want to display everything at any given time; say hello to the <em>FilterIterator</em>, a abstract class that have one function you will need to implement: <em>accept</em>.</p>

<div class="wp_syntax"><table><tr><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
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/**
 * Our filter, it implements the accept() function,
 * this is where we decide what will be accepted by
 * the foreach further down
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Filter <span style="color: #000000; font-weight: bold;">extends</span> FilterIterator <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/**
     * The filteriterator needs a iterator as param:
     */</span>
	<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> Iterator <span style="color: #000033;">$it</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$it</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/**
     *  When we iterate over the array the following
     *  function will be called to see wether we should
     *  accept the current key:
     */</span> 
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> accept<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;/^php/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getInnerIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</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;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>       
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000033;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>  
    <span style="color: #0000ff;">&quot;php&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;php.net&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;php_pecl&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;pecl.php.net&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;perl&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;perl.org&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;python&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;python.org&quot;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$arr</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000033;">$it</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Filter<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$it</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$value</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;key=$key, value=$value<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Running this will give us ( as we would suspect ) the following:</p>
<pre>
key=php, value=php.net
key=php_pecl, value=pecl.php.net
</pre>
<p>And we have successfully filtered out anything where the key doesn&#8217;t start with <em>php</em>.<br />
A FilterIterator can be very useful when you are iterating over items and want to automaticly check the current item before outputting it ( a &#8220;adult filter&#8221; can be one example of what you want to implement ).</p>
<h3>LimitIterator</h3>
<p>Now: you have a large result set, but only want to display a certain part of it, what do you do? Create a counter, increment it and see if we have reached the amount for the current page? No: you create a <em>LimitIterator</em> that will do it for you. The LimitIterator is a class that keeps track of how many items there are in a iterator and display the number you specify in the constructor.</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Array with items:</span>
<span style="color: #000033;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;php&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;php.net&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;perl&quot;</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;perl.org&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;python&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;python.org&quot;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Array object with all our web sites</span>
<span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$arr</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #666666; font-style: italic;">/*
 *  The limit iterator takes a iterator as the first
 *  param, then where you want to start, and finally how
 *  many you want to display:
 */</span>
<span style="color: #000033;">$it</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LimitIterator<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span>  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$it</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$value</span> <span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;key=$key, value=$value<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>   
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>We tell the LimitIterator to display from item 0, and show 1 item, running this we will see:</p>
<pre>
key=php, value=php.net
</pre>
<p>which is correct.</p>
<p>Let&#8217;s create three &#8220;pages&#8221; where we display one item for each page ( not the best of examples, but it will demonstrate the effect of passing a new starting point to the LimitIterator )</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Array with items:</span>
<span style="color: #000033;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;php&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;php.net&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;perl&quot;</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;perl.org&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;python&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;python.org&quot;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Array object with all our web sites</span>
<span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$arr</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000033;">$page</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$perPage</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000033;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #000033;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>----Page #&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000033;">$page</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>
    <span style="color: #000033;">$it</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LimitIterator<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$page</span><span style="color: #339933;">,</span> <span style="color: #000033;">$perPage</span>  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$it</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000033;">$value</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;key=$key, value=$value<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>   
    <span style="color: #000033;">$page</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will give the following output:</p>
<pre>
----Page #0-----------
key=php, value=php.net

----Page #1-----------
key=perl, value=perl.org

----Page #2-----------
key=python, value=python.org
</pre>
<p>That&#8217;s it for creating a easy LimitIterator over a Iterator, this can easily be implemented to split up large reports into separate files for every N-elements for example, but only you can set the limits on what to use it for.</p>
<h3>NoRewindIterator</h3>
<p>The <em>NoRewindIterator</em> Iterator is a iterator that doesn&#8217;t call rewind(), so it is a Iterator that can only be used once.</p>
<p>First: let&#8217;s look at a normal Iterator:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000033;">$sites</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;wiki.cc&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;planet-php.net&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$sites</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$arr</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$site</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #000033;">$site</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>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$arr</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$site</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #000033;">$site</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>
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will output:</p>
<pre>
wiki.cc
planet-php.net
wiki.cc
planet-php.net
</pre>
<p>But: if we only want the user of a iterator to be able to iterate over a collection once we can use the NoRewindIterator:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000033;">$sites</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;wiki.cc&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;planet-php.net&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$sites</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$it</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NoRewindIterator<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$arr</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$it</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$site</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #000033;">$site</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>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$it</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$site</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #000033;">$site</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>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>this will give you:</p>
<pre>
wiki.cc
planet-php.net
</pre>
<p>so you can see that we only get the output once, even though we tried to iterate over the collection two times.</p>
<p>The iterators in SPL is a valuable collection that you should have a look at in your day-to-day programming tasks since it has a set of common ways on how to implement various iterating tasks, this will help you  and others that will look at your code since they share the commonality of the SPL iterators.</p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/08/05/iterators/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Countable</title>
		<link>http://eide.org/2008/07/30/countable/</link>
		<comments>http://eide.org/2008/07/30/countable/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 20:06:42 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://eide.org/?p=80</guid>
		<description><![CDATA[Today I will look at the Countable interface, it has a single function that needs to be implemented: count(), by implementing this you can ensure that there is a count() function ready to use on any given class that implements it. The Countable interface is used in other places in the SPL as well: the [...]]]></description>
			<content:encoded><![CDATA[<p>Today I will look at the Countable interface, it has a single function that needs to be implemented: <em>count()</em>, by implementing this you can ensure that there is a <em>count()</em> function ready to use on any given class that implements it. The Countable interface is used in other places in the SPL as well: the ArrayIterator and ArrayObject classes implements this interface ( and SqliteResult if present )<br />
<span id="more-80"></span><br />
First we&#8217;ll look at a simple example that keeps track of my sites:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> WebSiteCollection implements Countable <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$sites</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: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sites</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
        	<span style="color: #0000ff;">&quot;http://eide.org&quot;</span><span style="color: #339933;">,</span>
        	<span style="color: #0000ff;">&quot;http://phpvolcano.com&quot;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sites</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000033;">$sites</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WebSiteCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I have &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000033;">$sites</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; sites&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Running this you will get:<br />
<em>I have 2 sites</em><br />
As you can see: we call <em>count()</em> on the object and we get how many sites I have running, but: that is not the good thing with <em>Countable</em>: it also hooks into the native <em>count()</em> functionality so that you can do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> WebSiteCollection implements Countable <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$sites</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: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sites</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
        	<span style="color: #0000ff;">&quot;http://eide.org&quot;</span><span style="color: #339933;">,</span>
        	<span style="color: #0000ff;">&quot;http://phpvolcano.com&quot;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sites</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000033;">$sites</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WebSiteCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I have &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$sites</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; sites&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Which will output the same as the first example.</p>
<p>The above examples are very basics, but the power of <em>Countable</em> also lies in the fact that you can manipulate the count within the <em>count()</em> function before returning it, making it respond to a certain flag like the following example where we can set a TLD that we want the <em>count()</em> function to restrict to.</p>

<div class="wp_syntax"><table><tr><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
30
31
32
33
34
35
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> WebSiteCollection implements Countable <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$sites</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$tldrestriction</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: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sites</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> 
        	<span style="color: #0000ff;">&quot;http://eide.org&quot;</span><span style="color: #339933;">,</span>
        	<span style="color: #0000ff;">&quot;http://phpvolcano.com&quot;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tldrestriction</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">null</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> restrictToTld<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$tld</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	<span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tldrestriction</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$tld</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> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #339933;">===</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tldrestriction</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    		<span style="color: #b1b100;">return</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sites</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000033;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sites</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$site</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tldrestriction</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;$/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000033;">$site</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        		<span style="color: #000033;">$count</span><span style="color: #339933;">++;</span>
        	<span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000033;">$count</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000033;">$sites</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WebSiteCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I have &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$sites</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; sites<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$sites</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">restrictToTld</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;org&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;I have &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$sites</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; sites<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And the output for this will be:<br />
<em>I have 2 sites<br />
I have 1 sites<br />
</em></p>
<p>This has been a short post, but the <em>Countable</em> interface is really easy to use and there is as said only a single function to implement, and by using it you can ensure that your class correctly will return the current number of elements when you start to filter out elements and such.</p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/07/30/countable/feed/</wfw:commentRss>
		</item>
		<item>
		<title>php testfest</title>
		<link>http://eide.org/2008/07/30/php-testfest/</link>
		<comments>http://eide.org/2008/07/30/php-testfest/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 19:05:46 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://eide.org/?p=76</guid>
		<description><![CDATA[Well, the testfest 2008 is wrapping up and a lot of the tests are now a part of the php distribution. php.no had a session where Hannes Magnusson introduced us to writing tests. I focused on some SPL areas that didn&#8217;t have coverage, and I got myself a elephant! ( check out the flickr group [...]]]></description>
			<content:encoded><![CDATA[<p>Well, the <a href="http://qa.php.net/testfest.php">testfest 2008</a> is wrapping up and a lot of the tests are now a part of the php distribution. <a href="http://php.no/node/260">php.no</a> had a session where <a href="http://bjori.blogspot.com/">Hannes Magnusson</a> introduced us to writing tests. I focused on some SPL areas that didn&#8217;t have coverage, and I <a href="http://www.php.net/archive/2008.php#id2008-07-30-1">got myself a elephant</a>! ( check out the <a href="http://www.flickr.com/groups/elephpants/">flickr group</a> if you are unfamiliar with it)</p>
<p>The testfest is a great introduction to the internals of php and will teach you a lot when it comes to the behavior of the functionality you touch on, so next year: meet up at your local testfest and participate in getting the code coverage for php even higher ( or just digg into writing tests now! ).</p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/07/30/php-testfest/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Beitostøylen</title>
		<link>http://eide.org/2008/07/28/beitostoylen/</link>
		<comments>http://eide.org/2008/07/28/beitostoylen/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 21:10:20 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[pictures]]></category>

		<category><![CDATA[fishing]]></category>

		<category><![CDATA[vacation]]></category>

		<category><![CDATA[walk]]></category>

		<guid isPermaLink="false">http://eide.org/?p=68</guid>
		<description><![CDATA[Well, back from vacation out west at my parents house and a week at beitost&#248;len. The first week was kinda bad with rain and really bad weather, but the final week I spent with some friends in a cabin doing some walking in the mountain area of Jotunheimen. Since I recently got a new Canon [...]]]></description>
			<content:encoded><![CDATA[<p>Well, back from vacation out west at my parents house and a week at <a href="http://en.wikipedia.org/wiki/Beitostølen">beitost&oslash;len</a>. The first week was kinda bad with rain and really bad weather, but the final week I spent with some friends in a cabin doing some walking in the mountain area of <a href="http://en.wikipedia.org/wiki/Jotunheimen">Jotunheimen</a>. Since I recently got a new <a href="http://en.wikipedia.org/wiki/450D">Canon 450D</a> and took a lot of pictures I&#8217;m posting some here.<br />
<span id="more-68"></span><br />
The Jotunheimen area is really beautiful ( especially when you have weather like I had last week ) and is absolutely recommended if you happen to pass by. </p>
<p>First up: me and St&aring;le went flyfishing a couple of days, didn&#8217;t catch anything, but the scenery was nice:<br />
<div id="attachment_61" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/07/beito2008_24.jpg"><img src="http://eide.org/wp-content/uploads/2008/07/beito2008_24-300x200.jpg" alt="Flyfishing" title="beito2008_24" width="300" height="200" class="size-medium wp-image-61" /></a><p class="wp-caption-text">Driving into the fishing area</p></div><br />
<div id="attachment_62" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/07/beito2008_31.jpg"><img src="http://eide.org/wp-content/uploads/2008/07/beito2008_31-300x199.jpg" alt="Me doing some flyfishing" title="beito2008_31" width="300" height="199" class="size-medium wp-image-62" /></a><p class="wp-caption-text">me doing some flyfishing</p></div></p>
<h3>Besseggen</h3>
<p>We also walked <a href="http://en.wikipedia.org/wiki/Besseggen">Besseggen</a>, a very famous hike with some 40k people every year taking it. It is also made famous in the <a href="http://en.wikipedia.org/wiki/Peer_Gynt">Peer Gynt</a> play by <a href="http://en.wikipedia.org/wiki/Henrik_Ibsen">Henrik Ibsen</a>. A beautiful walk in the sun, a bit of climbing where you have to make sure not to drop 400 meters down to the water below, but absolutely worth it!<br />
<div id="attachment_63" class="wp-caption alignnone" style="width: 210px"><a href="http://eide.org/wp-content/uploads/2008/07/beito2008_71.jpg"><img src="http://eide.org/wp-content/uploads/2008/07/beito2008_71-200x300.jpg" alt="Starting out at Gjendesheim" title="beito2008_71" width="200" height="300" class="size-medium wp-image-63" /></a><p class="wp-caption-text">Starting out at Gjendesheim</p></div><br />
<div id="attachment_64" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/07/beito2008_80.jpg"><img src="http://eide.org/wp-content/uploads/2008/07/beito2008_80-300x200.jpg" alt="We walked a alternate route around the Gjendevatnet" title="beito2008_80" width="300" height="200" class="size-medium wp-image-64" /></a><p class="wp-caption-text">We walked a alternate route around the Gjendevatnet</p></div><br />
<div id="attachment_65" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/07/beito2008_109.jpg"><img src="http://eide.org/wp-content/uploads/2008/07/beito2008_109-300x200.jpg" alt="The view from Besseggen" title="beito2008_109" width="300" height="200" class="size-medium wp-image-65" /></a><p class="wp-caption-text">The view from Besseggen</p></div><br />
<div id="attachment_66" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/07/beito2008_122.jpg"><img src="http://eide.org/wp-content/uploads/2008/07/beito2008_122-300x200.jpg" alt="Besseggen with the bessvatnet to the left" title="beito2008_122" width="300" height="200" class="size-medium wp-image-66" /></a><p class="wp-caption-text">Besseggen with the bessvatnet to the right and a 400m drop to the left from the lowest point of the edge</p></div><br />
<div id="attachment_67" class="wp-caption alignnone" style="width: 310px"><a href="http://eide.org/wp-content/uploads/2008/07/beito2008_135.jpg"><img src="http://eide.org/wp-content/uploads/2008/07/beito2008_135-300x200.jpg" alt="Almost at the top, looking down to Gjendesheim" title="beito2008_135" width="300" height="200" class="size-medium wp-image-67" /></a><p class="wp-caption-text">Almost at the top, looking down to Gjendesheim</p></div></p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/07/28/beitostoylen/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SplObjectStorage</title>
		<link>http://eide.org/2008/07/21/splobjectstorage/</link>
		<comments>http://eide.org/2008/07/21/splobjectstorage/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 10:00:06 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://eide.org/?p=28</guid>
		<description><![CDATA[In this post I will look at SplObjectStorage: a container that allows to store objects uniquly without the need to compare them one by one.

Adding objects
So, what can the SplObjectStorage do for you? Let&#8217;s see how it behaves when we start to use it.

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
&#60;?php
&#160;
/**
 *  Test class that we will store in the
 * [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will look at SplObjectStorage: a container that allows to store objects <em>uniquly</em> without the need to compare them one by one.<br />
<span id="more-28"></span></p>
<h3>Adding objects</h3>
<p>So, what can the SplObjectStorage do for you? Let&#8217;s see how it behaves when we start to use it.</p>

<div class="wp_syntax"><table><tr><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"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/**
 *  Test class that we will store in the
 *  SplObjectStorage object.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> StorageTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$title</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: #000033;">$title</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$title</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> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000033;">$storage</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SplObjectStorage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StorageTest<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;wiki.cc&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$obj2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StorageTest<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;eide.org&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$storage</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$o</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">echo</span> <span style="color: #000033;">$o</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>And this will, as you most likely would think, display the following:</p>
<pre>
wiki.cc
eide.org
</pre>
<p>The SplObjectStorage class implements Iterator and Countable, so we can use the storage object as any other objects that implement these. Just look at this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php"><span style="color: #990000;">echo</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000033;">$storage</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Will output &#8220;2&#8243;</p>
<h3>Adding the same object</h3>
<p>The SplObjectStorage is, as the name suggests, a storage, so what will happen if we add the same object twice?</p>

<div class="wp_syntax"><table><tr><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
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/**
 *  Test class that we will store in the
 *  SplObjectStorage object.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> StorageTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$title</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: #000033;">$title</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$title</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> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000033;">$storage</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SplObjectStorage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StorageTest<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;www.eide.org&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$storage</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$o</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">echo</span> <span style="color: #000033;">$o</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>We now get:</p>
<pre>
www.eide.org
</pre>
<p>as output. So: the SplObjectStorage object can tell if you add the same object more than one time, this way we can keep adding N objects to the storage, and we can be sure that when we iterate over the object we will only get unique objects.</p>
<h3>Updating objects</h3>
<p>What then, if we update a object, will we get more than one object when we iterate over the storage?</p>

<div class="wp_syntax"><table><tr><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
30
31
32
33
34
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/**
 *  Test class that we will store in the
 *  SplObjectStorage object.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> StorageTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$title</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: #000033;">$title</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$title</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> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</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> setTitle<span style="color: #009900;">&#40;</span> <span style="color: #000033;">$title</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000033;">$storage</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SplObjectStorage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StorageTest<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;wiki.cc&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTitle</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;eide.org&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$storage</span> <span style="color: #b1b100;">as</span> <span style="color: #000033;">$o</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">echo</span> <span style="color: #000033;">$o</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>We now get:</p>
<pre>
eide.org
</pre>
<p>So: we can see that the storage object keeps track of the fact that we are manipulating the same object, and we can safely manipulate objects when adding them to the storage.</p>
<h3>Check for objects added</h3>
<p>With a storage like SplObjectStorage it is always interesting to check if a certain item has been added or not, this feature is covered with the <em>contains()</em> function:</p>

<div class="wp_syntax"><table><tr><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
30
31
32
33
34
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/**
 *  Test class that we will store in the
 *  SplObjectStorage object.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> StorageTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$title</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: #000033;">$title</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$title</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> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000033;">$storage</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SplObjectStorage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StorageTest<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;wiki.cc&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$obj2</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StorageTest<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;eide.org&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">contains</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;storage contains the object<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;storage does NOT contain the object<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">contains</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj2</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;storage contains the object<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;storage does NOT contain the object<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will give us the following output:</p>
<pre>
storage contains the object
storage does NOT contain the object
</pre>
<p>And you can see that SplObjectStorage correctly detects which of the two objects that has been added to it.</p>
<h3>Detaching objects</h3>
<p>With adding objects there always comes the time when you want to remove a object: this is covered by the <em>detach()</em> function:</p>

<div class="wp_syntax"><table><tr><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
30
31
32
33
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/**
 *  Test class that we will store in the
 *  SplObjectStorage object.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> StorageTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000033;">$title</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: #000033;">$title</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000033;">$title</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> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000033;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000033;">$storage</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SplObjectStorage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StorageTest<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;eide.org&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">attach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">contains</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;storage contains the object<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;storage does NOT contain the object<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">detach</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$storage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">contains</span><span style="color: #009900;">&#40;</span> <span style="color: #000033;">$obj</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;storage contains the object<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;storage does NOT contain the object<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will give us:</p>
<pre>
storage contains the object
storage does NOT contain the object
</pre>
<h3>Finally</h3>
<p>The SplObjectStorage object is a nice addition to the SPL family that will ease the use of object collections without worrying about what objects you already have added to the mix.</p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/07/21/splobjectstorage/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Hammer throwing</title>
		<link>http://eide.org/2008/07/19/hammer-throwing/</link>
		<comments>http://eide.org/2008/07/19/hammer-throwing/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 20:47:14 +0000</pubDate>
		<dc:creator>Håvard Eide</dc:creator>
		
		<category><![CDATA[sport]]></category>

		<guid isPermaLink="false">http://eide.org/?p=33</guid>
		<description><![CDATA[Congratulations to Trude and Marte from my track &#038; field team ( gfil ) with silver and bronze in todays national championship in women&#8217;s hammer throwing competition!
Ok: why this interest in hammer throwing? Some people might not know, but I have a history that might have eluded some: I&#8217;ve been throwing hammer since I was [...]]]></description>
			<content:encoded><![CDATA[<p>Congratulations to Trude and Marte from my track &#038; field team ( <a href="http://gfil.no/">gfil</a> ) with silver and bronze in todays national championship in women&#8217;s hammer throwing competition!<br />
Ok: why this interest in hammer throwing? Some people might not know, but I have a history that might have eluded some: I&#8217;ve been throwing hammer since I was 15 years old, I have one gold medal in the Norwegian championship for boys 16yo and actually was on the under 23 national team on one competition!</p>
<p>This week was actually sort of a &#8220;comeback&#8221; when it comes to hammer throwing for me: I had a session on tuesday night then went to a competition in <a href="http://hyen.com/">Hyen</a> on wednesday afternoon! Of course: I threw 45.5 meters on tuesday and only just over 43 on wednesday, but I was so sore after the tuesday night session that I was not able to throw any further ( note to self: don&#8217;t do that again&#8230; ). If my hand behaves ( I&#8217;ve had RSI for a long time that have prevented me from throwing ) I will try to get to the national championship next year, shouldn&#8217;t be too hard.</p>
<p>This leads to another question: am I the only programmer that also enjoys a good hammer throwing competition? Let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://eide.org/2008/07/19/hammer-throwing/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
