<?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>Dereleased &#187; Web Development</title>
	<atom:link href="http://www.dereleased.com/category/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dereleased.com</link>
	<description>Information Saturation</description>
	<lastBuildDate>Sat, 10 Sep 2011 20:41:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The importance of ZVals and Circular References</title>
		<link>http://www.dereleased.com/2011/04/27/the-importance-of-zvals-and-circular-references/</link>
		<comments>http://www.dereleased.com/2011/04/27/the-importance-of-zvals-and-circular-references/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 03:40:59 +0000</pubDate>
		<dc:creator>Clark</dc:creator>
				<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[PHP Quirks]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dereleased.com/?p=170</guid>
		<description><![CDATA[Just a quick post for now. Do you know how PHP&#8217;s symbol table works? To put it in nutshell, symbols are stored in one place and values (also called ZVals) are stored in another. Normally, this abstraction will mean nothing to you, but take the following sample code: $foo = &#38;$bar; $bar = &#38;$foo; Pretty [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick post for now.  Do you know how PHP&#8217;s symbol table works?  To put it in nutshell, symbols are stored in one place and values (also called ZVals) are stored in another.  Normally, this abstraction will mean nothing to you, but take the following sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$bar</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">;</span></pre></div></div>

<p>Pretty basic circular reference, and one that might be pretty difficult to assign in a few other languages.  Now what?  Well, let&#8217;s take a look at another reference construct for a moment.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'foo'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$z</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$y</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$z</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
string(3) &quot;foo&quot;
string(3) &quot;foo&quot;
string(3) &quot;foo&quot;
*/</span></pre></div></div>

<p>Pretty much what we expected.  Now, let&#8217;s throw a wrench into the mix and reassign <span style="font-family: 'Courier New', courier, mono;">$y</span> by reference to <span style="font-family: 'Courier New', courier, mono;">&#038;$b</span>, and then examine the results:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$z</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
string(3) &quot;foo&quot;
string(3) &quot;bar&quot;
string(3) &quot;foo&quot;
*/</span></pre></div></div>

<p>Only the value of <span style="font-family: 'Courier New', courier, mono;">$y</span> changed!  That is because PHP, when assigning a reference to a reference, always points at the same ZVal, instead of creating a reference chain; this is one significant way in which PHP References are <strong>NOT</strong> pointers &#8211; they&#8217;re never more than one layer deep.  Let&#8217;s go back to our original example and assign a value to one of those variables:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
int(3);
int(3);
*/</span></pre></div></div>

<p>Works like a charm!  This is because both references pointed at the same location in the ZVal table.  But what if we start over again, and reassign $foo by reference to something else?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$bar</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$baz</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'baz'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$baz</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
string(3) &quot;baz&quot;
NULL
*/</span></pre></div></div>

<p>If you&#8217;ve been following along, this should make perfect sense.  <span style="font-family: 'Courier New', courier, mono;">$foo</span> is created, and pointed at a ZVal location identified by <span style="font-family: 'Courier New', courier, mono;">$bar</span>; when <span style="font-family: 'Courier New', courier, mono;">$bar</span> is created, it points at the same place <span style="font-family: 'Courier New', courier, mono;">$foo</span> was pointed.  That location, of course, is null.  When <span style="font-family: 'Courier New', courier, mono;">$foo</span> is reassigned, the only thing that changes is to which ZVal <span style="font-family: 'Courier New', courier, mono;">$foo</span> points; if we had assigned a different value to <span style="font-family: 'Courier New', courier, mono;">$foo</span> first, then <span style="font-family: 'Courier New', courier, mono;">$bar</span> would still retain that value.</p>
<p>While we&#8217;re on the topic of ZVals, I&#8217;ll mention just one more thing.  PHP uses a lazy-copying (or, copy-on-write) mechanism, thanks to the ZVal table.  Consider the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mem1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">memory_get_usage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar3</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar4</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar5</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar6</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$foo</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mem2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">memory_get_usage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar1</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;...&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mem3</span> <span style="color: #339933;">=</span> <span style="color: #990000;">memory_get_usage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I leave the calls to <span style="font-family: 'Courier New', courier, mono;">memory_get_usage()</span> in place so that their effects will be more obvious.  If we dump those three values, we get 426040, 426408 and 526536, respectively.  In the second phase, as you can see, we only increased memory usage by 386 bytes (and that includes the memory required to store the memory that was used).  During the third phase, when a variable was altered, memory usage increased by 100128 bytes.  PHP uses about 24 bytes of memory to make an entry into the symbol table, and 80 more to create a null entry in the ZVal table.</p>
<p>So, the next time you think about passing a parameter you don&#8217;t intend to modify to a function by reference in order to save memory, or returning one for the same reason, don&#8217;t worry about it so much; it&#8217;s only 24 bytes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereleased.com/2011/04/27/the-importance-of-zvals-and-circular-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Quirks &#8211; String manipulation by offset</title>
		<link>http://www.dereleased.com/2011/04/27/php-quirks-string-manipulation-by-offset/</link>
		<comments>http://www.dereleased.com/2011/04/27/php-quirks-string-manipulation-by-offset/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 02:55:29 +0000</pubDate>
		<dc:creator>Clark</dc:creator>
				<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[PHP Quirks]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dereleased.com/?p=165</guid>
		<description><![CDATA[Just a quick update for a mild PHP Quirk/annoyance I have noticed recently while doing some manipulation of strings by character offset. Say you have a string, such as &#8216;abcde&#8217;; Now, suppose you want to check the value of the third character (at index 2). You might have done something like this: $str = 'abcde'; [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick update for a mild PHP Quirk/annoyance I have noticed recently while doing some manipulation of strings by character offset.</p>
<p>Say you have a string, such as &#8216;abcde&#8217;;  Now, suppose you want to check the value of the third character (at index 2).  You might have done something like this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcde'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">}</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'c'</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  <span style="color: #666666; font-style: italic;">// do something...</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<p>And, of course, that&#8217;s all fine, well and dandy, it does what you expect and you can move on with your life.  In fact, if you&#8217;re in to micro-optimizations, that construct provides a great way to check a string for minimum length, and is, on average, 44% faster than using <span style="font-family: 'Courier New', courier, mono;">strlen()</span>.  However, you can use this same construct to change the value of the character at whatever string you&#8217;re working with.  It works roughly as expected, but with a few gotchas:
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Gotcha #0 - Adding multiple characters to a single offset; shouldn't really be a gotcha
 */</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abc123'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// aac123</span>
<span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'123'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// aac113</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Gotcha #1 - Adding characters past the end of the string
 */</span>
<span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// aac113 c</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">ord</span><span style="color: #009900;">(</span><span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prints '32', the space character</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Gotcha #2 - Adding characters to an empty string
 */</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// array( 0 =&gt; 'a' )</span></pre>
</div>
</div>
<p>In the first case, we see that, rather than leave the &#8220;uninitialized&#8221; area between where we&#8217;ve defined characters as a null character, it has been silently converted to a space.  Arguably, this is so that an <span style="font-family: 'Courier New', courier, mono;">isset($str[6]);</span> check would not return false, but this is important to know if you expected the values of those spaces to remain at zero.</p>
<p>In the second case, we see PHP&#8217;s weak typing in place; since an empty string has no offsets to begin with, attempts to add characters results in silent conversion to an array.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereleased.com/2011/04/27/php-quirks-string-manipulation-by-offset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Let&#8217;s talk about your password model</title>
		<link>http://www.dereleased.com/2010/02/09/lets-talk-about-your-password-model/</link>
		<comments>http://www.dereleased.com/2010/02/09/lets-talk-about-your-password-model/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 17:02:50 +0000</pubDate>
		<dc:creator>Clark</dc:creator>
				<category><![CDATA[Information Security]]></category>
		<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[The Internet]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[blowfish]]></category>
		<category><![CDATA[crypt]]></category>
		<category><![CDATA[md5]]></category>
		<category><![CDATA[nonce]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[salt]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sha1]]></category>
		<category><![CDATA[sha256]]></category>

		<guid isPermaLink="false">http://www.dereleased.com/?p=151</guid>
		<description><![CDATA[First off, let me just say that I am by no means an expert cryptographer; there are all sorts of wonderful, terrible things about hashes and block ciphers that I just don&#8217;t understand (I&#8217;d like to believe that it&#8217;s because I&#8217;ve not been exposed to them, whoever&#8217;s fault that is, and that if given a [...]]]></description>
			<content:encoded><![CDATA[<p>First off, let me just say that I am by no means an expert cryptographer; there are all sorts of wonderful, terrible things about hashes and block ciphers that I just don&#8217;t understand (I&#8217;d like to believe that it&#8217;s because I&#8217;ve not been exposed to them, whoever&#8217;s fault that is, and that if given a chance I would get it), but that&#8217;s also why I&#8217;m writing this &#8211; to give the opinion of someone who recognizes his own weakness, and how that translates to another&#8217;s strength.  Furthermore, this explanation gives a very simplistic view of web security that only examines one aspect of a secure system.  For loads more information about securing your web application, take a look at <a href="http://cookies.lcs.mit.edu/pubs/webauth:tr.pdf">&#8220;Dos and Don&#8217;ts of Client Authentication on the Web&#8221; <small>[PDF]</small></a> written by some very smart folks at M.I.T.</p>
<p>So, let&#8217;s start with a beginner&#8217;s introduction.  In the beginning, there were users, and users wanted to be able to log in because otherwise being a user was rather pointless indeed.  Thus, the password is born, and forevermore it becomes the goal of clever crackers and security experts alike.  The first problem someone encounters with passwords is how to store them, and that depends very much on a few key factors: Audience, Exposure, and Uniqueness.  If you are running a &#8220;homegrown&#8221; application (shout out to MecTracker) for use only inside the company, containing (in general) zero sensitive data, and you intend to pick user&#8217;s passwords for them (preventing the loss of a life password, itself a bad-yet-unavoidable practice), then why not just store them in plain text?  Certainly makes it easy to retrieve a password for someone without having to reset it (useful for someone away from their work machine with saved password who needs to log in).</p>
<p>Conversely, if you&#8217;re a bank, and you&#8217;re storing any of this in plain text, you will be razed to the ground by angry tech-savvy customers and auditors alike, hopefully BEFORE you get grandma and grandpa Jones to type in the password they use for everything else, too.  Hopefully, if you&#8217;re a bank, you&#8217;re using some crazy method I&#8217;m not about to describe here.</p>
<p>Then, there&#8217;s the middle ground.  I, for example, am not a bank (who would&#8217;ve guessed?  Can someone please notify my ex-girlfriend?), so my needs are much more middle-of-the-road, which is why I&#8217;ve settled for hashing.  When I started using PHP, I generally stuck to simple MD5 hashes; it was 10 years ago, and breaking MD5 seemed reasonably difficult.  Then I was told not to use MD5 because, at 128 bits, it was too weak, and I should be using SHA-1, which was 160 bits.  Then came the recommendation for SHA-256 (guess how many bits that one is!), and then whirlpool, and so on.  If you&#8217;re using a proper password strategy then you&#8217;ve been salting all along (I&#8217;ll admit I wasn&#8217;t in the old days, but you&#8217;ve got to be a beginner sometime), but if you haven&#8217;t, allow me to give you a word on salt.</p>
<p>&#8220;Salting&#8221; a password hash is the practice of taking a piece of input data, adding in an extra piece of information (called &#8220;salt&#8221;; see where this is going?), and hashing that, instead of just hashing the raw input.  In fact, with sites <a href="http://md5.rednoize.com/">that act like a search engine for MD5 and SHA-1 hashes</a>, not salting your input is, for general purpose storage, only one-degree of separation away from just storing the data in plain text.  Furthermore, good salt will be ever-changing (in this practice, the salt is also known as a &#8216;nonce&#8217;), and can safely be stored without obfuscation, as having included it means that a table not accounting for the nonce is useless, and a table that accounts for the nonce is only good against one of the passwords in your database.  Now you&#8217;ve just made an attack much more expensive, but that may not be as useful in reality as we&#8217;d like to believe.</p>
<p>MD5 and SHA-1 hashes can be calculated very, very quickly.  In fact, it&#8217;s generally more expensive to include some data about the current time (for use in salting/as a nonce) than it is to calculate the actual hash.  Here is some experimental code to prove my point:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'ITERATIONS'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$th</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">(</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;</span> ITERATIONS<span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$j</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
	<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">(</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">(</span><span style="color: #000088;">$i</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
	<span style="color: #000088;">$tt</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">(</span><span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$th</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">(</span><span style="color: #000088;">$tt</span> <span style="color: #339933;">/</span> ITERATIONS<span style="color: #339933;">,</span> <span style="color: #000088;">$th</span> <span style="color: #339933;">/</span> ITERATIONS<span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>Simply hashing the value of the counter averaged 320,000 hashes per second on my work machine, which is not very powerful, and is certainly not running this in a very optimized way.  By changing what is being hashed to the current time to the microsecond, the number of hashes per second is reduced to an average of about 150,000 &#8211; in short, the hash is NOT the expensive part of what&#8217;s going on here.  So, let&#8217;s say that, given a more optimized environment but a more expensive dictionary list to be hashed, that the average is 200,000 hashes per second, and the dictionary is about 50,000,000 common passwords.  Simple math tells you that generating a hash list for this will take about 250 seconds, or less than 5 minutes.  If it takes under 5 minutes to generate a table, and only a few seconds from there to query it, then even a database of 150,000 users can be fully cracked in just under a fortnight.</p>
<p>So how can this be combated?  Well, strong password guidelines are a good start, but if you&#8217;re relying on users to implement password security for you, you&#8217;re probably doing it very, very wrong.  I&#8217;d like to challenge one of the assumptions you&#8217;ve probably made that I&#8217;ve had to challenge recently, and that is the value of speed; speed is <em>bad</em>.  Think about it: using a hash method that can generate a table of fifty million values in under 5 minutes sounds great from a performance perspective, but who are you really helping?  Is your user going to notice that your hash method took under 1ms to calculate, or is this performance more likely to benefit someone trying to crack your passwords?  Who would be more hurt if your passwords took closer to 12ms to generate and verify, your users or your would-be attacker?</p>
<p>If you haven&#8217;t heard of it yet, may I introduce you to <a href="http://www.bletchleypark.net/cryptology/blowfish.html">Blowfish Encryption</a>.  Blowfish is designed to scale with Moore&#8217;s Law by allowing you, the programmer, to decide how long it takes to generate a hash.  This is done by allowing you to specify a number which will be interpreted as a log-base-2 of how many iterations the hashing sequence should take; this metadata is then stored as part of the salt, prepended to the hash, and can be verified by the same function that created it since hashes are of fixed length and will be truncated or padded accordingly.  By using a log-base-2 scale, every increment of that number (n) literally doubles the time required to calculate the hash, as it will have to undertake 2<sup>n</sup> iterations to generate the password.  From what I can gather, a number like 7 or 8 is a fair industry standard at this time, and on my work machine limits the hashes-per-second to around 86.6 and 43.3, respectively.</p>
<p>Now, performance is a factor in real world applications, so let&#8217;s pick a number like 2<sup>7</sup>, which as I said allows about 87 hashes per second.  At that rate, a single dictionary table (useful for only one user, since we are salting these passwords) takes about six and a half days to generate.  For that same database of 150,000 users, it would take over 2,733 <b>years</b> to crack.  Of course, computational power will get less expensive as time goes on, and the same number of operations can and will get faster, but with the blowfish algorithm you need only increment the log to double the computational cost, keeping the cracking of your database safely outside the realm of technical feasibility.</p>
<p>So how does one use the blowfish algorithm in PHP?  The <a href="http://php.net/manual/en/function.crypt.php" style="font-family: 'Courier New',courier,monospace;">crypt()</a> function is your friend!  However, the manual is not entirely clear on the implementation details of blowfish, as it does not include one key part (which caused me to tear my hear out a little bit, since, as a Windows user, I was unable to check the man pages for crypt(3)) in any great detail, and that is the log base.  When you generate the salt, you will need to prepend it with an instruction string that tells it what kind of hash to generate, and what parameters to use.  Furthermore, the salt is not sixteen characters, but sixteen BYTES, and the characters in your hash will be read as a BASE64 encoded string, which means that using characters not allowed in a base64 string will cause the function to revert back to whatever the default is on your system, probably STD_DES or MD5.</p>
<p>All of that information might have seemed a bit hazy, so I&#8217;ll include the timing example I used before modified to suit crypt/blowfish.  Note also that I am storing the microtime result on every iteration of the for-loop, as in order to give you worst-case scenarios on the cracker&#8217;s timetable, I had to give best-case timings on the hashing, and that means as few calls to microtime as possible.</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'ITERATIONS'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$th</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">(</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;</span> ITERATIONS<span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$j</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
	<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">(</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">(</span><span style="color: #000088;">$z</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">(</span><span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$2a$07$'</span> <span style="color: #339933;">.</span> <span style="color: #009900;">(</span>string<span style="color: #009900;">)</span><span style="color: #000088;">$z</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
	<span style="color: #000088;">$tt</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">(</span><span style="color: #000088;">$z</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$th</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">(</span><span style="color: #000088;">$tt</span> <span style="color: #339933;">/</span> ITERATIONS<span style="color: #339933;">,</span> <span style="color: #000088;">$th</span> <span style="color: #339933;">/</span> ITERATIONS<span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>Of paramount importance is the literal string prepended to the stored value.  The first four characters, <span style="font-family:'Courier New',courier,monospace;">$2a$</span>, simply instruct crypt to use the blowfish algorithm.  The next three, <span style="font-family:'Courier New',courier,monospace;">07$</span>, pass the number 7 as our log-base-2 argument, meaning the computation will run for 2<sup>7</sup> iterations.  After that, we concatenate our salt (values shorter than 22 characters will be padded in a predictable fashion, and longer than 22 will be truncated) to the argument string and send it off on its merry, 12ms way.</p>
<p>Do I think I&#8217;ve defeated all the clever crackers out there?  Certainly not.  However, I&#8217;m definitely in a better boat for having stood on the shoulders of giants and listened to people smarter than I am about security.  In fact, don&#8217;t listen to me, check out these links for more info:</p>
<p><a href="http://vwng.blogspot.com/2008/01/php-hash.html">(Victor) Xi Wang talks about salt, nonces and rainbow tables</a></p>
<p><a href="http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html">Matasano Security, LLC, talks about blowfish and why you shouldn&#8217;t design your own password protection scheme.</a></p>
<p><a href="http://www.bletchleypark.net/cryptology/blowfish.html">Linked earlier, explains blowfish encryption &#8211; very math/pseudocode heavy.</a></p>
<p><a href="http://php.net/manual/en/function.crypt.php">Also linked earlier, the PHP Manual Entry for Crypt()</a></p>
<p>Happy Hashing!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereleased.com/2010/02/09/lets-talk-about-your-password-model/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Arrays of Objects and __get: Friends Forever</title>
		<link>http://www.dereleased.com/2010/01/11/arrays-of-objects-and-__get-friends-forever/</link>
		<comments>http://www.dereleased.com/2010/01/11/arrays-of-objects-and-__get-friends-forever/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 22:51:13 +0000</pubDate>
		<dc:creator>Clark</dc:creator>
				<category><![CDATA[Design Considerations]]></category>
		<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[PHP Quirks]]></category>
		<category><![CDATA[The Internet]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[ArrayObject]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[object-array]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[visibility]]></category>

		<guid isPermaLink="false">http://www.dereleased.com/?p=138</guid>
		<description><![CDATA[In PHP, an object is always passed around as a reference, which allows one to deal with objects in a very transparent manner, since the only way to deal with a by-value copy instead of the real deal is to explicitly use the clone operator. Recently, I came upon a situation in which it was [...]]]></description>
			<content:encoded><![CDATA[<p>In PHP, an object is always passed around as a reference, which allows one to deal with objects in a very transparent manner, since the only way to deal with a by-value copy instead of the real deal is to explicitly use the <strong>clone</strong> operator.  Recently, I came upon a situation in which it was very useful for me to have an array of objects inside an object; the scenario was somewhat simple, a parent object can contain an indefinite number of children, and in order to have easy access to them I created a lazy loading property to contain them all as an array, indexed by their unique IDs.  Of course, setting the stage for that is a bit more complicated than is needed for this example, so here is an extremely minimal example:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> foo <span style="color: #009900;">{</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #009900;">)</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;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">(</span><span style="color: #000088;">$n</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<p>So now we have a simple object with an array whose single element is an instance of PHP&#8217;s default object, stdClass.  In reality you&#8217;d likely have more than just one element to the array, but it&#8217;s not necessary here to prove the point.  Now, since objects are always returned by reference, accessing the first index of the array returned by __get when you try to access any member will allow you unfettered access to the contents of the object, to do with what you will (or rather, what the object will allow you to do).</p>
<p>With that in mind, let&#8217;s examine this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> foo<span style="color: #339933;">;</span>
<span style="color: #000088;">$foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">]</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">baz</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'I am a test'</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>This code is pretty easy to follow, and in fact does exactly what you&#8217;d expect: the stdClass object sitting in the first element of the &#8220;bar&#8221; array has a new member, &#8220;baz&#8221;, defined and assigned.  Viewing the contents of the object will show that this is exactly what happened:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;">  <span style="color: #009900;">[</span><span style="color: #0000ff;">"bar"</span><span style="color: #339933;">:</span><span style="color: #0000ff;">"foo"</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">private</span><span style="color: #009900;">]</span><span style="color: #339933;">=&gt;</span> <span style="color: #009900;">{</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    <span style="color: #009900;">[</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">]</span><span style="color: #339933;">=&gt;</span>
    object<span style="color: #009900;">(</span>stdClass<span style="color: #009900;">)</span><span style="color: #666666; font-style: italic;">#2 (1) {
</span>      <span style="color: #009900;">[</span><span style="color: #0000ff;">"baz"</span><span style="color: #009900;">]</span><span style="color: #339933;">=&gt;</span>
      string<span style="color: #009900;">(</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">)</span> <span style="color: #0000ff;">"I am a test"</span>
    <span style="color: #009900;">}</span>
  <span style="color: #009900;">}</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<p>However, there&#8217;s a problem.  Somewhere along the line, we generated a notice:</p>
<blockquote><p>Notice: Indirect modification of overloaded property foo::$bar has no effect in &#8230;</p></blockquote>
<p>While the notice certainly won&#8217;t halt the script&#8217;s execution, and the expected (and desired) action has taken place with no other apparent side effects, we are left with the conundrum of what to do with this notice (Note: While <a href="http://bugs.php.net/bug.php?id=41641">this issue has been brought to the attention of the PHP team</a>, no word of a fix has yet surfaced).  Since I am a firm believer that Notices and Warnings are potentially more dangerous than Fatal Errors, I won&#8217;t simply turn off error reporting; indeed, since the errors are still raised that doesn&#8217;t completely fix the small performance hit of generating the error, either.</p>
<p>In order to address this issue, it is important to understand what the notice is trying to tell us.  Once upon a time, __get was a return-by-reference function by default.  Of course, this doesn&#8217;t really help with wanting to prevent the modification of an object&#8217;s internal data, so __get was corrected to always return by value; in fact, even objects are &#8220;returned by value&#8221; in this case, since the value of the member variable is being returned (which just happens to also be a reference to an object), whereas the old __get would have returned a reference to the member variable itself; while the difference may seem subtle, it is monumental.  Since this change occurred, it was important to notify coders that if they attempted to modify the contents of an array element which came from an overloaded array, this action would have no effect, as the modified element would only exist in the copy returned from __get.</p>
<p>Armed with this knowledge of history, we have a few obvious options for solving this problem</p>
<ol>
<li><span style="font-weight:bold;font-family: 'Courier New',courier,mono;">public function &#038; __get($n)</span>.  This will technically prevent the warning from coming up, but if you&#8217;re going to go this route you might as well just declare all your member variables as public anyway, as this is what it will effectively cause __get to do.  It opens the door to such dangerous situations as:
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>That&#8217;s right, if you return by reference explicitly in __get, then you will circumvent any rules you&#8217;ve set for assignment via __set.  Even objects are not immune to this, as a reference to the member variable (itself containing a reference) will be returned.  This option removes the efficacy of even having visibility operators for anything you intend to provide overloaded access to.</li>
<li><strong>Assign a variable to the contents of the array element</strong>.  Again, technically, this works, but it is messy, inelegant, and is nowhere near the ideal.  Here are two examples:
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">]</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">baz</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This works'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">###
</span><span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #009900;">[</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">baz</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This also works'</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>Again, though, this is not the clean, simple approach we were looking for to begin with.
</li>
<li><strong>Just turn off notices</strong>.  Nah, we ain&#8217;t doin&#8217; that.</li>
</ol>
<p>So what&#8217;s left to consider?  After thinking about the problem for a little while, I realized that this problem wouldn&#8217;t even exist if I could just store the array as an object instead, but objects don&#8217;t allow numerical indices, so it would take a little jimmy-rigging to get it to work.  Here was the first version:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> arrayReference <span style="color: #009900;">{</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">(</span><span style="color: #009900;">)</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> __set<span style="color: #009900;">(</span><span style="color: #000088;">$n</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_<span style="color: #009900;">[</span><span style="color: #000088;">$n</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">(</span><span style="color: #000088;">$n</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">(</span><span style="color: #000088;">$n</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_<span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_<span style="color: #009900;">[</span><span style="color: #000088;">$n</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">}</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_<span style="color: #009900;">[</span><span style="color: #000088;">$n</span><span style="color: #009900;">]</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_<span style="color: #009900;">[</span><span style="color: #000088;">$n</span><span style="color: #009900;">]</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">(</span><span style="color: #000088;">$n</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #000088;">$n</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'array'</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_<span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">}</span>
	<span style="color: #009900;">}</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> _<span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_<span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
<span style="color: #009900;">}</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> foo <span style="color: #009900;">{</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</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;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> arrayReference<span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">(</span><span style="color: #000088;">$n</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
<span style="color: #009900;">}</span>
&nbsp;
<span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> foo<span style="color: #339933;">;</span>
<span style="color: #000088;">$foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$foo</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">99</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #339933;">;</span>
<span style="color: #000088;">$foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">99</span><span style="color: #009900;">}</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">baz</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">33</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>Which, for the adjusted syntax, actually worked out pretty well.  It might take more than an instant glance from your average PHP coder for what&#8217;s going on to make sense, or even seem syntactically correct, but it certainly worked; it even allowed for loop-based iteration by doing something like so:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span><span style="color: #009900;">(</span><span style="color: #000088;">$foo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">array</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">)</span></pre>
</div>
</div>
<p>While that isn&#8217;t ideal, it&#8217;s fairly transparent about what it&#8217;s doing.</p>
<p>I wish there were a more climactic way to put this, but there isn&#8217;t: The next step involved me trying to combine the SPL&#8217;s ArrayObject built in class to allow natural array access to my wrapper class, and after a few minutes playing with my new hideous child-beast amalgamate and its Reflection, I finally settled on this for the final version of the class:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> foo <span style="color: #009900;">{</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</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;">(</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> arrayObject<span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">(</span><span style="color: #000088;">$n</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bar</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<p>No more messy syntax, no compromises, no hideous amalgamate beasts, and no figuring out how to mangle my behemoth class this lesson actually needed to be applied to in order to extend ArrayObject for the purposes of accessing just one property, as I saw <a href="http://weierophinney.net/matthew/archives/131-Overloading-arrays-in-PHP-5.2.0.html">advocated elsewhere during the googleing portion of my problem solving routine</a>.  The example I first gave?  Works just fine, and no error since the property being returned is an object, not an array.  Sometimes the best solution is fiendishly simple; the only real consideration I had to make here was that, in its actual application, the array in question was declared null so it could be lazy loaded, and since you can&#8217;t use the &#8220;new&#8221; keyword or even type-hinting in class member declarations, I had to be careful to make sure the lazy loading mechanism would still work, but I was never declaring a traditional array either: all in all, a 5 minute job to implement and test.</p>
<p>Five minutes that made the past day or so of work seem rather silly indeed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereleased.com/2010/01/11/arrays-of-objects-and-__get-friends-forever/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

