<?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>Digital Ambulation &#187; php</title>
	<atom:link href="http://www.alanbriolat.co.uk/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alanbriolat.co.uk</link>
	<description>Life, programming and general geekery</description>
	<lastBuildDate>Mon, 09 Jan 2012 12:07:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>When Dynamic Typing Goes Wrong</title>
		<link>http://www.alanbriolat.co.uk/2009/07/when-dynamic-typing-goes-wrong/</link>
		<comments>http://www.alanbriolat.co.uk/2009/07/when-dynamic-typing-goes-wrong/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 19:01:32 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.alanbriolat.co.uk/?p=388</guid>
		<description><![CDATA[Yesterday I found out first-hand how using a dynamically typed language can get you into trouble in unexpected ways whilst writing unit tests for CSF (my PHP framework). Take a look at this code&#8212;what do you think it should do? $foo = 'bar'; var_dump&#40;isset&#40;$foo&#91;'xyz'&#93;&#41;&#41;; var_dump&#40;$foo&#91;'xyz'&#93;&#41;; Most people would assume that indexing a string on another [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I found out first-hand how using a dynamically typed language can get you into trouble in unexpected ways whilst writing unit tests for <a href="http://codescape.net/csf/">CSF</a> (my PHP framework).</p>
<p><span id="more-388"></span></p>
<p>Take a look at this code&#8212;what do you think it should do?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'xyz'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</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;">$foo</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'xyz'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Most people would assume that indexing a string on another string is nonsensical, and they would be right&#8212;in PHP you can only index a string with an integer.  The logical extension would be that isset($foo['xyz']) would be false and/or any attempt to index using a string would raise an error.  And that is where they would be wrong.  In fact, isset($foo['xyz']) is true, and $foo['xyz'] is &#8220;b&#8221;.</p>
<p>PHP is <a href="http://en.wikipedia.org/wiki/Dynamic_typing#Dynamic_typing">dynamically typed</a>, which generally means there is a lot of implicit casting between types going on.  In this case, while indexing a string can only be done with an integer, there is an implicit cast from a string to an integer, so this cast is performed and the result used as the index.  However, the usual way of converting from a string to an integer is to use digits (0&#8211;9) up until the first non-digit character (after taking the minus sign into account if there is one).  This means the integer value of &#8216;xyz&#8217; is 0, since there are no digits to add up.</p>
<p>When explained, this may seem like sensible behaviour, but let&#8217;s think about the effect this has when coding and debugging: if you accidentally index a string with another string somewhere, you won&#8217;t know about it straight away.  Instead, you&#8217;ll just be getting &#8220;wrong&#8221; values and scratching your head over where they could be coming from.  I&#8217;m of the opinion that if you can only index a string on an integer, then indexing it on a string should be at the very least a warning.  Silently doing something completely different isn&#8217;t very helpful.</p>
<p>And so that this doesn&#8217;t seem like a nonsensical contrived example, here&#8217;s a reduced version of the code that raised the issue:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$path</span><span style="color: #339933;">,</span> <span style="color: #000088;">$default</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$array</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$p</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: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</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: #990000;">func_num_args</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
                <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ItemNotFound<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">else</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$default</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$item</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What this function does is traverse a multi-dimensional array with a &#8220;key path&#8221;, i.e. &#8220;key1.key2&#8243; gets $array['key1']['key2'].  Since isset() is the most common way people check the existence of an array, this is what I used here.  However, things break down in this example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foo'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> get_item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foo.xyz'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The expected behaviour is that the exception should be thrown, since $array['foo'] is not an array with a &#8220;xyz&#8221; key, but the issue described above instead leads this to return &#8220;b&#8221;.</p>
<p>In this case, there is a way around it: what I really should have been using is the following check:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This has more interesting consequences&#8212;it means that a very very large number of people are using &#8220;isset&#8221; where they mean &#8220;is_array and array_key_exists&#8221;.  As for me, I just fixed a bug in my code exposed by a unit test, so I&#8217;m happy (even if a little bewildered by PHP).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alanbriolat.co.uk/2009/07/when-dynamic-typing-goes-wrong/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tpl &#8211; A tiny PHP template engine</title>
		<link>http://www.alanbriolat.co.uk/2009/04/tpl-a-tiny-php-template-engine/</link>
		<comments>http://www.alanbriolat.co.uk/2009/04/tpl-a-tiny-php-template-engine/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 16:03:44 +0000</pubDate>
		<dc:creator>Alan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tpl]]></category>

		<guid isPermaLink="false">http://www.alanbriolat.co.uk/?p=303</guid>
		<description><![CDATA[While developing CodeScape Framework I came up with a simple template engine that instead of implementing it&#8217;s own language just provided some structure-related hooks, and I&#8217;ve now got around to separating out and releasing Tpl, a simple, self-contained, hierarchical template engine for PHP. Tpl&#8217;s role is centred around template structure, not content&#8212;it&#8217;s not a huge [...]]]></description>
			<content:encoded><![CDATA[<p>While developing <a href="http://codescape.net/csf/">CodeScape Framework</a> I came up with a simple template engine that instead of implementing it&#8217;s own language just provided some structure-related hooks, and I&#8217;ve now got around to separating out and releasing <a href="http://codescape.net/tpl/">Tpl</a>, a simple, self-contained, hierarchical template engine for PHP.</p>
<p>Tpl&#8217;s role is centred around template structure, not content&#8212;it&#8217;s not a huge library of helper functions for creating links, displaying images, etc.  Like any template engine, the aim is to separate presentation logic from application logic, but it doesn&#8217;t attempt this by creating a template language.  PHP is <a href="http://php.net/manual/en/history.php.php">designed</a> to be interleaved with &#8220;static&#8221; output, which means it&#8217;s already a template language.  Trying to replace it for the purpose of templating rarely adds anything useful, incurs hefty performance overheads (which you then have to mitigate with caching) and usually is more restrictive than PHP.  To me, that doesn&#8217;t seem like a sensible application of effort.  Instead Tpl provides its functionality in the form of hooks&#8212;functions which are called from within the template to specify the structure.  Instead of PHP being replaced, it is extended.</p>
<p>The main motivating factor for creating Tpl is that all PHP template engines seem to be centred around <em>inclusion</em> rather than <em>inheritance</em>, something I only noticed after spending time working with <a href="http://www.djangoproject.com/">Django</a>.  For example, a typical PHP template might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- header.php --&gt;
&lt;html&gt;
    &lt;head&gt;&lt;title&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$title</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/title&gt;&lt;/head&gt;
    &lt;body&gt;
        &lt;div id=&quot;header&quot;&gt;Website Name&lt;/div&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> footer<span style="color: #339933;">.</span>php <span style="color: #339933;">--&gt;</span>
        <span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;footer&quot;</span><span style="color: #339933;">&gt;</span>Some footer text<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- content.php --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'header.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;div id=&quot;content&quot;&gt;
    Some content
&lt;/div&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'footer.php'</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>The main problem with this is that it bears no real relation to the logical structure of the page, just the order in which stuff should appear in the HTML.  If you modify something in the header, you need to make sure you&#8217;ve updated the footer too.  The equivalent Django template would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{# base.html #}
&lt;html&gt;
    &lt;head&gt;&lt;title&gt;{{ title }}&lt;/title&gt;&lt;/head&gt;
    &lt;body&gt;
        &lt;div id=&quot;header&quot;&gt;Website Name&lt;/div&gt;
&nbsp;
        {% block content %}{% endblock %}
&nbsp;
        &lt;div id=&quot;footer&quot;&gt;Some footer text&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{# content.html #}
{% extends &quot;base.html&quot; %}
&nbsp;
{% block content %}
&lt;div id=&quot;content&quot;&gt;
    Some content
&lt;/div&gt;
{% endblock %}</pre></div></div>

<p>The point to note is that the structure is coherent and in one place, and the parts that change are defined as logical blocks which can be replaced.  This is the method I&#8217;ve adopted in Tpl&#8212;the equivalent template would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- base.php --&gt;
&lt;html&gt;
    &lt;head&gt;&lt;title&gt;<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$C</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/title&gt;&lt;/head&gt;
    &lt;body&gt;
        &lt;div id=&quot;header&quot;&gt;Website Name&lt;/div&gt;
&nbsp;
        <span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">block</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">endblock</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>
&nbsp;
        &lt;div id=&quot;footer&quot;&gt;Some footer text&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- content.php --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">inherit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'base.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">block</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;div id=&quot;content&quot;&gt;
    Some content
&lt;/div&gt;
<span style="color: #000000; font-weight: bold;">&lt;?</span> Tpl<span style="color: #339933;">::</span><span style="color: #004000;">endblock</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></div></div>

<p>More information about Tpl and how to download it can be found at the <a href="http://codescape.net/tpl/">Tpl project page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alanbriolat.co.uk/2009/04/tpl-a-tiny-php-template-engine/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

