<?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>Clarity Amidst Chaos</title>
	<atom:link href="http://clarityamidstchaos.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://clarityamidstchaos.com</link>
	<description>Notes from Moments of Clarity in a Chaotic Mind</description>
	<lastBuildDate>Tue, 06 Jul 2010 17:57:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Think before you serialize.</title>
		<link>http://clarityamidstchaos.com/2010/07/06/think-before-you-serialize/</link>
		<comments>http://clarityamidstchaos.com/2010/07/06/think-before-you-serialize/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 17:57:56 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/?p=228</guid>
		<description><![CDATA[I recently ran into an interesting problem as a result of using PHP&#8217;s serialize/unserialize functionality in a &#8230; well, a stupid way.
Background
For a brief background, I was working on a feature using a bunch of different classes.  These classes could be manipulated by the end-user by changing around options, adding and removing items from [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into an interesting problem as a result of using PHP&#8217;s serialize/unserialize functionality in a &#8230; well, a stupid way.</p>
<p><strong>Background</strong><br />
For a brief background, I was working on a feature using a bunch of different classes.  These classes could be manipulated by the end-user by changing around options, adding and removing items from lists, and so on.  I thought it would be simple and convenient to just serialize these objects right into the database (I was working within a schema and was limited to using a single text blob, so one way or another I was going to be serializing data).</p>
<p><strong>The problem</strong><br />
Originally, my classes used nothing but private members and accessors where needed.  More recently, I wanted to change some of these private members to public, and when I did so my existing serialized objects broke!  What I found was this:</p>
<blockquote><p>When PHP attempts to unserialize() data containing private members into a class that contains public members of the same name, it produces an object that has both the private and public members of the same name.</p></blockquote>
<p>So I ended up with an object who had two members, both with the same name, and $this-&gt;member was only returning the public member.  The private member, which actually held information, was inaccessible.  I looked for some magic __wakeup hackery to work around this but failed.  Ultimately, since my code changes were now relying on public behavior, I used __get and __set to mimic it, along with a comment block explaining why I have all private members, and all exposed directly via __get and __set.  Ugly, ugly stuff.</p>
<p><strong>The lesson</strong><br />
Be careful what you do with serialized data, especially if you&#8217;re planning to keep it around for a while.  You can modify the methods of your serialized class all you want, since methods are not serialized, but your data structure (the members) may become much trickier to update in the future.</p>
<p>I didn&#8217;t test this all fully, so I&#8217;m not sure what exactly is required to trigger this problem.  For example, I know that replacing serialized public members with private members causes problems&#8230; what about the other direction?  What about removing a public member from the class &#8211; would this cause the serialized public member to show up anyhow?  I&#8217;m guessing it would.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/07/06/think-before-you-serialize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I never understood&#8230;</title>
		<link>http://clarityamidstchaos.com/2010/06/19/i-never-understood/</link>
		<comments>http://clarityamidstchaos.com/2010/06/19/i-never-understood/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 13:54:22 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Social commentary]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/?p=219</guid>
		<description><![CDATA[I never understood why people begin sentences with &#8220;I never understood why people&#8221; when they totally do.
]]></description>
			<content:encoded><![CDATA[<p>I never understood why people begin sentences with &#8220;I never understood why people&#8221; when they totally do.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/06/19/i-never-understood/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My artwork that I have made by myself.  Me.</title>
		<link>http://clarityamidstchaos.com/2010/06/12/my-artwork-that-i-have-my-by-myself-me/</link>
		<comments>http://clarityamidstchaos.com/2010/06/12/my-artwork-that-i-have-my-by-myself-me/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 19:51:57 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Artwork]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/?p=217</guid>
		<description><![CDATA[For anybody who is feeling self-abusive, here is some terrible artwork I have created over the years, mostly in Photoshop, which I am terrible at using.  Please comment and tell me how terrible you think it is, for I am nothing without the validation of strangers.
]]></description>
			<content:encoded><![CDATA[<p>For anybody who is feeling self-abusive, here is some terrible artwork I have created over the years, mostly in Photoshop, which I am terrible at using.  Please comment and tell me how terrible you think it is, for I am nothing without the validation of strangers.</p>
<div id="attachment_207" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/Stormscape.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/Stormscape-300x234.jpg" alt="zomg clouds over a cheezy mountain" width="300" height="234" class="size-medium wp-image-207" /></a><p class="wp-caption-text">zomg clouds over a cheezy mountain</p></div><br />
<div id="attachment_208" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/snowy_header.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/snowy_header-300x52.jpg" alt="We were out!  I was on to a big .... dish of peas!" width="300" height="52" class="size-medium wp-image-208" /></a><p class="wp-caption-text">We were out!  I was on to a big .... dish of peas!</p></div><br />
<div id="attachment_209" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/Skyscape.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/Skyscape-300x234.jpg" alt="How is it floating?  What are those circles?  Are these questions related?  No one will ever know." width="300" height="234" class="size-medium wp-image-209" /></a><p class="wp-caption-text">How is it floating?  What are those circles?  Are these questions related?  No one will ever know.</p></div><br />
<div id="attachment_210" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/Seascape.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/Seascape-300x225.jpg" alt="Where lakes come from.   Waterfalls." width="300" height="225" class="size-medium wp-image-210" /></a><p class="wp-caption-text">Where lakes come from.   Waterfalls.</p></div><br />
<div id="attachment_211" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/pen.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/pen-300x169.jpg" alt="A mosaic of penned landscaping." width="300" height="169" class="size-medium wp-image-211" /></a><p class="wp-caption-text">A mosaic of penned landscaping.</p></div><br />
<div id="attachment_212" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/Nightscape.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/Nightscape-300x225.jpg" alt="Is it night?  Or is the sun playing tricks on you?  Magic." width="300" height="225" class="size-medium wp-image-212" /></a><p class="wp-caption-text">Is it night?  Or is the sun playing tricks on you?  Magic.</p></div><br />
<div id="attachment_213" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/mountain.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/mountain-300x296.jpg" alt="Feel-good mountain of the year." width="300" height="296" class="size-medium wp-image-213" /></a><p class="wp-caption-text">Feel-good mountain of the year.</p></div><br />
<div id="attachment_214" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/island.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/island-300x225.jpg" alt="An island scene, much like a desert scene, but with less desert and more water." width="300" height="225" class="size-medium wp-image-214" /></a><p class="wp-caption-text">An island scene, much like a desert scene, but with less desert and more water.</p></div><br />
<div id="attachment_216" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/Fogscape.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/Fogscape-300x225.jpg" alt="Any minute now, Aerosmith is going to jump through the fog and start rocking out." width="300" height="225" class="size-medium wp-image-216" /></a><p class="wp-caption-text">Any minute now, Aerosmith is going to jump through the fog and start rocking out.</p></div><br />
<div id="attachment_205" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/desert.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/desert-300x225.jpg" alt="Desert landscape with green hills." width="300" height="225" class="size-medium wp-image-205" /></a><p class="wp-caption-text">Desert landscape with green hills.</p></div><br />
<div id="attachment_206" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.clarityamidstchaos.com/files/2010/06/cave.jpg"><img src="http://www.clarityamidstchaos.com/files/2010/06/cave-300x156.jpg" alt="Dimly lit cave" width="300" height="156" class="size-medium wp-image-206" /></a><p class="wp-caption-text">Dimly lit cave</p></div>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/06/12/my-artwork-that-i-have-my-by-myself-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iptables and traceroute</title>
		<link>http://clarityamidstchaos.com/2010/06/06/iptables-and-traceroute/</link>
		<comments>http://clarityamidstchaos.com/2010/06/06/iptables-and-traceroute/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 12:51:34 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/?p=198</guid>
		<description><![CDATA[I had to do a traceroute yesterday and realized my iptables rules were preventing it.  traceroute www.google.com gave me 30 hops of * * *; useless data.
It seemed like a simple problem with a simple solution, but since I didn&#8217;t find the simple solution as easily as I&#8217;d hoped, I thought I would echo it [...]]]></description>
			<content:encoded><![CDATA[<p>I had to do a traceroute yesterday and realized my iptables rules were preventing it.  <code>traceroute www.google.com</code> gave me 30 hops of <code>* * *</code>; useless data.</p>
<p>It seemed like a simple problem with a simple solution, but since I didn&#8217;t find the simple solution as easily as I&#8217;d hoped, I thought I would echo it here.  In my case, I was able to perform traceroutes by adding these rules to my <code>INPUT</code> chain:</p>
<blockquote><p>-A INPUT &#8211;proto icmp &#8211;icmp-type destination-unreachable -j ACCEPT<br />
-A INPUT &#8211;proto icmp &#8211;icmp-type time-exceeded -j ACCEPT<br />
-A INPUT&#8211;proto icmp &#8211;icmp-type echo-reply -j ACCEPT<br />
-A INPUT&#8211;proto icmp &#8211;icmp-type echo-request -j ACCEPT</p></blockquote>
<p>Note that, in my case, I <code>ACCEPT</code> all <code>OUTPUT</code>, so outbound conversation was never a problem, only inbound.  I&#8217;m fairly certain the rule that actually fixed my problem was the third, which allows <code>echo-reply</code> packets to come in.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/06/06/iptables-and-traceroute/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My new piano!</title>
		<link>http://clarityamidstchaos.com/2010/06/01/my-new-piano/</link>
		<comments>http://clarityamidstchaos.com/2010/06/01/my-new-piano/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 20:37:16 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Music]]></category>
		<category><![CDATA[piano]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/?p=189</guid>
		<description><![CDATA[This is my newly delivered piano.  How exciting!

]]></description>
			<content:encoded><![CDATA[<p>This is my newly delivered piano.  How exciting!</p>
<div id="attachment_190" class="wp-caption aligncenter" style="width: 160px"><a href="http://www.clarityamidstchaos.com/files/2010/06/piano-1.jpg"><img class="size-thumbnail wp-image-190 " src="http://www.clarityamidstchaos.com/files/2010/06/piano-1-150x150.jpg" alt="My piano in its new home" width="150" height="150" /></a><p class="wp-caption-text">My piano in its new home</p></div>
<p style="text-align: center"><a href="http://www.clarityamidstchaos.com/files/2010/06/piano-2.jpg"><img class="aligncenter size-thumbnail wp-image-191" src="http://www.clarityamidstchaos.com/files/2010/06/piano-2-150x150.jpg" alt="Hammers and strings" width="150" height="150" /></a><a href="http://www.clarityamidstchaos.com/files/2010/06/piano-3.jpg"><img class="aligncenter size-thumbnail wp-image-192" src="http://www.clarityamidstchaos.com/files/2010/06/piano-3-150x150.jpg" alt="Hammers and strings - closeup" width="150" height="150" /></a><a href="http://www.clarityamidstchaos.com/files/2010/06/piano-4.jpg"><img class="aligncenter size-thumbnail wp-image-193" src="http://www.clarityamidstchaos.com/files/2010/06/piano-4-150x150.jpg" alt="Hammers and strings - closeup" width="150" height="150" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/06/01/my-new-piano/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easy, classless, protected global variables: pattern for PHP.</title>
		<link>http://clarityamidstchaos.com/2010/05/04/easy-classless-protected-global-variables-pattern-for-php/</link>
		<comments>http://clarityamidstchaos.com/2010/05/04/easy-classless-protected-global-variables-pattern-for-php/#comments</comments>
		<pubDate>Tue, 04 May 2010 15:12:19 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[global]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/?p=160</guid>
		<description><![CDATA[If you ever wrap a class around a variable just to make it a globally available singleton, here&#8217;s a simpler approach.  I haven&#8217;t done cost tests, so feel free to post here if you do.

&#60;?php

function name($new = null) {
    static $name = 'Unknown';  // default value, if desired.
   [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever wrap a class around a variable just to make it a globally available singleton, here&#8217;s a simpler approach.  I haven&#8217;t done cost tests, so feel free to post here if you do.</p>
<pre>
&lt;?php

function name($new = null) {
    static $name = 'Unknown';  // default value, if desired.
    if ($new !== null) {
        $name = $new;
    }
    return $name;
}

?&gt;
</pre>
<p>I think it&#8217;s pretty self explanatory.  The <code>static</code> keyword is important, as it tells PHP to perserve that variable&#8217;s value within that function&#8217;s scope between function calls.</p>
<p>With a little imagination, you have a more generic function that can be used as an application-wide register in which you can store any data by key.  Throw this at the start of your application (rename the function if necessary) for a poor man&#8217;s namespace solution:</p>
<ol>
<li>You could add a $namespace argument to the function.</li>
<li>Or you could have separate copies of this function, each named after its spiritual namespace.  For example, <code>widget()</code> would be for my &#8220;widget&#8221; namespace, and <code>foobar()</code> would be for my &#8220;foobar&#8221; namespace.</li>
</ol>
<pre>
&lt;?php

function get($key, $new = null)
{
    static $data = array();
    if ($new !== null) {
        return $data[$key] = $new;
    }
    return array_key_exists($key, $data) ? $data[$key] : null;
}

?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/05/04/easy-classless-protected-global-variables-pattern-for-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: Variable Argument Functions by Reference</title>
		<link>http://clarityamidstchaos.com/2010/04/04/php-variable-argument-functions-by-reference/</link>
		<comments>http://clarityamidstchaos.com/2010/04/04/php-variable-argument-functions-by-reference/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 14:53:16 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/?p=152</guid>
		<description><![CDATA[Introduction
PHP lets you implement variable argument functions using functions like func_get_args, and func_num_args.  However, when you want to get references involved, things get a little bit tricky.  Variable length argument lists as references can be a useful thing, such as if you&#8217;re implementing some kind of filtering layer.  Below I discuss some patterns for making [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>PHP lets you implement variable argument functions using functions like func_get_args, and func_num_args.  However, when you want to get references involved, things get a little bit tricky.  Variable length argument lists as references can be a useful thing, such as if you&#8217;re implementing some kind of filtering layer.  Below I discuss some patterns for making this work, with pros and cons for each.</p>
<p><strong>Warning:</strong> I don&#8217;t know how or if the below code will be interpreted by PHP 5.3.  The information below is accurate as of 5.1.6.</p>
<p><strong>Other warning:</strong> The examples below use some practices that would be considered bad practice.  This is because some of the things you can do in PHP are risky if done wrong, and easy to do wrong.  My perspective on these practices is always the same: if you know why its risky, and you come up with a way for it to be zero risk and useful, then use it.  Best practices are best interpreted as a theoretical guide, not a cardinal rule.  Plus, I&#8217;m a rebel.</p>
<h2>Arbitrary Default Arguments</h2>
<p>Hack alert.  This method is definitely a hack, but at the same time it is inexpensive (from a design and CPU standpoint) and has some advantaged.</p>
<blockquote><p>&lt;?php</p>
<p>// This function will receive arguments by reference and modify them.<br />
function receive_variable_references(&amp;$a1 = null, &amp;$a2 = null, &amp;$a3 = null, &amp;$a4 = null,<br />
&amp;$a5 = null, &amp;$a6 = null, &amp;$a7 = null, &amp;$a8 = null, &amp;$a9 = null, &amp;$a10 = null) {</p>
<p>	// This will tell you how many arguments this function was invoked with.<br />
	$num_args = func_num_args();</p>
<p>	// Make your changes to $a1 through $a10.<br />
	$a1 = &#8220;changed&#8221;;<br />
}</p>
<p>$my_arg = &#8216;foobar&#8217;;<br />
receive_variable_references($my_arg);<br />
echo &#8220;$my_arg\n&#8221;;  // This will output &#8220;changed&#8221;</p>
<p>?&gt;</p></blockquote>
<h3>Discussion</h3>
<h4>Pros</h4>
<ul>
<li>Easy to call with an argument list of variables (but not literals).</li>
</ul>
<h4>Cons</h4>
<ul>
<li>Fatal error if you call this function with literal data, such as the number 1 or a literal array.</li>
<li>Limited to a certain number of referenced arguments (ten in the example above).</li>
</ul>
<p>PHP&#8217;s methods for fetching arguments arbitrarily (name, func_get_args) will always return a copy.  End of story.  Thus, if you want references, you need to make them yourself.  By defining 10 arbitrarily named arguments, all as references, and all defaulted to null, we can get references to any of the first 10 arguments passed into the function.  If the caller omits some of them, or all of them, they will gracefully default to null.</p>
<p>The method above is, well, a hack.  But it&#8217;s well-contained in one spot,  and does its job well for a very limited set of situations.  If you know you will be invoking this function with only variables (not literals), and will never need more than a certain number, the above solution is actually more elegant than it looks.  Hide the ugly inside the function, and the calling funcions never need to know about it.</p>
<p>If you need to know how many arguments the caller provided, you can still use func_num_args, which will return how many arguments the function was <strong>invoked</strong> with, not how many it received.  In other words, func_num_args tells you how many arguments the caller provided, regardless of how many defaults you have listed in your arguments list.</p>
<h2>Arrays of references</h2>
<p>You can eliminate the cons (and, incidentally, the pro) above by moving the referencing into the caller.  &#8220;But wait,&#8221; you say, &#8220;you can&#8217;t pass arguments by reference, you can only <em>receive</em> arguments by references.&#8221;  Well, I pity your naivete and in my graciousness shall open your mind.</p>
<blockquote><p>&lt;?php</p>
<p>function receive_variable_references($refs = array()) {<br />
	$refs[0] = &#8216;Bubba&#8217;;<br />
	$refs[1] = &#8216;green-cheeked conure&#8217;;<br />
	$refs[2] = &#8216;evil, with a little blue&#8217;;<br />
}</p>
<p>$name = &#8220;Jimmy&#8221;;<br />
$type = &#8220;brown-headed parrot&#8221;;<br />
$color = &#8220;more or less green&#8221;;<br />
receive_variable_references(array(&amp;$name, &amp;$type, &amp;$color));</p>
<p>echo &#8220;Name: $name\nType: $type\nColor: $color\n&#8221;;</p>
<p>?&gt;</p></blockquote>
<h3>Discussion</h3>
<h4>Pros</h4>
<ul>
<li>Unlimited references.</li>
<li>Function is not receiving references directly, and thus can accept literals as well.</li>
</ul>
<h4>Cons</h4>
<ul>
<li>More convoluted calling mechanism (passing arrays of references instead of direct values).</li>
</ul>
<p>If your function doesn&#8217;t receive its arguments by reference (as in this example), it receives copies instead.  We&#8217;re taking advantage of the fact that a copy of a reference to $a is still a reference to $a, just another copy of it.  Practically speaking, they&#8217;re identical.  In this case we&#8217;re creating an array of references (you can do that, because array(&#8230;) is a language construct, not a function) and passing that in.  On the function side, a copy of this array is what is actually received, but these copied references still point where we want them to.  This is where I show you a colorful graphic demonstrating the references and copies, and it&#8217;s very enlightening.</p>
<p>The con here is only questionably a con.  You could say that this method puts the burden of passing references on the caller, or you could say it gives the control of passing references to the caller.</p>
<p>If you are astute, you may have considered that <a title="PHP: compact" href="http://www.php.net/compact" target="_blank">compact</a> would go well with this method; it doesn&#8217;t.  compact will not return references, only copies, thus undermining the pattern we&#8217;re using.</p>
<h2>Calling variable length functions</h2>
<blockquote><p>&lt;?php</p>
<p>function receive_variable_references(&amp;$a1 = null, &amp;$a2 = null, &amp;$a3 = null, &amp;$a4 = null,<br />
&amp;$a5 = null, &amp;$a6 = null, &amp;$a7 = null, &amp;$a8 = null, &amp;$a9 = null, &amp;$a10 = null) {</p>
<p>	// This will tell you how many arguments this function was invoked with.<br />
	$num_args = func_num_args();</p>
<p>	// Make your changes to $a1 through $a10.<br />
	$a1 = &#8220;changed&#8221;;<br />
}</p>
<p>$arg = &#8216;original&#8217;;<br />
call_user_func_array(&#8217;receive_variable_references&#8217;, array(&amp;$arg));<br />
echo &#8220;$arg\n&#8221;;</p>
<p>?&gt;</p></blockquote>
<h3>Discussion</h3>
<p>We&#8217;ve gone back to our first example and shown how to call it with a variable list of arguments.  By providing an array of references to call_user_func_array, we ensure that the invoked function is receiving references, not copies.  If you try replacing the above with call_user_func and passing in literal references (instead of a literal array of references), you will get an warning about passing by reference, which is deprecated (and thus we don&#8217;t want to do it).</p>
<h2>Example</h2>
<p>This example mixes and matches some of the tips above to demonstrate a pattern you can use for functions that have access to their caller&#8217;s scope.  Obviously, this can be dangerous, so make sure you understand the security implications of what this does.  If you&#8217;re wondering, &#8220;What security implications?&#8221; then don&#8217;t use this.  If you&#8217;re thinking, &#8220;Oh yeah, <em>those</em> security implications, I&#8217;ll keep those in mind,&#8221; then you still probably shouldn&#8217;t use this.  In fact, I&#8217;m not sure why I&#8217;m demonstrating such a terrible idea.  Enjoy.</p>
<blockquote><p>&lt;?php</p>
<p>function some_filter($scope) {<br />
	$scope['line1'] = &#8216;All your&#8217;;<br />
	$scope['line2'] = &#8216;Base are&#8217;;<br />
	$scope['line3'] = &#8216;Belong to&#8217;;<br />
	$scope['line4'] = &#8216;An Internet meme from 2000.  Seriously.  Let it die.&#8217;;<br />
}</p>
<p>$line1 = &#8216;Mary had a little lamb&#8217;;<br />
$line2 = &#8216;Whose fleece was white as snow&#8217;;<br />
$line3 = &#8216;Jack and Jill went up the hill&#8217;;<br />
$line4 = &#8216;And god only knows what they did up there.  I\&#8217;m just saying, they were gone for an awfully long time.&#8217;;</p>
<p>$args = get_defined_vars();<br />
$refs = array();<br />
unset($args['GLOBALS'], $args['_FILES'], $args['_SERVER'], $args['_COOKIE'], $args['_GET'], $args['_POST'], $args['argc'], $args['argv']);<br />
foreach ($args as $ident =&gt; $val) {<br />
	$refs[$ident] = &amp;$$ident;<br />
}<br />
some_filter($refs);</p>
<p>echo &lt;&lt;&lt;END<br />
$line1<br />
$line2<br />
$line3<br />
$line4</p>
<p>END;</p>
<p>?&gt;</p></blockquote>
<h3>Discussion</h3>
<p>We pull a list of variables in the current scope with get_defined_vars, and then prune out the ones we&#8217;re not likely to want.  At the very least, there is no point in including superglobals like $_GET and $_POST, since they will already be available in the invoked function.</p>
<p>We build up an array of references to these, each mapped to a key of the same name.  &amp;$$ident will give us a reference to a variable whose name is the string value of $ident.  We have control over $ident the entire time we&#8217;re using it in this way so as to avoid the potential for an attacker to modify this behavior.</p>
<p>When some_filter is called with our array of references, we effectively has passed the current scope into some_filter.</p>
<p>This pattern is exceptionally useful for a plugin-based architecture.  If you want to give plugins full control to modify your current behavior (likely enough control to break things), this is a brute-force but fully flexible means of doing so.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/04/04/php-variable-argument-functions-by-reference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Useful PHP Constants</title>
		<link>http://clarityamidstchaos.com/2010/03/30/useful-php-constants/</link>
		<comments>http://clarityamidstchaos.com/2010/03/30/useful-php-constants/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 20:03:24 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[constants]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/?p=150</guid>
		<description><![CDATA[PHP has a whole bunch of built-in constants that, some of which are particularly useful.  They aren&#8217;t commonly used in the wild, but I believe that&#8217;s simply due to lack of awareness.  This is probably why I, after years of PHP development, only recently became aware of them.
Using built-in constants has a few [...]]]></description>
			<content:encoded><![CDATA[<p>PHP has a whole bunch of built-in constants that, some of which are particularly useful.  They aren&#8217;t commonly used in the wild, but I believe that&#8217;s simply due to lack of awareness.  This is probably why I, after years of PHP development, only recently became aware of them.</p>
<p>Using built-in constants has a few advantages, namely portability and less likelihood for errors.</p>
<h1>Highlights</h1>
<p><code>PHP_EOL</code>: This is a newline character.  For example, <code>echo $some_variable . PHP_EOL</code>.<br />
<code>DATE_W3C</code>: This is one of a few <code>DATE_*</code> constants, which are format strings suitable for in the date() function.  This one, in particular, plays well with MySQL.  If you have a hard time remembering the right format for getting a date into MySQL, or just don&#8217;t want to, use this.  For example, <code>mysql_query(sprintf('INSERT INTO `my_table` (`date_col`) VALUES "%s"', date(DATE_W3C)))</code>.</p>
<h1>The whole list</h1>
<p>If you want to see all of the constants PHP defines out the door, just put this simple script into a file and run it from the command line.  I don&#8217;t recommend exposing it via a public website since it will expose information best left private.</p>
<p><code><br />
 $v) {<br />
	printf("%30s: %s\n", $n, $v);<br />
}</p>
<p>?&gt;<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/03/30/useful-php-constants/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bookmark Firefox&#8217;s Cookie Window</title>
		<link>http://clarityamidstchaos.com/2010/03/29/bookmark-firefoxs-cookie-window/</link>
		<comments>http://clarityamidstchaos.com/2010/03/29/bookmark-firefoxs-cookie-window/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 01:48:06 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[cookies]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/?p=147</guid>
		<description><![CDATA[Short Answer
Bookmark the following URL:
chrome://browser/content/preferences/cookies.xul
Short-ish Answer
FireFox stores alol of its UI elements in files called XUL files.  chrome:// URLs point to these various XUL files on your hard drive.  They live inside JAR files (Java ARchives), which are essentially zip files.  Chrome URLs beginning with chrome://browser will point specifically to a file called browser.jar, which [...]]]></description>
			<content:encoded><![CDATA[<h1>Short Answer</h1>
<p>Bookmark the following URL:</p>
<p><code>chrome://browser/content/preferences/cookies.xul</code></p>
<h1>Short-ish Answer</h1>
<p>FireFox stores alol of its UI elements in files called XUL files.  <code>chrome://</code> URLs point to these various XUL files on your hard drive.  They live inside JAR files (Java ARchives), which are essentially zip files.  Chrome URLs beginning with <code>chrome://browser</code> will point specifically to a file called browser.jar, which lives in your Firefox installation directory.  On Windows 7, in my case, this was at <code>C:\Program Files (x86)\Mozilla Firefox\chrome\browser.jar</code>.</p>
<p>From there, the rest of the URL is pointing within the JAR file.  In this case, we&#8217;re looking at content/browser/preferences/cookies.xul, which is within browser.jar.</p>
<p>It&#8217;s that simple.  When you point FireFox to a chrome URL, assuming it exists, it will be displayed.  To see a useless but funny example of this, try this URL:</p>
<p><code>chrome://browser/content/browser.xul</code></p>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/03/29/bookmark-firefoxs-cookie-window/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Stat Caching and Process Forking</title>
		<link>http://clarityamidstchaos.com/2010/03/09/php-stat-caching-and-process-forking/</link>
		<comments>http://clarityamidstchaos.com/2010/03/09/php-stat-caching-and-process-forking/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 18:39:04 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[clearstatcache]]></category>
		<category><![CDATA[forking]]></category>
		<category><![CDATA[pcntl_fork]]></category>

		<guid isPermaLink="false">http://www.clarityamidstchaos.com/2010/03/09/php-stat-caching-and-process-forking/</guid>
		<description><![CDATA[On Linux, a forked process inherits a copy of the parent&#8217;s cache, but after forking the two caches do not impact each other.  The snippet below demonstrates this by creating a child and confirming outdated (cached) information, then clearing the cache, and getting new information.

&#60;?php

function report($directory, $prefix = '') { printf('%sDoes %s exist?  [...]]]></description>
			<content:encoded><![CDATA[<p>On Linux, a forked process inherits a copy of the parent&#8217;s cache, but after forking the two caches do not impact each other.  The snippet below demonstrates this by creating a child and confirming outdated (cached) information, then clearing the cache, and getting new information.</p>
<pre>
&lt;?php

function report($directory, $prefix = '') { printf('%sDoes %s exist?  PHP says "%s"'. PHP_EOL, $prefix, $directory, is_dir($directory) ? 'yes' : 'no'); }
$target = './delete-me-before-running-statcache';

if (is_dir($target)) {
    die("Delete $target before running.\n");
}

echo "Creating $target.\n";
mkdir($target) || die("Unable to create $target.\n");
report($target); // is_dir($target) is now cached as true

echo "Unlinking $target.\n";
rmdir($target) || die("Unable to unlink $target.\n");

// This will say "yes", which is old (inaccurate) information.
report($target);

if (($pid = pcntl_fork()) === -1) { die("Failed to pcntl_fork.\n"); }
elseif ($pid === 0) {
    // child
    report($target, '&lt;&gt; ');
    echo "&lt;&gt; Clearing stat cache.\n";
    clearstatcache();
    report($target, '&lt;&gt; ');
} else {
    // parent
    sleep(2); // move this to the child block to reverse the test.
    report($target, '&lt;&lt;&gt; ');
    clearstatcache();
    report($target, '&lt;&lt;&gt; ');
}

?&gt;
</pre>
<p><strong>Output:</strong></p>
<pre>
Creating ./delete-me-before-running-statcache.
Does ./delete-me-before-running-statcache exist?  PHP says "yes"
Unlinking ./delete-me-before-running-statcache.
Does ./delete-me-before-running-statcache exist?  PHP says "yes"
&lt;&gt; Does ./delete-me-before-running-statcache exist?  PHP says "yes"
&lt;&gt; Clearing stat cache.
&lt;&gt; Does ./delete-me-before-running-statcache exist?  PHP says "no"
&lt;&lt;&gt; Does ./delete-me-before-running-statcache exist?  PHP says "yes"
&lt;&lt;&gt; Does ./delete-me-before-running-statcache exist?  PHP says "no"
</pre>
]]></content:encoded>
			<wfw:commentRss>http://clarityamidstchaos.com/2010/03/09/php-stat-caching-and-process-forking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
