<?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; Core Library</title>
	<atom:link href="http://finite.grimoire.ca/category/core-library/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>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>God Money, I&#8217;ll do anything for you</title>
		<link>http://finite.grimoire.ca/2010/01/fread-suck/</link>
		<comments>http://finite.grimoire.ca/2010/01/fread-suck/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 20:40:33 +0000</pubDate>
		<dc:creator>Owen Jacobson</dc:creator>
				<category><![CDATA[Core Library]]></category>

		<guid isPermaLink="false">http://picatrix/?p=1</guid>
		<description><![CDATA[The loop

1
2
3
4
5
&#60;?php
while &#40;$read &#60; $n &#38;&#38; &#40;false !== &#40;$buf = fread&#40;$this-&#62;sock, $n - $read&#41;&#41;&#41;&#41; &#123;
    /* ... */
&#125;
?&#62;

contains a subtle bug. Go ahead, read the relevant function documentation and see if you can spot it.

PHP&#8217;s fread function directly maps the C library&#8217;s fread function. As TBlue explains, the C fread function returns [...]]]></description>
			<content:encoded><![CDATA[<p>The loop</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: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$read</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$buf</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sock</span><span style="color: #339933;">,</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$read</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>contains a subtle bug. Go ahead, read the <a href="http://php.net/manual/en/function.fread.php">relevant function documentation</a> and see if you can spot it.</p>
<p><span id="more-1"></span></p>
<p>PHP&#8217;s <code>fread</code> function directly maps the C library&#8217;s <code>fread</code> function. As <a href="http://www.php.net/manual/en/function.fread.php#95413">TBlue</a> explains, the C <code>fread</code> function returns <code>0</code> when reading at the end of a stream. PHP does the simplest possible thing with this and returns an empty string; to determine that the stream is at end of file, programs must check with <code>feof</code> as well.</p>
<p>Most modern languages realize that explicitly checking for errors is a fairly error-prone convention. Java&#8217;s <a href="http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#read(byte[])">InputStream.read(byte[])</a> methods return -1 on EOF (and only on EOF) and raise exceptions on errors. Python&#8217;s <a href="http://docs.python.org/library/stdtypes.html#file-objects">File-like objects</a> convention supports mechanisms other than raw byte reads (primarily, iteration) that make stopping at end of file implicit, on top of only returning an empty string at end of input (as with Java, Python raises exceptions when a stream read fails). Not PHP, though! In PHP, the loop above has to look like</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: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$read</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sock</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$buf</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sock</span><span style="color: #339933;">,</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">-</span> 
<span style="color: #000088;">$read</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Encountered in the wild: <a href="http://code.google.com/p/php-amqplib/issues/detail?id=12">php-amqplib bug #12</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://finite.grimoire.ca/2010/01/fread-suck/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

