<?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>Davey Shafik</title>
	<atom:link href="http://daveyshafik.com/feed" rel="self" type="application/rss+xml" />
	<link>http://daveyshafik.com</link>
	<description>As close to my brain as you can safely get...</description>
	<lastBuildDate>Fri, 27 Apr 2012 11:25:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Mimetypes (and APIs)</title>
		<link>http://daveyshafik.com/archives/35507-mimetypes-and-apis.html</link>
		<comments>http://daveyshafik.com/archives/35507-mimetypes-and-apis.html#comments</comments>
		<pubDate>Wed, 25 Apr 2012 04:18:54 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=35507</guid>
		<description><![CDATA[As part of my day-job at Engine Yard, I spend a lot of time working with, and writing APIs. For all of the APIs I write, I use the awesome FRAPI API framework; and have been hacking away adding new features and fixing bugs more and more frequently over the last few months. One such feature, was the addition of mimetype support. Mimetype support allows you to specify mimetypes allowed in the Accept header, and to which format the response would be. The reason behind this was that at one point I was working with Githubs API which uses mimetypes [...]]]></description>
			<content:encoded><![CDATA[<p>As part of my day-job at <a href="http://www.engineyard.com/products/orchestra">Engine Yard</a>, I spend a lot of time working with, and writing APIs.</p>
<p>For all of the APIs I write, I use the awesome <a href="http://github.com/frapi/frapi">FR<em>API</em></a> API framework; and have been hacking away adding new features and fixing bugs more and more frequently over the last few months.</p>
<p>One such feature, was the addition of mimetype support. Mimetype support allows you to specify mimetypes allowed in the <code>Accept</code> header, and to which format the response would be.</p>
<p>The reason behind this was that at one point I was working with <a href="http://developer.github.com/">Githubs API</a> which uses mimetypes extensively for various reasons; and at the time, I thought they were very good reasons.</p>
<p>A full github mimetype looks like: <code>application/vnd.github[.version].&lt;param&gt;[+json]</code></p>
<p>So lets break this down:</p>
<dl>
<dt><code>application/</code></dt>
<dd>The media type (this indicates it is intended for an application to work with the data)</dd>
<dt><code>vnd.github</code></dt>
<dd>As specified in <a href="http://tools.ietf.org/html/rfc2048#section-2.1.2">RFC 2048</a>, vendor specific mimetypes should be prefixed with <code>vnd.</code> followed by the producers name</dd>
<dt><code>[.version]</code></dt>
<dd>Next up, we have [optionally] the API version, currently 3</dd>
<dt><code>.&lt;param&gt;</code></dt>
<dd>This is followed by a value that designates the response data type (<code>full</code>, <code>raw</code>, <code>text</code>, or <code>html</code>)</dd>
<dt><code>[+json]</code></dt>
<dd>We finish with the [optional] serialization, always <code>json</code>.</dd>
</dl>
<p>In theory, this looks great; just parse the mimetype and you have the version number, the response type, and potentially any number of serializations (think: XML, jsonp, serialize PHP even!). Simple, right?</p>
<p>Well&#8230; not quite. Lets look at an example API request (using the awesome <a href="http://httpie.org">httpie</a>):</p>
<p><script src="https://gist.github.com/2485502.js?file=1-http-get-user-repos.js"></script></p>
<p>The important line to note here, is <code>Content-Type: application/json; charset=utf-8</code>.</p>
<p>Now lets try again, this time, with the <code>Accept: application/vnd.github.3+json</code>. What this says is: I want you to serve me content of <strong>this</strong> type.</p>
<p><script src="https://gist.github.com/2485502.js?file=2-http-get-user-repos-vnd.txt"></script></p>
<p>Notice that we get the same <strong>type</strong> of response as before, <code>application/json</code>… but this isn&#8217;t what we requested. While it&#8217;s what we <strong>expect</strong> (because we read the docs and we know the entire API uses json); why isn&#8217;t the response <code>Content-Type: application/vnd.github.3+json</code>?</p>
<p>Most likely, it&#8217;s because most clients that understand json (for example those that unserialize it in to native data structures automatically) look for two types: <code>text/json</code> and <code>application/json</code>, others like <a href="https://github.com/jquery/jquery/blob/master/src/ajax.js#L340">jquery</a> just look for <codejson</code> anywhere in the <code>Content-Type</code> response header.</p>
<p>What&#8217;s <em>wrong</em> with this? Well, for starters, what if you request <code>Accept: text/plain</code> and you get back <code>Content-Type: image/jpeg</code>. That&#8217;s not going to work. Secondly, for caching proxies and such, it is isn&#8217;t possible to differentiate between the <code>application/vnd.github.3.full</code> response, and the <code>application/vnd.github.3.raw</code> or <code>application/vnd.github.4.full</code> response.</p>
<p>It would be entirely reasonable to respond with a <code>HTTP/1.1 406 Not Acceptable</code> header in the case where the server cannot respond with an accepted media type.</p>
<p>All in all, I think this use of mimetypes is less than great. Not terrible; definitely within the confines of the spec. So what <em>is</em> the solution?</p>
<p>The solution is part of the MIME RFCs (there are 5 original RFCs, 2045-2049, and dozens of updates) and explained in this context, in the HTTP 1.1 spec (<a href="http://tools.ietf.org/html/rfc2616">RFC 2616</a>), and that solution is <a href="http://tools.ietf.org/html/rfc2616#section-14.1">accept-params</a> or mimetype parameters. You&#8217;ve seen, and used them, regularly I&#8217;d bet, here&#8217;s an example:</p>
<p><code>Content-Type: text/html<strong>;charset=utf-8</strong></code></p>
<p>These exist <strong>explicitly</strong> to pass variable named values along with the mimetype. The one explicitly defined in the HTTP spec is the <code>q</code> parameter for designating preference for mimetypes in the <code>Accept</code> header.</p>
<p>So lets try the github mimetypes using parameters instead:</p>
<p><code>application/json<strong>;version=3;response=raw</strong></code></p>
<p>And in the response:</p>
<p><code>Content-Type: application/json<strong>;version=3;response=raw</strong></code></p>
<p>Not only do we get back the same mimetype, any decent HTTP client will still see the <code>application/json</code>, and caches will respect the parameters, just like they would the <code>charset</code> parameter. You could even drop parameters to indicate it was ignored; add parameters to give more details (e.g. <code>charset</code>) or change the values to indicate what you actually did.</p>
<p>Better yet, we can now do things like use q-values. So, should Github add XML output to their API, we could indicate we can handle both, but prefer json:</p>
<p><code>Accept: <strong>application/xml;q=0.8</strong>;version=3;response=raw, <strong>application/json;q=1.0</strong>;version=3;response=raw</code></p>
<p>Or that we prefer (the future) version 4, but can still handle version 3:</p>
<p><code>Accept: application/json;<strong>q=1.0;version=4</strong>;response=raw, application/json;<strong>q=0.5;version=3</strong>;response=raw</code></p>
<p>All-in-all, I believe this to be more semantically and technically correct, while also being easier (parameters are named (case-insensitive), optionally quoted, and can be in any order except <code>q</code> which must be first)</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/35507-mimetypes-and-apis.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Speaking at phpDay! (Verona, Italy)</title>
		<link>http://daveyshafik.com/archives/35500-speaking-at-phpday-verona-italy.html</link>
		<comments>http://daveyshafik.com/archives/35500-speaking-at-phpday-verona-italy.html#comments</comments>
		<pubDate>Mon, 02 Apr 2012 17:51:33 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=35500</guid>
		<description><![CDATA[Update: I will no longer be attending phpDay due to family circumstances. Sorry all! (I will still be at php&#124;tek, DPC and Lonestar PHP) I am adding another date to my summer conference circuit; this time the two-day phpDay conference in Verona, Italy on May 18th and 19th. This time I will be presenting on PHP Streams; this is will be an in-depth look in to the streams layer and is a talk I last gave back in 2008, so there is lots of new material to cover! I love PHP streams, definitely one of the most underrated part of [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update: I will no longer be attending phpDay due to family circumstances. Sorry all!</strong> (I will still be at php|tek, DPC and Lonestar PHP)</p>
<p>I am adding another date to my summer conference circuit; this time the two-day <a href="http://phpday.it">phpDay</a> conference in Verona, Italy on May 18th and 19th.</p>
<p>This time I will be presenting on PHP Streams; this is will be an in-depth look in to the streams layer and is a talk I last gave back in 2008, so there is lots of new material to cover!</p>
<p>I love PHP streams, definitely one of the most underrated part of PHP in my experience.</p>
<p>Again, if you are able to make it, do come and say Hello!</p>
<p>Also, I will also be at <a href="http://tek.phparch.com">php|tek</a> in Chicago between May 22nd-25th, however I am not speaking. (Actually, I will be there from the 20-21st to the 26th, if you want to hang out!)</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/35500-speaking-at-phpday-verona-italy.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Speaking at the Dutch PHP Conference and Lone Star PHP!</title>
		<link>http://daveyshafik.com/archives/35468-speaking-at-dpc-and-lone-star-php.html</link>
		<comments>http://daveyshafik.com/archives/35468-speaking-at-dpc-and-lone-star-php.html#comments</comments>
		<pubDate>Mon, 27 Feb 2012 14:42:41 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=35468</guid>
		<description><![CDATA[I will be speaking at the Dutch PHP Conference (in Amsterdam, June 7th &#8211; 9th) and Lone Star PHP Conference (in Dallas, Texas, June 29th &#8211; 30th). I will be presenting the same talks at both conferences, &#8220;PHP 5.4: The New Bits&#8221;, and &#8220;Fast, Not Furious: Performance Tuning That Works&#8221;; so if you can&#8217;t make it across the atlantic one way or the other, you can still catch me. If you are able to make it, find me and say hello. If you&#8217;re not able to make it, sacrifice a goat or some such and change things so you can [...]]]></description>
			<content:encoded><![CDATA[<p>I will be speaking at the <a href="http://www.phpconference.nl/">Dutch PHP Conference</a> (in Amsterdam, June 7th &#8211; 9th) and <a href="http://lonestarphp.com/">Lone Star PHP Conference</a> (in Dallas, Texas, June 29th &#8211; 30th).</p>
<p>I will be presenting the same talks at both conferences, &#8220;PHP 5.4: The New Bits&#8221;, and &#8220;Fast, Not Furious: Performance Tuning That Works&#8221;; so if you can&#8217;t make it across the atlantic one way or the other, you can still catch me.</p>
<p>If you are able to make it, find me and say hello. If you&#8217;re not able to make it, sacrifice a goat or some such and change things so you can — both of these conferences are &#8220;must-goes&#8221; if you want to learn, have fun, and engage with the awesome PHP community.</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/35468-speaking-at-dpc-and-lone-star-php.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5.4: The New Bits Slides (PHP UK Conference)</title>
		<link>http://daveyshafik.com/archives/35463-php-5-4-the-new-bits-slides-php-uk-conference.html</link>
		<comments>http://daveyshafik.com/archives/35463-php-5-4-the-new-bits-slides-php-uk-conference.html#comments</comments>
		<pubDate>Sat, 25 Feb 2012 15:31:37 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=35463</guid>
		<description><![CDATA[My slides for my talk at the PHP UK Conference 2012 are now available here. ALso, if you attended the talk, any feedback on Joind.in would be great, for friday and saturday.]]></description>
			<content:encoded><![CDATA[<p>My slides for my talk at the PHP UK Conference 2012 are <a href="http://speakerdeck.com/u/dshafik/p/php-54-the-new-bits" title="PHP 5.4: The New Bits Slides">now available here</a>.</p>
<p>ALso, if you attended the talk, any feedback on <a href="http://Joind.in">Joind.in</a> would be great, for <a href="http://joind.in/talk/view/4946">friday</a> and <a href="http://joind.in/talk/view/4973">saturday</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/35463-php-5-4-the-new-bits-slides-php-uk-conference.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Blowfish Debacle</title>
		<link>http://daveyshafik.com/archives/35354-the-blowfish-debacle.html</link>
		<comments>http://daveyshafik.com/archives/35354-the-blowfish-debacle.html#comments</comments>
		<pubDate>Sat, 11 Feb 2012 23:24:45 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=35354</guid>
		<description><![CDATA[With the release of PHP 5.3.7 a small change was made to upgrade crypt_blowfish (for crypt()) to version 1.2. This was a great security fix, solving an issue with insecure passwords due to incorrect behavior. However, what wasn&#8217;t made clear, is that this change was actually a backwards compatibility break. If you upgraded to 5.3.7+ data hashed pre-5.3.7 would no longer match data hashed post-5.3.7; this means if you use it for passwords, it will no longer match. So what&#8217;s the deal here? To encrypt using blowfish in pre-5.3.7, you would prefix your salt with $2a$ followed by a 2 [...]]]></description>
			<content:encoded><![CDATA[<p>With the release of PHP 5.3.7 a small change was made to upgrade crypt_blowfish (for <code>crypt()</code>) to version 1.2. This was a great security fix, solving an issue with insecure passwords due to incorrect behavior.</p>
<p><strong>However</strong>, what wasn&#8217;t made clear, is that this change was actually a backwards compatibility break. If you upgraded to 5.3.7+ data hashed pre-5.3.7 would no longer match data hashed post-5.3.7; this means if you use it for passwords, it will no longer match.</p>
<p>So what&#8217;s the deal here?</p>
<p>To encrypt using blowfish in pre-5.3.7, you would prefix your salt with <code>$2a$</code> followed by a 2 digit cost parameter (base-2 logarithm, between 04-31), another <code>$</code> sign, and finally 22 alphanumeric characters (0-9, A-Z, a-z).</p>
<p>So you end up with something like this: <code>$2a$10$22randomcharactershere$</code></p>
<p>With PHP 5.3.7, the behavior of <code>crypt()</code> when using the <code>$2a$</code> has changed to promote the new, more secure behavior — this is where the backwards compatibility break comes in. Now <code>$2a$</code> will use the correct behavior, and has explicit countermeasures against the insecurities of the old behavior.</p>
<p>So what do you do when you&#8217;ve got these already hashed passwords? Reset all your user passwords? That suck!</p>
<p>Luckily, two new prefixes were introduced, <code>$2x$</code>, which exactly replicates the behavior of <code>$2a$</code> in pre-5.3.7 and <code>$2y$</code> which is secure like the new <code>$2a$</code> but with no countermeasures.</p>
<p>What this means is, you can simply do the following to upgrade your passwords on-the-fly: </p>
<p><script src="https://gist.github.com/1804939.js"></script></p>
<p>Now, this code assumes that you spotted the issue <strong>before</strong> updating the server; if you have had new passwords created since the upgrade, you will want to check against the <code>$2a$</code> hash if the <code>$2x$</code> check fails, and then update to <code>$2y$</code>.</p>
<p>The reason I chose to use <code>$2y$</code> is that you can identify non-upgraded passwords by the old <code>$2a$</code> prefix. I would recommend that after say, 3 months, you might want to examine your dataset for those still using <code>$2a$</code> and then think about forcing password changes.</p>
<p>While re-using the same password might not be as secure as we would like, this ensures minimal user impact, and an easy upgrade path.</p>
<p>For better security, rather than automatically upgrading the hashes, you could check for <code>$2a$</code> and require a new password be supplied before continuing.</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/35354-the-blowfish-debacle.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Closure Puzzle</title>
		<link>http://daveyshafik.com/archives/32789-the-closure-puzzle.html</link>
		<comments>http://daveyshafik.com/archives/32789-the-closure-puzzle.html#comments</comments>
		<pubDate>Fri, 13 Jan 2012 18:10:52 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=32789</guid>
		<description><![CDATA[While writing my slides for the PHP UK Conference I looked much more closely at the changes to closures in PHP 5.4. The biggest change that people have been asking for, is access to $this when the closure is defined inside of an object method. This has been added: Simple enough. However, it didn&#8217;t stop there; there was also the addition of Closure::bind() and Closure->bindTo(). These methods are identical except one is a static method into which the closure is passed, the second an instance method on the closure itself. These methods both take two arguments (on top of the [...]]]></description>
			<content:encoded><![CDATA[<p>While writing my slides for <a href="http://phpconference.co.uk">the PHP UK Conference</a> I looked much more closely at the changes to closures in PHP 5.4.</p>
<p>The biggest change that people have been asking for, is access to <code>$this</code> when the closure is defined inside of an object method. This has been added:</p>
<p><script src="https://gist.github.com/1607647.js?file=closure-this.php"></script></p>
<p>Simple enough. However, it didn&#8217;t stop there; there was also the addition of <code>Closure::bind()</code> and <code>Closure->bindTo()</code>. These methods are identical except one is a static method into which the closure is passed, the second an instance method on the closure itself.</p>
<p>These methods both take two arguments (on top of the closure for the static version): <code>$newthis</code> and <code>$newscope</code>.</p>
<p>What this means is that unlike the regular object model the concept of <code>$this</code> and lexical scope (what is in scope for the function with regards to private/protected methods inside objects) are completely separated.</p>
<p>You can change <code>$this</code> to be a different object, while leaving the scope as the current object which if you then pass the current object in gives the different object access to the private/protected methods of the current method. That sounds complicated right? It is. A more likely scenario is to keep <code>$this</code> the same while changing the scope to another object (or static) such that you can pretend you&#8217;re accessing it from that other object and not hit private/protected methods.</p>
<p>Either of these scenarios is great for unit testing — I&#8217;m sure we&#8217;ll see this creep into <a href="https://github.com/sebastianbergmann/phpunit/">PHPUnit</a> sooner rather than later (I&#8217;m pretty sure Sebastian was promoting access to private/protected methods in reflection at some point — this makes that moot).</p>
<p>However, this can be seriously confusing, consider the following code that intentionally busts the OO model we know and love:</p>
<p><script src="https://gist.github.com/1607647.js?file=closure-puzzle.php"> </script></p>
<p>We have two classes, <code>Bar</code> with two methods, <code>public function A() { }</code> and <code>private B() { }</code>, we then have <code>Bat</code> which uses the <code>__call()</code> magic method to act as a proxy for <code>Bar</code>, as well as <code>public function C()</code> and <code>private function D()</code>.</p>
<p>Inside <code>__call()</code> we instantiate an instance of <code>Bar</code> as <code>$bar</code> and then if <code>__call()</code> is able to call the requested function on <code>$bar</code> it simply does so, otherwise it creates a closure, passing in both the requested function name and <code>$bar</code>. Then using <code>Closure::bind()</code> we leave <code>$this</code> as the current object, but change the lexical scope to be that of <code>$bar</code>. The result of this is a new closure that is assigned to <code>$bustOO</code></p>
<p>What this means is that inside the context of the new closure, any call on <code>$this</code> is referencing the object in which it was originally instantiated, but any calls on the passed in <code>$bar</code> will be the same as calling <code>$this</code> from inside <code>$bar</code> itself.</p>
<p>This means that the call to the private <code>$bar->B()</code> will now work. Also, the call to <code>$this->C()</code> works (calling <code>Bat->C()</code>), but things get tricky when we call <code>$this->D()</code>.</p>
<p>When this happens, because the lexical scope is <code>$bar</code>, it cannot access <code>Bat->D()</code>, which means it calls <code>Bat->__call()</code>. Inside <code>__call()</code> the <code>is_callable(array($bar, $function))</code> check fails (the method doesn&#8217;t exist on  then tries to call <code>$bar->D()</code> through the callback, resulting in <code>Fatal error: Call to undefined method Bar::D()</code>.</p>
<p>The full output looks like this:</p>
<p><script src="https://gist.github.com/1607647.js?file=results.txt"></script></p>
<p>Talk about head spinning!</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/32789-the-closure-puzzle.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>I&#8217;m speaking at the PHP UK Conference</title>
		<link>http://daveyshafik.com/archives/32363-im-speaking-at-the-php-uk-conference.html</link>
		<comments>http://daveyshafik.com/archives/32363-im-speaking-at-the-php-uk-conference.html#comments</comments>
		<pubDate>Sat, 07 Jan 2012 03:30:24 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=32363</guid>
		<description><![CDATA[If you&#8217;re going to be in London at the end of February, you should be attending the PHP UK Conference on February 24th and 25th. This is the first year they are doing a multi-day conference and I&#8217;m excited to be giving my talk on PHP 5.4: The New Bits. You can read more details on my talk and all the other great talks on their website. I&#8217;m looking forward to my first major experience with my home countries community, please come and say Hello!]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re going to be in London at the end of February, you should be attending the <a href="http://phpconference.co.uk/">PHP UK Conference</a> on February 24th and 25th.</p>
<p>This is the first year they are doing a multi-day conference and I&#8217;m excited to be giving my talk on <strong>PHP 5.4: The New Bits</strong>.</p>
<p>You can read more details on <a href="http://phpconference.co.uk/talk/php-54-new-bits">my talk</a> and <a href="http://phpconference.co.uk/talks/2012">all the other great talks</a> on their website.</p>
<p>I&#8217;m looking forward to my first major experience with my home countries community, please come and say Hello!</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/32363-im-speaking-at-the-php-uk-conference.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Orlando PHP User Group — Get Soaked: An In-depth look at PHP Streams</title>
		<link>http://daveyshafik.com/archives/30610-orlando-php-user-group-%e2%80%94%c2%a0get-soaked-an-in-depth-look-at-php-streams.html</link>
		<comments>http://daveyshafik.com/archives/30610-orlando-php-user-group-%e2%80%94%c2%a0get-soaked-an-in-depth-look-at-php-streams.html#comments</comments>
		<pubDate>Sat, 12 Nov 2011 21:12:44 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=30610</guid>
		<description><![CDATA[This past Thursday, I attended the Orlando PHP User Group and, because David Rogers wasn&#8217;t feeling too good, I stepped in to give a talk on PHP Streams. I met a bunch of really great people, and had a blast! If you&#8217;re in the area, you should check it out. As promised, here are my slides.]]></description>
			<content:encoded><![CDATA[<p>This past Thursday, I attended the Orlando PHP User Group and, because David Rogers wasn&#8217;t feeling too good, I stepped in to give a talk on PHP Streams.</p>
<p>I met a bunch of really great people, and had a blast! If you&#8217;re in the area, you should check it out.</p>
<p>As promised, <a href='http://daveyshafik.com/wp-content/uploads/2011/11/Get-Soaked-An-in-depth-look-at-streams.pdf' title="Get Soaked: An in-depth look at streams">here are my slides</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/30610-orlando-php-user-group-%e2%80%94%c2%a0get-soaked-an-in-depth-look-at-php-streams.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Faster Arrays</title>
		<link>http://daveyshafik.com/archives/30320-faster-arrays.html</link>
		<comments>http://daveyshafik.com/archives/30320-faster-arrays.html#comments</comments>
		<pubDate>Mon, 07 Nov 2011 12:50:33 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=30320</guid>
		<description><![CDATA[Arrays, long considered the work horse of PHP have one flaw: they can be incredibly slow. There is however an alternative — at least, for a small subset of use cases. SplFixedArray. You use SplFixedArray like so: The SplFixedArray class provides a super-fast, fixed size array implementation. There are some limitations however, first you must use numeric keys and secondly you cannot use anonymous assignment (i.e. $array[] = 'value';). You&#8217;ll notice one requirement was missing, that it should have a fixed size. While having a fixed size is what will bring you the speed increase it&#8217;s actually not a requirement that [...]]]></description>
			<content:encoded><![CDATA[<p>Arrays, long considered the work horse of PHP have one flaw: they can be incredibly slow. There is however an alternative — at least, for a small subset of use cases. <code>SplFixedArray</code>.</p>
<p>You use <code>SplFixedArray</code> like so:</p>
<p><script src="https://gist.github.com/1805227.js?file=1-spl-fixed-array.php"></script></p>
<p>The <code>SplFixedArray</code> class provides a super-fast, fixed size array implementation. There are some limitations however, first you <em>must use numeric keys</em> and secondly you <em>cannot use anonymous assignment</em> (i.e. <code>$array[] = 'value';</code>).</p>
<p>You&#8217;ll notice one requirement was missing, that it should have a fixed size. While having a fixed size is what will bring you the speed increase it&#8217;s actually not a requirement that the size <em>be</em> fixed. Though you must specify a size to the constructor, you can change it (and lose most — if not all — speed benefits) at any time using <code>SplFixedArray->setSize()</code>.</p>
<p>So, what sort of speed increase are we talking about? In my testing of arrays 100, 100&#8230; 1,000,000 elements, you will see a speed increase about 20-25%; for arrays smaller than 100, it will actually be slower by 25-40%.</p>
<p>The benchmarking was very simple, a comparison of a read and write iteration for both normal and fixed arrays of different sizes like so:</p>
<p><script src="https://gist.github.com/1805227.js?file=2-benchmark.php"></script></p>
<p>Additionally, the memory usage to run the benchmarks for array vs SplFixedArray is significantly different, regular arrays clock in at <strong>198MB</strong> while SplFixedArray uses a mere <strong>83MB</strong>, that&#8217;s a <em>59% memory saving</em>.</p>
<p>In practical terms, you&#8217;re only going to be worried about the speed of arrays when you&#8217;re dealing with larger arrays anyway, so the speed loss for the lower digits isn&#8217;t a big concern&#8230; but where exactly could this be useful?</p>
<p>There is one common scenario where you will commonly be dealing with large numerically indexed arrays of data: Your database result sets. Using PDO, you can tell how many results you have before you retrieve the row data using <code>PDOStatement->rowCount()</code>. </p>
<p><script src="https://gist.github.com/1805227.js?file=3-pdo-example.php"></script></p>
<p>Unfortunately, it is not possible to set the result set container for <code>PDOStatement->fetchAll()</code> to use <code>SplFixedArray</code> — however, if someone wants to help (that is, someone who knows internals and&#8230; well, C), I&#8217;ve got an opening for a coach!</p>
<p>At the urging of my co-worker <a href="http://helgi.ws">Helgi</a>, I threw the arrays into a <code>FilterIterator</code> and got some pretty interesting results. Using similar code to the first benchmark, but instead of just reading out the array, we created and used a custom <code>FilterIterator</code>:</p>
<p><script src="https://gist.github.com/1805227.js?file=4-even-filter-example.php"></script></p>
<p>For regular arrays, we must first create an iterator:</p>
<p><script src="https://gist.github.com/1805227.js?file=5-even-filter-example-cont.php"></script></p>
<p>For the SplFixedArray, we passed it straight into the <code>EvenFilterIterator</code>, otherwise the code is the same.</p>
<p>Even with the extra overhead of creating the <code>ArrayIterator</code>, the SplFixedArray is only marginally (1%) faster till it reaches the 10000 elements mark, and then it starts to become marginally slower (again 1%). So, I guess the take-away is: <strong>use with caution</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/30320-faster-arrays.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>New Start</title>
		<link>http://daveyshafik.com/archives/28649-new-start.html</link>
		<comments>http://daveyshafik.com/archives/28649-new-start.html#comments</comments>
		<pubDate>Thu, 29 Sep 2011 15:06:31 +0000</pubDate>
		<dc:creator>Davey Shafik</dc:creator>
				<category><![CDATA[tagged]]></category>

		<guid isPermaLink="false">http://daveyshafik.com/?p=28649</guid>
		<description><![CDATA[So, like others, I am starting at EngineYard as a member of the Orchestra.io team effective October 4th. I&#8217;m really excited about working with many talented people, on a really cool project. However, the best part is that I get to join a company who not only &#8220;gets&#8221; the community, but embraces and participates in it. The community and their presence in it is core to the working philosophy of the company&#8230; it was obvious in every interaction I&#8217;ve had to date and I&#8217;m really excited to be contributing to it in the future. On that note, look for me [...]]]></description>
			<content:encoded><![CDATA[<p>So, like <a href="http://naramore.net/blog/s-sourceforge-engineyard">others</a>, I am starting at EngineYard as a member of the Orchestra.io team effective October 4th.</p>
<p>I&#8217;m really excited about working with <a href="http://onemoredigit.com/">many</a> <a href="http://helgi.ws/">talented</a> <a href="http://eamo.net/">people</a>, on a <a href="http://orchestra.io">really cool project</a>.</p>
<p>However, the best part is that I get to join a company who not only &#8220;gets&#8221; the community, but embraces and participates in it. The community and their presence in it is core to the working philosophy of the company&#8230; it was obvious in every interaction I&#8217;ve had to date and I&#8217;m really excited to be contributing to it in the future.</p>
<p>On that note, look for me around ZendCon in a few weeks and come and say hello!</p>
]]></content:encoded>
			<wfw:commentRss>http://daveyshafik.com/archives/28649-new-start.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

