<?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 &#187; Language Design</title>
	<atom:link href="http://finite.grimoire.ca/category/language-design/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>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>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 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>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>

