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

<channel>
	<title>jakefolio</title>
	<atom:link href="http://jakefolio.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jakefolio.com</link>
	<description></description>
	<lastBuildDate>Thu, 01 Dec 2011 22:08:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Callback Filter Iterator in PHP 5.3/5.4</title>
		<link>http://jakefolio.com/2011/12/callback-filter-iterator-in-php/</link>
		<comments>http://jakefolio.com/2011/12/callback-filter-iterator-in-php/#comments</comments>
		<pubDate>Thu, 01 Dec 2011 22:04:43 +0000</pubDate>
		<dc:creator>jake</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://jakefolio.com/?p=61</guid>
		<description><![CDATA[For anyone who has met me, they would tell you I love the PHP iterators.  I have shared how I feel that are totally under utilized, http://speakerdeck.com/u/jakefolio/p/unsung-heroes-of-php.  The biggest down fall to Iterators in PHP is the documentation.  The documentation is &#8230; <a href="http://jakefolio.com/2011/12/callback-filter-iterator-in-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For anyone who has met me, they would tell you I love the PHP iterators.  I have shared how I feel that are totally under utilized, <a href="http://speakerdeck.com/u/jakefolio/p/unsung-heroes-of-php">http://speakerdeck.com/u/jakefolio/p/unsung-heroes-of-php</a>.  The biggest down fall to Iterators in PHP is the documentation.  The documentation is very limited and it&#8217;s not always clear when is a good time to use an iterator.</p>
<p>Back in June of this year, I was looking at the documentation on <a href="http://www.php.net/manual/en/spl.iterators.php">http://www.php.net/manual/en/spl.iterators.php</a> and I found there was a new iterator, YAY! This Iterator is built for PHP 5.4+, but obviously I can&#8217;t/don&#8217;t want to run PHP 5.4 in  production yet.  I liked the concept of the iterator so much I decided to create it to be used in PHP 5.3+</p>
<p>The Filter Iterator is probably my second favorite iterator, next to Directory Iterator.  There are many great use cases for the Filter Iterator, and when you do filter the original data is left untouched. A Filter Iterator is really simple to use, create a class that extends FilterIterator and adjust the accept method to meet your criteria.  This is great and all, but having the ability to create filter iterators on the fly, ones that won&#8217;t be used application wide, without having to create a class is even better.</p>
<p>Let&#8217;s get to the code already!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> FilterCallbackIterator <span style="color: #000000; font-weight: bold;">extends</span> FilterIterator <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$callback</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>Iterator <span style="color: #000088;">$it</span><span style="color: #339933;">,</span> Closure <span style="color: #000088;">$callback</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callback</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$callback</span><span style="color: #339933;">;</span>
    parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$it</span><span style="color: #009900;">&#41;</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> accept<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;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callback</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</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: #339933;">,</span> <span style="color: #000088;">$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: #009900;">&#41;</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> callback<span style="color: #009900;">&#40;</span><span style="color: #000088;">$callback</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callback</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$callback</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see I extended the FilterIterator like you normally would.  Then my construct requires an Inner Iterator to be passed in (this can be a directory iterator, array iterator, etc.) and also the callback (anonymous function or closure). I store the callback for later use and run the parent construct to add the Inner Iterator.</p>
<p>As I mentioned earlier the accept method is the only method you have to change to make the Filter Iterator fit your criteria.  It is very simple, return true or false.  I use call_user_func because I&#8217;m storing the callback in an object property and that is the simplest way to run it.  I pass the parameters current, key and inner iterator to the callback.  I do this because this is how the class will work in PHP 5.4.</p>
<p>I&#8217;m sure you&#8217;re wondering why I have callback method also, and why does it return the object?  I wanted to be able to change the criteria of acceptance whenever I wanted, you may notice the callback isn&#8217;t required in the construct.</p>
<p>Let&#8217;s test this code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">23432</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">120</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$it</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$filterCB</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FilterCallbackIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$it</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$gt10</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filterCB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callback</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</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: #000088;">$val</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; 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: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Greater than 10<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gt10</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$result</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></pre></div></div>

<p>My example is very simple.  I want to output only values that are greater than 10.  I pass in my anonymous function to do this test for me.  And the output should look like this:</p>
<pre>Greater than 10
20
23432
120
12</pre>
<p>That was fun, but lets show only those greater than 100 later in our script:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$gt100</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filterCB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">callback</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</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: #000088;">$val</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; 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: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Greater than 100<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$gt100</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$result</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></pre></div></div>

<p>As you can see, all I had to do was update my callback and it is now testing my new criteria. The result with both the examples would be:</p>
<pre>Greater than 10
20
23432
120
12
Greater than 100
23432
120</pre>
<p>The Possibilities are endless with the CallbackFilterIterator. This blog post was a simple introduction to what you can do. * Note: I named my class differently from the actual class being used in PHP 5.4 to avoid any conflicts. *</p>
<p>To the view the docs for the upcomaing CallbackFilterIterator: <a href="http://www.php.net/manual/en/class.callbackfilteriterator.php">http://www.php.net/manual/en/class.callbackfilteriterator.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jakefolio.com/2011/12/callback-filter-iterator-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>HTML5.tx Review</title>
		<link>http://jakefolio.com/2011/10/html5-tx-review/</link>
		<comments>http://jakefolio.com/2011/10/html5-tx-review/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 18:35:02 +0000</pubDate>
		<dc:creator>jake</dc:creator>
				<category><![CDATA[Conferences]]></category>

		<guid isPermaLink="false">http://jakefolio.com/?p=53</guid>
		<description><![CDATA[Welcome to my newly skinned blog, it&#8217;s been a while.  Please excuse the mess of content as I try and get this blog/portfolio back up to working order. I was provided the great opportunity to speak at HTML5.tx this past &#8230; <a href="http://jakefolio.com/2011/10/html5-tx-review/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Welcome to my newly skinned blog, it&#8217;s been a while.  Please excuse the mess of content as I try and get this blog/portfolio back up to working order.</p>
<p>I was provided the great opportunity to speak at <a href="http://html5tx.com">HTML5.tx</a> this past weekend in Austin, TX.  All I can say is wow.  They were able to provide shirts, lanyards (really nice), breakfast/lunch/snacks and plenty of other swag at a steal of a price, $79.  If the price and goodies wasn&#8217;t enough to entice you, then you should look no further than the all star speaking cast.</p>
<p>The one thing that I felt stood out was the &#8220;open spaces&#8221;, a great place for informal conversation about whatever you want to go on.  Literally a group of people sat in a semi-circle around a poster board.  I was able to hang out for about 45 minutes and meet some really cool people.  I think more and more conferences are starting to implement an uncon/open space type area, and if you are an organizer and you don&#8217;t have this&#8230;.SET IT UP! I think this is the breeding ground for the next great speaker and/or the encouragement needed to get people more involved in the community.</p>
<p>The content/presentations were great content and delivery.  Each talk was jam packed with great gems of information for all levels of developer.  Every speaker seemed very energetic, organized and passionate about their topic.</p>
<p>As usual, the hallway conversation/connection was a great benefit.  If you don&#8217;t take the time to just conversate with someone you don&#8217;t know in the hallway/pre-party/post-party then you really missed out on a key part of the conference. I hope to see everyone I had a chance to talk with again soon at another conference/event, possibly Barcamp Dallas (more info to come).</p>
<p>A special thinks to Brandon Satrom and team for taking charge to provide the best atmosphere for learning and sharing.  Thank you to all the speakers for the time and preparation you did.  Thank you to the attendees, because you guys helped create the culture of the conference. Thank you to all the sponsors for the great swag/food/beer, great selection of beer!</p>
<p>Also, please <a href="http://speakerrate.com/events/1090-html5-tx">rate your speakers</a> and leave comments, because there is always room for improvement. Please follow/interact with me via twitter/irc, because I&#8217;d love to hear from you.</p>
]]></content:encoded>
			<wfw:commentRss>http://jakefolio.com/2011/10/html5-tx-review/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

