<?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>Finite Hate Machine</title>
	<atom:link href="http://finite.grimoire.ca/feed/" rel="self" type="application/rss+xml" />
	<link>http://finite.grimoire.ca</link>
	<description>PHP is not my favourite language.</description>
	<lastBuildDate>Wed, 02 Feb 2011 15:18:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Stale incense, old sweat, and lies, lies, lies</title>
		<link>http://finite.grimoire.ca/2011/02/superglobal-surprise/</link>
		<comments>http://finite.grimoire.ca/2011/02/superglobal-surprise/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 15:17:46 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=124</guid>
		<description><![CDATA[Superglobals are even more global than you might reasonably expect.

&#60;?php
function foo&#40;$_SERVER&#41; &#123;
    /* do nothing */
&#125;
&#160;
foo&#40;'oh dear'&#41;;
var_dump&#40;$_SERVER&#41;;


This is obviously unreasonable code, but I don&#8217;t think anyone would automatically expect that function call to replace the $_SERVER superglobal&#8217;s value with the string 'oh dear'.

string(7) "oh dear"

Hat tip to @mark_story.
]]></description>
			<content:encoded><![CDATA[<p>Superglobals are even more global than you might reasonably expect.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* do nothing */</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
foo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'oh dear'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><!-- more --></p>
<p>This is obviously unreasonable code, but I don&#8217;t think anyone would automatically expect that function call to replace the <code>$_SERVER</code> superglobal&#8217;s value with the string <code>'oh dear'</code>.</p>
<pre>
string(7) "oh dear"
</pre>
<p>Hat tip to <a href="http://twitter.com/mark_story">@mark_story</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2011/02/superglobal-surprise/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>You make this all go away</title>
		<link>http://finite.grimoire.ca/2010/12/array-keys-surprise/</link>
		<comments>http://finite.grimoire.ca/2010/12/array-keys-surprise/#comments</comments>
		<pubDate>Fri, 17 Dec 2010 15:59:04 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Language Design]]></category>
		<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=118</guid>
		<description><![CDATA[Been a while. Have a simple one &#8211; what does this print?

$arr&#91;null&#93; = &#34;Null string&#34;;
$arr&#91;false&#93; = &#34;False string&#34;;
$arr&#91;0&#93; = &#34;Zero string&#34;;
&#160;
print_r&#40;$arr&#41;;


It&#8217;s&#8230;
Array ( [] => Null string [0] => Zero string )
Surprise!
It gets better:

$arr&#91;null&#93; = &#34;Null string&#34;;
$arr&#91;false&#93; = &#34;False string&#34;;
$arr&#91;0&#93; = &#34;Zero string&#34;;
$arr&#91;''&#93; = &#34;Empty string string&#34;;

gives
Array ( [] => Empty string string [0] => Zero [...]]]></description>
			<content:encoded><![CDATA[<p>Been a while. Have a simple one &#8211; what does this print?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Null string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;False string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Zero string&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-118"></span><br />
It&#8217;s&#8230;</p>
<pre>Array ( [] => Null string [0] => Zero string )</pre>
<p>Surprise!</p>
<p>It gets better:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Null string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;False string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Zero string&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$arr</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Empty string string&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>gives</p>
<pre>Array ( [] => Empty string string [0] => Zero string )</pre>
<p>Hat tip to Will for this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/12/array-keys-surprise/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Just when everything was making sense</title>
		<link>http://finite.grimoire.ca/2010/01/lambdarrrgh/</link>
		<comments>http://finite.grimoire.ca/2010/01/lambdarrrgh/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 00:06:39 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=97</guid>
		<description><![CDATA[Okay, let&#8217;s say you managed to ensure that your program doesn&#8217;t exhaust the heap with anonymous functions, and that you understand the edge cases involved in function-type variables. There are still lots of ways for create_function to bite you in the glutes.

The program argument to create_function is a string. This has two immediate knock-on effects [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, let&#8217;s say you managed to ensure that your program doesn&#8217;t <a href="http://finite.grimoire.ca/2010/01/lambduh/">exhaust the heap with anonymous functions</a>, and that you <a href="http://finite.grimoire.ca/2010/01/lambdurr/">understand the edge cases involved in function-type variables</a>. There are still lots of ways for <code>create_function</code> to bite you in the glutes.</p>
<p><span id="more-97"></span></p>
<p>The program argument to <code>create_function</code> is a string. This has two immediate knock-on effects that can really ruin your afternoon.</p>
<p>One: <code>create_function</code>&#8217;s arguments are strings, which means they&#8217;re invisible to any syntax highlighting, code navigation, or refactoring features you might be using to help make sense of the code. Any attempt to implement these properly for <code>create_function</code> will be heuristic at best.</p>
<p>Two: <code>create_function</code>&#8217;s arguments are strings, which means they interact with PHP&#8217;s string interpolation rules. The following program has a very subtle bug:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$axes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dimensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$x</span>&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">&quot;return <span style="color: #006699; font-weight: bold;">$x</span> * <span style="color: #006699; font-weight: bold;">$x</span>;&quot;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$axes</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Prints several errors and then nothing (FALSE); should print 1 (TRUE) */</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dimensions</span> <span style="color: #339933;">==</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Can you see it? Here&#8217;s the fixed version:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$axes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dimensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'$x'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'return $x * $x;'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$axes</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Prints 1 (TRUE) */</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dimensions</span> <span style="color: #339933;">==</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The PHP developers <em>do</em> think this is a problem: this is one of the motivations for the PHP closures RFC.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/lambdarrrgh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Did you think I wouldn&#8217;t recognize this compromise</title>
		<link>http://finite.grimoire.ca/2010/01/lambduh/</link>
		<comments>http://finite.grimoire.ca/2010/01/lambduh/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 21:00:07 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=94</guid>
		<description><![CDATA[On Thursday, commenter Grumqa asked how PHP deals with the slow accumulation of functions if you call create_function.
It doesn&#8217;t.


&#60;?php
for &#40;$i = 0; $i &#60; 100000; $i++&#41;
    create_function&#40;&#34;&#34;, &#34;return 0;&#34;&#41;;
?&#62;

This script fairly reliably kills the interpreter with the message &#8220;Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 40 bytes) [...]]]></description>
			<content:encoded><![CDATA[<p>On Thursday, commenter <a href="/2010/01/lambdurr/comment-page-1/#comment-9">Grumqa</a> asked how PHP deals with the slow accumulation of functions if you call <code>create_function</code>.</p>
<p>It doesn&#8217;t.</p>
<p><span id="more-94"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</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: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">100000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;return 0;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This script fairly reliably kills the interpreter with the message &#8220;Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 40 bytes) &#8230;&#8221;. You can postpone it by increasing PHP&#8217;s <a href="http://php.net/manual/en/ini.core.php#ini.memory-limit">memory limit</a>, but if you call <code>create_function</code> an arbitrary number of times, eventually you will murder the interpreter. The only safe way to use <code>create_function</code> is by ensuring you only call it a bounded number of times, and at that point, why not use a normal function and be done with it?</p>
<p>Naturally, <a href="http://bugs.php.net/bug.php?id=6333">the PHP team doesn&#8217;t think this is a problem</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/lambduh/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My purity you stole</title>
		<link>http://finite.grimoire.ca/2010/01/lambdurr/</link>
		<comments>http://finite.grimoire.ca/2010/01/lambdurr/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 00:26:51 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Core Library]]></category>
		<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=33</guid>
		<description><![CDATA[Yesterday, I wrote about how $f() variable-function syntax works in PHP. While it is pretty bad, it&#8217;s also the groundwork for understanding the ways in which create_function is terrible.
No, I mean besides taking a string full of code as one of its arguments.

Consider this contrived example:

1
2
3
4
5
6
7
8
9
10
11
&#60;?php
$input = array&#40;1, 2, 3&#41;;
&#160;
$monolithic_dimensions = array_map&#40;
    [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote about how <code>$f()</code> variable-function syntax works in PHP. While it is pretty bad, it&#8217;s also the groundwork for understanding the ways in which <a href="http://php.net/manual/en/function.create-function.php"><code>create_function</code></a> is terrible.</p>
<p>No, I mean besides taking a string full of code as one of its arguments.</p>
<p><span id="more-33"></span></p>
<p>Consider this contrived example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$monolithic_dimensions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$elem'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return $elem * $elem;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$input</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Prints Array([0] =&gt; 1, [1] =&gt; 4, [2] =&gt; 9) */</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$monolithic_dimensions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>On the face of it, this looks like a decent approximation of functional programming idiom. However, every time PHP executes <code>create_function</code>, it adds a new function to the global function table.</p>
<p>What&#8217;s really happening is a little easier to see if you look at <code>create_function</code>&#8217;s return value:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$square</span> <span style="color: #339933;">=</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$elem'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return $elem * $elem;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> <span style="color: #990000;">gettype</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$square</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* Prints string */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Prints
0  []
108	[l]
97	[a]
109	[m]
98	[b]
100	[d]
97	[a]
95	[_]
49	[1]
*/</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</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: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$square</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">print</span> <span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$square</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>[&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$square</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Yup. PHP is creating a function at runtime, with an &#8220;impossible&#8221; name (the zero byte isn&#8217;t allowed in <code>function</code> statements) to avoid collisions with user-defined functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/lambdurr/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Carry out my sentence</title>
		<link>http://finite.grimoire.ca/2010/01/no-opaque-function-types/</link>
		<comments>http://finite.grimoire.ca/2010/01/no-opaque-function-types/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 18:05:14 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Language Design]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=77</guid>
		<description><![CDATA[Early on in PHP&#8217;s life, its type system contained only strings, ints, reals, arrays, &#8220;resources,&#8221; and null. A number of features have been bodged in on top of this minimal system without really extending it. &#8220;Function pointer&#8221; functionality was one of the first.

The initial implementation simply took a string and looked it up in the [...]]]></description>
			<content:encoded><![CDATA[<p>Early on in PHP&#8217;s life, its type system contained only strings, ints, reals, arrays, &#8220;resources,&#8221; and null. A number of features have been bodged in on top of this minimal system without really extending it. &#8220;Function pointer&#8221; functionality was one of the first.</p>
<p><span id="more-77"></span></p>
<p>The initial implementation simply took a string and looked it up in the interpeter&#8217;s global function table, then invoked it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> add<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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</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: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$f</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;add&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$f</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* Prints 3 */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This probably got implemented in a couple of hours while Rasmus or one of his minions debated how it should work on IRC, and it shows:</p>
<ul>
<li>Because a &#8220;function&#8221; variable is any arbitrary string, there&#8217;s no way to statically validate them. Static analysis of PHP is a crap-shoot anyways, so this isn&#8217;t a big loss on its own, but overloading one type (<code>string</code>) to do something magic introduces more chances for programmer confusion, too.</li>
<li>Attempting to call an undefined function is a fatal error, as it should be. However, since it&#8217;s trivial to create a &#8220;function&#8221; variable that refers to an undefined function, this is a lot more headache-inducing than it needs to be. The presence of <a href="http://ca3.php.net/manual/en/function.function-exists.php"><code>function_exists</code></a> doesn&#8217;t help; it just makes generic code that much more verbose.</li>
<li>There are multiple code paths that result in a string being evaluated as a function name, and they don&#8217;t all have the same semantics. <a href="http://php.net/manual/en/function.call-user-func.php"><code>call_user_func</code></a> doesn&#8217;t abort your script if you pass it a bogus name, but you can&#8217;t tell if the called function might return <code>FALSE</code>, you&#8217;ll never know it because <code>call_user_func</code> returns that value to indicate errors.</li>
<li>Several early examples of how to use this feature showcased scripts that accepted function names straight out of user input, which is a security disaster on par with arbitrary code injection. Just because you only created HTML fields for certain names doesn&#8217;t mean malicious or bored users won&#8217;t start stuffing garbage in there.</li>
</ul>
<p>Unfortunately, when PHP 4 introduced a simple object model to PHP (necessitating a new variable type, for objects — but more on that later), the PHP team opted not to clean up this hole in the language. Instead, support for methods and static methods was bolted on top. Instead, the <code>$variable()</code> syntax was extended to accept <em>arrays</em> as well. All of the following variables <strong>might</strong> be callable:</p>
<dl>
<dt>Function names as strings.</dt>
<dd><code>$f = "add";</code></dd>
<dt>Instance methods, as an array.</dt>
<dd><code>$ops = new MathOperations(); $f = array($ops, "add");</code></dd>
<dt>Static methods, as an array.</dt>
<dd><code>$f = array("StaticMathOperations", "add");</code></dd>
<dt>Static methods, as a string.</dt>
<dd><code>$f = "StaticMathOperations::add";</code></dd>
</dl>
<p>There&#8217;s no real way of knowing if a value is <a href="http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback">callable</a> unless you try.</p>
<p>PHP 5.3&#8217;s <a href="http://wiki.php.net/rfc/closures">closure syntax</a> addresses some of this: closures are handled as a resource type within the PHP interpreter, finally. However, they come with their own <a href="http://wiki.php.net/rfc/closures/removal-of-this">weird implementation limitations</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/no-opaque-function-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I decided I was never coming down</title>
		<link>http://finite.grimoire.ca/2010/01/recursion-segfaults/</link>
		<comments>http://finite.grimoire.ca/2010/01/recursion-segfaults/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 19:04:44 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=70</guid>
		<description><![CDATA[I&#8217;m still sick, so you get another easy target today. It&#8217;s trivially easy to kill the PHP interpreter, dead, without invoking any extensions or unusual language features:

1
2
3
4
5
6
7
8
9
&#60;?php
function recurse&#40;$n&#41; &#123;
    if &#40;$n &#60;= 0&#41;
        return;
    return recurse&#40;$n - 1&#41;;
&#125;
&#160;
recurse&#40;100000&#41;; /* You may need [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still sick, so you get another easy target today. It&#8217;s trivially easy to kill the PHP interpreter, dead, without invoking any extensions or unusual language features:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> recurse<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> recurse<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
recurse<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* You may need to adjust this upwards. */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><span id="more-70"></span></p>
<p>This script dies unceremoniously (on Linux, with a segmentation fault) when the PHP interpreter runs off the end of its own stack. This is the kind of behaviour that gives security researchers nightmares, since <a href="http://en.wikipedia.org/wiki/Stack_buffer_overflow">segmentation faults and bus errors usually go hand in hand with code injection exploits</a>. It&#8217;s also a pain to diagnose: you need to break out <code>gdb</code>, because the PHP interpreter is too dead to explain what went wrong.</p>
<p>Other modern languages either monitor the depth of the stack and abort gracefully when it gets too large (Python, Ruby, Java, C#) or prevent large stacks from scribbling over sensitive parts of the program (Perl, Lisp, Haskell). The <a href="http://www.freshbooks.com/">FreshBooks</a> dev blog <a href="http://developers.freshbooks.com/blog/view/comparative_infinite_recursion/">illustrates</a> a few examples.</p>
<p>Predictably, the PHP developers <a href="http://bugs.php.net/bug.php?id=37612">don&#8217;t think this is a problem</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/recursion-segfaults/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I really don&#8217;t know what you mean</title>
		<link>http://finite.grimoire.ca/2010/01/indexing-expressions/</link>
		<comments>http://finite.grimoire.ca/2010/01/indexing-expressions/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 18:15:38 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Language Design]]></category>
		<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=26</guid>
		<description><![CDATA[It&#8217;s Monday, and I&#8217;m sick, so you get some low-hanging fruit today. The following snippet produces a syntax error in PHP:

1
2
3
4
5
6
7
&#60;?php
function monolithic_dimensions&#40;&#41; &#123;
    return array&#40;1, 4, 9&#41;;
&#125;
&#160;
print monolithic_dimensions&#40;&#41;&#91;0&#93;; // syntax error, unexpected '['
?&#62;


Of course, it&#8217;s trivial to work around:

1
2
3
4
5
6
&#60;?php
/* ... */
&#160;
$temp = monolithic_dimensions&#40;&#41;;
print $temp&#91;0&#93;; // Prints 1
?&#62;

In almost every other language with [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s Monday, and I&#8217;m sick, so you get some low-hanging fruit today. The following snippet produces a syntax error in PHP:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">function</span> monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">print</span> monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// syntax error, unexpected '['</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><span id="more-26"></span></p>
<p>Of course, it&#8217;s trivial to work around:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
&nbsp;
<span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Prints 1</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In almost every other language with array-indexing syntax, any expression can be indexed if it evaluates to an array. Consider Python:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> monolithic_dimensions<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>: 
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">9</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> monolithic_dimensions<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># Prints 1</span></pre></td></tr></table></div>

<p>Or Ruby:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> monolithic_dimensions
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">4</span>, <span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> monolithic_dimensions<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#008000; font-style:italic;"># Prints 1</span></pre></td></tr></table></div>

<p>Or even C:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> <span style="color: #993333;">const</span> <span style="color: #339933;">*</span>monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">static</span> <span style="color: #993333;">int</span> <span style="color: #993333;">const</span> dimensions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">9</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> dimensions<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> monolithic_dimensions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* Prints 1 */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Of course, since there&#8217;s no formal language spec for PHP, there&#8217;s no way to know whether this is by design or by accident.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/indexing-expressions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Let&#8217;s go dancing on the backs of the bruised</title>
		<link>http://finite.grimoire.ca/2010/01/raii-surprise/</link>
		<comments>http://finite.grimoire.ca/2010/01/raii-surprise/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 22:26:24 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Ugly Implementation]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=12</guid>
		<description><![CDATA[Remember the C++ idiom I mentioned yesterday? In PHP, it has some unique gotchas.

First, let&#8217;s look at a simple C++ RAII example, using objects that do nothing except log their own destruction:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include &#60;iostream&#62;
#include &#60;string&#62;
&#160;
class LogsDestruction &#123;
public:
  LogsDestruction&#40;std::string name&#41; &#123;
    this-&#62;name = name;
  &#125;
&#160;
  ~LogsDestruction&#40;&#41; &#123;
    std::cout [...]]]></description>
			<content:encoded><![CDATA[<p>Remember the <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization#C.2B.2B_example">C++ idiom</a> I mentioned <a href="/2010/01/no-finally/">yesterday</a>? In PHP, it has some unique gotchas.</p>
<p><span id="more-12"></span></p>
<p>First, let&#8217;s look at a simple C++ RAII example, using objects that do nothing except log their own destruction:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">class</span> LogsDestruction <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  LogsDestruction<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> name<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>name <span style="color: #000080;">=</span> name<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  ~LogsDestruction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>name <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
  std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> name<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  LogsDestruction a<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;A&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  LogsDestruction b<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;B&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>If you build and run this, you can see that objects <code>a</code> and <code>b</code> are destroyed in last-to-first order:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #007800;">CXXFLAGS</span>=<span style="color: #ff0000;">'-W -Wall -pedantic -std=c++98'</span> <span style="color: #c20cb9; font-weight: bold;">make</span> raii
<span style="color: #c20cb9; font-weight: bold;">g++</span> <span style="color: #660033;">-W</span> <span style="color: #660033;">-Wall</span> <span style="color: #660033;">-pedantic</span> <span style="color: #660033;">-std</span>=<span style="color: #c20cb9; font-weight: bold;">c++</span><span style="color: #000000;">98</span>    raii.cpp   <span style="color: #660033;">-o</span> raii
$ .<span style="color: #000000; font-weight: bold;">/</span>raii 
B
A</pre></div></div>

<p>This order is guaranteed by the C++ language spec.</p>
<p>Now let&#8217;s see what happens with the equivalent PHP program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> LogsDestruction <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	     <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	     <span style="color: #b1b100;">print</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span>
             <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> raii_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LogsDestruction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LogsDestruction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;B&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
raii_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Running this from either a web request or the PHP command-line interpreter gives:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ php raii.php 
A
B</pre></div></div>

<p>Surprise!</p>
<p>This is guaranteed to bite you if you have multiple resources that need guaranteed cleanup in the same block. The only workaround available in the language is to do at most one cleanup <em>per function</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #000000; font-weight: bold;">function</span> raii_example_inner<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LogsDestruction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;B&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> raii_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LogsDestruction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  raii_example_inner<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
raii_example<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/raii-surprise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;d rather die than give you control</title>
		<link>http://finite.grimoire.ca/2010/01/no-finally/</link>
		<comments>http://finite.grimoire.ca/2010/01/no-finally/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 21:52:48 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Language Design]]></category>

		<guid isPermaLink="false">http://finite.grimoire.ca/?p=9</guid>
		<description><![CDATA[Most modern languages have a construct that allows code to run at the end of a block regardless of how flow leaves a block. This allows code to reliably close resources, perform rollbacks, and otherwise maintain invariants relatively easily, even in the presence of exceptions.

Exceptions were added to PHP in PHP5, allowing blocks of PHP [...]]]></description>
			<content:encoded><![CDATA[<p>Most modern languages have a construct that allows code to run at the end of a block regardless of how flow leaves a block. This allows code to reliably close resources, perform rollbacks, and otherwise maintain invariants relatively easily, even in the presence of exceptions.</p>
<p><span id="more-9"></span></p>
<p><a href="http://php.net/manual/en/language.exceptions.php">Exceptions</a> were added to PHP in PHP5, allowing blocks of PHP to terminate abruptly. Constructs like</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$h</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config.ini&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> some_local_function<span style="color: #009900;">&#40;</span><span style="color: #000088;">$h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>do not guarantee that <code>$h</code> gets closed reliably: if <code>some_local_function</code> throws an exception, flow never reaches line 4 and never closes the handle. For most things, this isn&#8217;t actually as big a deal as it would be in other languages: connections, file handles, and other resources are all closed at the end of each request anyways.</p>
<p>However, sometimes you care what order resources are closed in, or you want to perform some cleanup actions with a resource before closing it, and then the lack of a <code>finally</code> clause hurts. One PHP project I&#8217;ve worked on talks to an <a href="http://www.amqp.org/">AMQP</a> <a href="http://www.rabbitmq.com/">broker</a>; AMQP has a defined connection teardown process that&#8217;s necessary to verify that published messages are actually being processed. The ideal way to handle that would look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$amqp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AMQPConnection<span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">/*...*/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* Publish messages using $amqp */</span>
<span style="color: #009900;">&#125;</span> finally <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$amqp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Without finally (or with blocks/using statements, for the Python and C# programmers out there), the obvious solution is much uglier:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$amqp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AMQPConnection<span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">/*...*/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
try <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* Publish messages using $amqp */</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$amqp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    throw <span style="color: #000088;">$e</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$amqp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Note the near-mandatory duplication.</p>
<p>Of course, it&#8217;s possible to work around the lack of automatic resource management syntax by using <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">an idiom</a> from, of all things, C++:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> Closing <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$closeable</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$closable</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">closeable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$closeable</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">closeable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #000088;">$amqp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AMQPConnection<span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">/*...*/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$close_amqp</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Closing<span style="color: #009900;">&#40;</span><span style="color: #000088;">$amqp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* Publish messages using $amqp */</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>However, this relies on understanding the interactions between scope, object lifespans, and PHP&#8217;s optional garbage collector, which are <a href="http://php.net/manual/en/language.variables.scope.php">non</a>-<a href="http://tuxradar.com/practicalphp/18/1/10">trivial</a>.</p>
<p>The PHP development team <a href="http://bugs.php.net/bug.php?id=32100">doesn&#8217;t think this is a problem</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/no-finally/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

