<?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>Alan's blog &#187; php</title>
	<atom:link href="http://www.alandix.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alandix.com/blog</link>
	<description>just starting ...</description>
	<lastBuildDate>Wed, 08 Feb 2012 18:53:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>online PHP 5.3 syntax checker</title>
		<link>http://www.alandix.com/blog/2010/08/13/online-php-5-3-syntax-checker/</link>
		<comments>http://www.alandix.com/blog/2010/08/13/online-php-5-3-syntax-checker/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 18:28:34 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[academic]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[syntax checker]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?p=271</guid>
		<description><![CDATA[My online PHP syntax checker (mentioned in earlier post) now has option to check PHP5.3.  There are still options to check PHP 4 and PHP 5.2, which should cover major syntax variations.]]></description>
			<content:encoded><![CDATA[<p>My online PHP syntax checker (mentioned in <a href="http://www.alandix.com/blog/2010/06/09/php-syntax-checker-updated/" target="_blank">earlier post</a>) now has option to <a href="http://www.meandeviation.com/tutorials/learnphp/php-syntax-check/v5-3/" target="_blank">check PHP5.3</a>.  There are still options to check <a href="http://www.meandeviation.com/tutorials/learnphp/php-syntax-check/v4/" target="_blank">PHP 4</a> and <a href="http://www.meandeviation.com/tutorials/learnphp/php-syntax-check/v5-2/" target="_blank">PHP 5.2</a>, which should cover major syntax variations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alandix.com/blog/2010/08/13/online-php-5-3-syntax-checker/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Apache: pretty URLs and rewrite loops</title>
		<link>http://www.alandix.com/blog/2010/07/13/apache-pretty-urls-and-rewrite-loops/</link>
		<comments>http://www.alandix.com/blog/2010/07/13/apache-pretty-urls-and-rewrite-loops/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 09:30:02 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[academic]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rewriterule]]></category>
		<category><![CDATA[snip!t]]></category>
		<category><![CDATA[techie]]></category>
		<category><![CDATA[vfridge]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?p=265</guid>
		<description><![CDATA[[another techie post - a problem I had and can see that other people have had too] It is common in various web frameworks to pass pretty much everything through a central script using Apache .htaccess file and mod_rewrite.  For example enabling permalinks in a WordPress blog generates an .htaccess file like this: RewriteEngine On [...]]]></description>
			<content:encoded><![CDATA[<p>[another techie post - a problem I had and can see that other people have had too]</p>
<p>It is common in various web frameworks to pass pretty much everything through a central script using Apache .htaccess file and mod_rewrite.  For example enabling <a href="http://codex.wordpress.org/Using_Permalinks" target="_blank">permalinks</a> in a WordPress blog generates an .htaccess file like this:</p>
<pre class="brush: plain; title: ;">
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</pre>
<p>I use similar patterns for various sites such as <a href="http://www.vfridge.com/" target="_blank">vfridge</a> (see recent post &#8220;<a href="http://www.alandix.com/blog/2010/06/11/phoenix-rises-vfridge-online-again/" target="_blank">Phoenix rises</a>&#8220;) and <a href="http://www.snipit.org/" target="_blank">Snip!t</a>.  For Snip!t however I was using not a local <a href="http://httpd.apache.org/docs/2.2/howto/htaccess.html" target="_blank">.htaccess</a> file, but an <a href="http://httpd.apache.org/docs/2.1/mod/mod_alias.html" target="_blank">AliasMatch</a> in httpd.conf, which meant I needed to ask Fiona every time I needed to do a change (as I can never remember the root passwords!).  It seemed easier (even if slightly less efficient) to move this to a local .htaccess file:</p>
<pre class="brush: plain; title: ;">
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ code/top.php/$1 [L]
</pre>
<p>The intention is to map &#8220;/an/example?args&#8221; into &#8220;/code/top.php/an/example?args&#8221;.</p>
<p>Unfortunately this resulted in a &#8220;500 internal server error&#8221; page and in the Apache error log messages saying there were too many internal redirects.  This seems to be a common problem reported in forums (see <a href="http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html#comment-104819" target="_blank" title="mathguy's comment on: .Htaccess rewrites, Mod_Rewrite Tricks and Tips">here</a>, <a href="http://drupal.org/node/60426" target="_blank" title=".htaccess rewrite loop">here</a> and <a href="http://stackoverflow.com/questions/1027801/500-server-error-infinite-rewrite-loop-in-apache-2-2-x-with-mod-vhost-alias-vir" target="_blank" title="500 Server Error: Infinite rewrite loop in Apache 2.2.x with mod_vhost_alias, VirtualDocumentRoot, and mod_rewrite for routing / “pretty” URLs">here</a>).  The reason for this is that .htaccess files are encountered very late in Apache&#8217;s processing and so anything rewritten by the rules gets thrown back into Apache&#8217;s processing almost as if they were a fresh request.  While the &#8220;[L]&#8220;(last)  flags says &#8220;don&#8217;t execute any more rules&#8221;, this means &#8220;no more rules on this pass&#8221;, but when Apache gets back to the .htaccess in the fresh round the rule gets encountered again and again leading to an infinite loop &#8220;/code/top/php/code/top.php/&#8230;/code/top.php/an/example?args&#8221;.</p>
<p>Happily, mod_rewrite thought of this and there is an additional &#8220;[NS]&#8221; (nosubreq) flag that says &#8220;only use this rule on the first pass&#8221;.  The mod_rewrite documentation for RewriteRule in Apache <a href="http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule" target="_blank" title="RewriteRule: Apache 1.3">1.3</a>, <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html" target="_blank" title="RewriteRule: Apache 2.0">2.0</a> and <a href="http://httpd.apache.org/docs/2.3/rewrite/flags.html#flag_ns" target="_blank" title="RewriteRule Flags: Apache 2.3">2.3</a> says:</p>
<blockquote><p>Use the following rule for your decision: whenever you           prefix some URLs with CGI-scripts to force them to be           processed by the CGI-script, the chance is high that you           will run into problems (or even overhead) on           sub-requests. In these cases, use this flag.</p></blockquote>
<p>I duly added the flag:</p>
<pre class="brush: plain; title: ;">
RewriteRule ^(.*)$ code/top.php/$1 [L,NS]
</pre>
<p>This <em>should</em> work, but doesn&#8217;t.  I&#8217;m not sure why except that the <a href="http://httpd.apache.org/docs/2.2/rewrite/rewrite_flags.html#flag_ns" target="_blank" title="RewriteRule Flags: Apache 2.2">Apache 2.2 documentation for NS|nosubreq</a> reads:</p>
<blockquote><p><span style="color: #000080;"><strong><a name="flag_ns">NS|nosubreq</a></strong></span></p>
<p>Use of the [NS] flag prevents the rule from being used on subrequests. For example, a page which is included using an SSI (Server Side Include) is a subrequest, and you may want to avoid rewrites happening on those subrequests.</p>
<p>Images, javascript files, or css files, loaded as part of an HTML page, are not subrequests &#8211; the browser requests them as separate HTTP requests.</p></blockquote>
<p>This is identical to the documentation for 1.3, 2.0 and 2.3 except that quote about &#8220;URLs with CGI-scripts&#8221; is singularly missing.  I can&#8217;t find anything that says so, but my guess is that there was some bug (feature?) introduced 2.2 that is being fixed in 2.3.</p>
<p>WordPress is immune from the infinite loop as the directive &#8220;<code>RewriteCond %{REQUEST_FILENAME} !-f</code>&#8221; says &#8220;if the file exists use that without rewriting&#8221;.  As &#8220;index.php&#8221; is a file, the rule does not rewrite a second time.  However, the layout of my files meant that I sometimes have an actual file in the pseudo location (e.g. /an/example really exists).  I could have reorganised the complete directory structure &#8230; but then I would have been still fixing all the broken links now!</p>
<p>Instead I simply added an explicit &#8220;please don&#8217;t rewrite my top.php script&#8221; condition:</p>
<pre class="brush: plain; title: ;">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !^/code/top.php/.*
RewriteRule ^(.*)$ code/top.php/$1 [L,NS]
</pre>
<p>I suspect that this will be unnecessary when Apache upgrades to 2.3, but for now &#8230; it works <img src='http://www.alandix.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.alandix.com/blog/2010/07/13/apache-pretty-urls-and-rewrite-loops/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>fix for toString error in PHPUnit</title>
		<link>http://www.alandix.com/blog/2010/07/09/fix-for-tostring-error-in-phpunit/</link>
		<comments>http://www.alandix.com/blog/2010/07/09/fix-for-tostring-error-in-phpunit/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 19:54:22 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[academic]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[techie]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?p=263</guid>
		<description><![CDATA[I was struggling to get PHPUnit to run under PHP 5.2.9. I&#8217;ve only used PHPUnit a little, so may have simply got something wrong, but I kept getting the error: Catchable fatal error: Object of class AbcTest could not be converted to string in {dir}/PHPUnit/Framework/TestFailure.php on line 98 The error happens in the PHPUnit_Framework_TestFailure::toString method, [...]]]></description>
			<content:encoded><![CDATA[<p>I was struggling to get <a href="http://www.phpunit.de/" target="_blank">PHPUnit</a> to run under PHP 5.2.9. I&#8217;ve only used PHPUnit a little, so may have simply got something wrong, but I kept getting the error:</p>
<blockquote><p><code><strong>Catchable fatal error</strong>:  Object of class AbcTest could not be  converted to string in <strong><em>{dir}</em>/PHPUnit/Framework/TestFailure.php</strong> on line <strong>98</strong></code></p></blockquote>
<p>The error happens in the <code>PHPUnit_Framework_TestFailure::toString</code> method, which tries to implicitly convert a test case to a string.</p>
<p>The class <code>AbcTest</code> is my test case, which it is trying to display following a test failure.  PHPUnit test cases all extend <code>PHPUnit_Framework_TestCase</code> and while this has a <code>toString</code> method it does not have the &#8216;<a href="http://php.net/manual/en/language.oop5.magic.php" target="_blank">magic method</a>&#8216; <code>__toString</code> required by PHP 5.2 onwards.</p>
<p>To fix the problem I simply added the following method to the class <code>PHPUnit_Framework_TestCase</code> in <code>PHPUnit/Framework/TestCase.php</code> .</p>
<pre class="brush: php; title: ;">
public function __toString()
 {
 return $this-&gt;toString();
 }
</pre>
<p>I am using PHPUnit 3.4.9, but peeking at 3.5.0beta it looks the same.  I&#8217;m guessing the <code>PHPUnit_Framework_TestFailure::toString</code> method is not used much so has got missed since the change to PHP 5.2.x.</p>
<p><a href="http://sebastian-bergmann.de/archives/876-PHPUnit-Development-Moved-to-GitHub.html" target="_blank">PHPUnit is now in GITHub</a> so I really ought to work out how to submit corrections to that &#8230; but another day I think.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alandix.com/blog/2010/07/09/fix-for-tostring-error-in-phpunit/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Phoenix rises &#8211; vfridge online again</title>
		<link>http://www.alandix.com/blog/2010/06/11/phoenix-rises-vfridge-online-again/</link>
		<comments>http://www.alandix.com/blog/2010/06/11/phoenix-rises-vfridge-online-again/#comments</comments>
		<pubDate>Fri, 11 Jun 2010 19:47:19 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[academic]]></category>
		<category><![CDATA[HCI and usability]]></category>
		<category><![CDATA[personal]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[aqtive]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Fiona]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[part-time]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[social networking]]></category>
		<category><![CDATA[vfridge]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[web2.0]]></category>
		<category><![CDATA[websharer]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?p=260</guid>
		<description><![CDATA[vfridge is back! I mentioned &#8216;Project Phoenix&#8217; in my last previous post, and this was it &#8211; getting vfridge up and running again. Ten years ago I was part of a dot.com company aQtive1 with Russell Beale, Andy Wood and others.  Just before it folded in the aftermath of the dot.com crash, aQtive spawned a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.alandix.com/images/vfridge-splash.png"><img class="alignright" style="margin: 3px; border: 0pt none;" title="vfridge home page" src="http://www.alandix.com/images/vfridge-splash.png" alt="" width="214" height="154" /></a>vfridge is back!</p>
<p>I mentioned &#8216;Project Phoenix&#8217; in my <a href="http://www.alandix.com/blog/2010/06/09/php-syntax-checker-updated/" target="_blank">last previous post</a>, and this was it &#8211; getting <a href="http://www.vfridge.com/" target="_blank">vfridge</a> up and running again.</p>
<p>Ten years ago I was part of a dot.com company aQtive<sup><a href="#footnote-1-260" id="footnote-link-1-260" title="See the footnote.">1</a></sup> with <a href="http://www.cs.bham.ac.uk/~rxb/" target="_blank">Russell Beale</a>, Andy Wood and others.  Just before it folded in the aftermath of the dot.com crash, aQtive spawned a small spin-off vfridge.com.  The <em>virtual fridge</em> was a social networking web site before the term existed, and while vfridge the company went the way of most dot.coms, for some time after I kept the vfridge web site running on Fiona&#8217;s servers until it gradually &#8216;decayed&#8217; partly due to Javascript/DOM changes and partly due to Java&#8217;s interactions with mysql becoming unstable (note very, very old Java code!).  But it is now <a href="http://www.vfridge.com/phoenix/f" target="_blank">back online</a> <img src='http://www.alandix.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><img class="alignright" title="vfridge screenshot full view" src="http://www.vfridge.com//using/screenshots/fullview-25.gif" alt="" width="181" height="139" />The core idea of vfridge is placing small notes, photos and &#8216;magnets&#8217; in a shareable web area that can be moved around and arranged like you might with notes held by magnets to a fridge door.</p>
<p>Underlying vfridge was what we called the <a href="http://www.hiraeth.com/alan/ebulletin/websharer/" target="_blank">websharer vision</a>, which looked towards a web of user-generated content.  Now this is passé, but at the time  was directly counter to accepted wisdom and looking back seem prescient &#8211; remember this was written in 1999:</p>
<blockquote><p><em>Although everyone isn&#8217;t a web developer, it is likely that soon everyone will become an Internet communicator — email, PC-voice-comms, bulletin boards, etc. For some this will be via a PC, for others using a web-phone, set-top box or Internet-enabled games console.<br />
&#8230;</em></p>
<p><em>The web/Internet is not just a medium for publishing, but a potential shared place.</em></p>
<p><em>Everyone may be a </em>web sharer<em> — not a publisher of formal public &#8216;content&#8217;, but personal or semi-private sharing of informal &#8216;bits and pieces&#8217; with family, friends, local community and virtual communities such as fan clubs.</em></p>
<p><em>This is not just a future for the cognoscenti, but for anyone who chats in the pub or wants to show granny in Scunthorpe the baby&#8217;s first photos.</em></p></blockquote>
<p><img class="alignright" title="vfridge closeup" src="http://www.vfridge.com/research/candf3/images/notes-1.gif" alt="" width="209" height="229" />Just over a year ago I thought it would be good to write a retrospective about vfridge in the light of the social networking revolution.  We did a poster &#8220;<a href="http://www.vfridge.com/research/candf3/candf.html" target="_blank">Designing a virtual fridge</a>&#8221; about vfridge years ago at a <a href="http://www-users.york.ac.uk/%7Eam1/candf3.html" target="_blank">Computers and Fun workshop</a>, but have never written at length abut its design and development.  In particular it would be good to analyse the reasons, technical, social and commercial, why it did not &#8216;take off&#8217; the time.  However, it is hard to do write about it without good screen shots, and could I find any? (Although now I have)  So I thought it would be good to revive it and now you can <a href="http://www.vfridge.com/phoenix/f" target="_blank">try it out again</a>. I started with a few days effort last year at Christmas and Easter time (leisure activity), but now over the last week have at last used the fact that I have <a href="http://www.alandix.com/blog/2010/02/02/now-part-time/" target="_blank">half my time unpaid</a> and so free for my own activities &#8230; and it is done <img src='http://www.alandix.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The original vfridge was implemented using Java Servlets, but I have rebuilt it in PHP.  While the original development took over a year (starting down in Coornwall while on holiday watching the solar eclipse), this re-build took about 10 days effort, although of course with no design decisions needed.  The reason it took so much development back then is one of the things I want to consider when I write the retrospective.</p>
<p>As far as possible the actual behaviour and design is exactly as it was back in 2000 &#8230; and yes it does feel clunky, with lots of refreshing (remember no AJAX or web2.0 in those days) and of course loads of frames!  In fact there is a little cleverness that allowed some client-end processing pre-AJAX<sup><a href="#footnote-2-260" id="footnote-link-2-260" title="See the footnote.">2</a></sup>.    Also the new implementation uses the same templates as the original one, although the expansion engine had to be rewritten in PHP.  In fact this template engine was one of our most re-used bits of Java code, although now of course many alternatives.  Maybe I will return to a discussion of that in another post.</p>
<p style="text-align: center;"><img class="alignnone" style="border: 1px solid black; margin-left: 20px; margin-right: 20px;" title="vfridge the WAP interface" src="http://www.alandix.com/images/iPhone-vfridge33.png" alt="" width="107" height="160" /> <img class="alignnone" style="margin-left: 20px; margin-right: 20px;" title="old WAP phone" src="http://www.alandix.com/images/phone-med50.jpg" alt="" width="128" height="147" /></p>
<p>I have even resurrected the <a href="http://www.vfridge.com/phoenix/f?op=wap" target="_blank">old mobile interface</a>.  Yes there were WAP phones even in 2000, albeit with tiny green and black screens.  I still recall the excitement I felt the first time I entered a note on the phone and saw it appear on a web page <img src='http://www.alandix.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   However, this was one place I had to extensively edit the page templates as nothing seems to process <a href="http://en.wikipedia.org/wiki/Wireless_Markup_Language" target="_blank">WML</a> anymore, so the WML had to be converted to plain-text-ish HTML, as close as possible to those old phones!  Looks rather odd on the iPhone :-/</p>
<p>So, if you were one of those who had an account back in 2000 (<a href="http://www.idemployee.id.tue.nl/p.markopoulos/default.htm" target="_blank">Panos Markopoulos</a> used it to share his baby photos <img src='http://www.alandix.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ), then everything is still there just as you left it!</p>
<p>If not, then you can register now and play.</p>
<br /><ol class="footnotes"><li id="footnote-1-260">The old aQtive website is still viewable at <a href="http://www.aqtive.org/" target="_blank">aqtive.org</a>, but don&#8217;t try to install onCue, it was developed in the days of Windows NT.  [<a href="#footnote-link-1-260">back</a>]</li><li id="footnote-2-260">One trick used the fact that you can get Javascript to pre-load images.  When the front-end Javascript code wanted to send information back to the server it preloaded an image URL that was really just to activate a back-end script.  The frames  used a change-propagation system, so that only those frames that were dependent on particular user actions were refreshed.  All of this is preserved in the current system, peek at the Javascript on the pages.    Maybe I&#8217;ll write about the details of these another time.  [<a href="#footnote-link-2-260">back</a>]</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.alandix.com/blog/2010/06/11/phoenix-rises-vfridge-online-again/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP syntax checker updated</title>
		<link>http://www.alandix.com/blog/2010/06/09/php-syntax-checker-updated/</link>
		<comments>http://www.alandix.com/blog/2010/06/09/php-syntax-checker-updated/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 16:05:39 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[academic]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?p=259</guid>
		<description><![CDATA[Took a quick break today from Project Phoenix1. I&#8217;ve had a PHP syntax checker on meandeviation for several years, but only checking PHP 4 as that is what is running on the server.  However, I had an email asking about PHP 5 , so now there is a PHP 5 version too The syntax checker [...]]]></description>
			<content:encoded><![CDATA[<p>Took a quick break today from Project Phoenix<sup><a href="#footnote-1-259" id="footnote-link-1-259" title="See the footnote.">1</a></sup>.</p>
<p>I&#8217;ve had a <a href="http://www.meandeviation.com/tutorials/learnphp/php-syntax-check/" target="_blank">PHP syntax checker</a> on <a href="http://www.meandeviation.com/" target="_blank">meandeviation</a> for several years, but only checking PHP 4 as that is what is running on the server.  However, I had an email asking about PHP 5 , so now there is a <a href="http://www.hcibook.com/meandeviation/php-syntax-check/v5/" target="_blank">PHP 5 version</a> too <img src='http://www.alandix.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The syntax checker is a pretty simple layer over the PHP <a href="http://uk.php.net/manual/en/features.commandline.options.php" target="_blank">command line option</a> &#8220;php -l&#8221; and also uses the PHP <a href="http://uk.php.net/manual/en/function.highlight-file.php" target="_blank">highlight_file</a> function.  The main complication is parsing the HTML outputs of both as they change between versions of PHP!</p>
<p>There is also a download archive so you can also have it running locally on your own system.</p>
<p><a href="http://www.alandix.com/images/php5-syntax-check-screenshot.png" target="_blank"><img class="alignnone" title="PHP syntax check screen shot" src="http://www.alandix.com/images/php5-syntax-check-thumbnail.gif" alt="" width="269" height="247" /></a></p>
<br /><ol class="footnotes"><li id="footnote-1-259">watch this space &#8230;  [<a href="#footnote-link-1-259">back</a>]</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.alandix.com/blog/2010/06/09/php-syntax-checker-updated/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>fix for WordPress shortcode bug</title>
		<link>http://www.alandix.com/blog/2009/07/26/fix-for-wordpress-shortcode-bug/</link>
		<comments>http://www.alandix.com/blog/2009/07/26/fix-for-wordpress-shortcode-bug/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 15:56:04 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[academic]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[shortcodes]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?p=196</guid>
		<description><![CDATA[I&#8217;m starting to use shortcodes heavily in WordPress1 as we are using it internally on the DEPtH project to coordinate our new TouchIT book.  There was minor bug which meant that HTML tags came out unbalanced (e.g. &#8220;&#60;p&#62;&#60;/div&#62;&#60;/p&#8221;). I&#8217;ve just been fixing it and posting a patch2, interestingly the bug was partly due to the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m starting to use <a href="http://codex.wordpress.org/Shortcode_API" target="_blank">shortcodes</a> heavily in <a href="http://wordpress.org/" target="_blank">WordPress</a><sup><a href="#footnote-1-196" id="footnote-link-1-196" title="See the footnote.">1</a></sup> as we are using it internally on the <a href="http://www.physicality.org/DEPtH/" target="_blank">DEPtH project</a> to coordinate our new <a href="http://www.physicality.org/TouchIT/" target="_blank">TouchIT book</a>.  There was minor bug which meant that HTML tags came out unbalanced (e.g. &#8220;&lt;p&gt;&lt;/div&gt;&lt;/p&#8221;).</p>
<p>I&#8217;ve just been fixing it and posting a patch<sup><a href="#footnote-2-196" id="footnote-link-2-196" title="See the footnote.">2</a></sup>, interestingly the bug was partly due to the fact that <a href="http://uk3.php.net/manual/en/regexp.reference.back-references.php" target="_blank">back-references</a> in regular expressions count from the beginning of the regular expression, making it impossible to use them if the expression may be &#8216;glued&#8217; into a larger one &#8230; lack of referential transparency!</p>
<p>For anyone having similar problems, full details and patch below (all WP and PHP techie stuff).</p>
<p><span id="more-196"></span></p>
<p>The regex returned by get_shortcode_regex() in <a href="http://svn.automattic.com/wordpress/tags/2.8.1/wp-includes/shortcodes.php" target="_blank">shortcodes.php</a> had a back-reference &#8216;\[\/\2\]&#8216; to match balanced tag-end-tag pairs such as &#8220;[x]text[/x]&#8220;.  The regex assumes it will be used &#8216;bare&#8217; as a regular expression.  However, wpautop in <a href="http://svn.automattic.com/wordpress/tags/2.8.1/wp-includes/formatting.php" target="_blank">formatting.php</a> (which turns newlines in posts into &lt;p&gt;s or &lt;br&gt;s) needs to remove &lt;p&gt;s form standalone shortcodes. To do this it adds extra enclosing brackets to the shortcode regex to be able to refer to the entire matched tags and content.  This means that the back-reference instead matches the (optional) preceding letter which is usually blank and thus the closing tag does not match properly.</p>
<p>In the case of single tag-end-tag pair, this means that</p>
<pre style="padding-left: 30px;"> [divtag]</pre>
<pre style="padding-left: 30px;"> abc</pre>
<pre style="padding-left: 30px;"> [/divtag]
</pre>
<p>ends up as</p>
<pre style="padding-left: 30px;"> &lt;div&gt;</pre>
<pre style="padding-left: 30px;"> &lt;p&gt;abc&lt;/p&gt;</pre>
<pre style="padding-left: 30px;"> &lt;p&gt;&lt;/div&gt;&lt;/p&gt;
</pre>
<p>This gets more complicated when there are internal tags such as:</p>
<p style="padding-left: 30px;">
<pre style="padding-left: 30px;"> [mytag] some [specialsymbol] text [/mytag]</pre>
<p>As in some cases the existing .*? for attribute matching chomps everything while it tries to find a &#8216;/&#8217;.</p>
<p>A simple fix would be to add an optional parameter to get_shortcode_regex($nested=0) and use this to modify the regular expression.  This would give the intended match for the shortcode regex, but in fact makes things worse. Looking at the same source:</p>
<pre style="padding-left: 30px;"> [divtag]</pre>
<pre style="padding-left: 30px;"> abc</pre>
<pre style="padding-left: 30px;"> [/divtag]</pre>
<p>it would generate (assuming [divtag] generates a div):</p>
<pre style="padding-left: 30px;"> &lt;div&gt;&lt;/p&gt;</pre>
<pre style="padding-left: 30px;"> &lt;p&gt;abc&lt;/p&gt;</pre>
<pre style="padding-left: 30px;"> &lt;p&gt;&lt;/div&gt;</pre>
<p>The attached patch addresses this by adding two new functions to shortcodes.php returning regular expressions for matching begin and end tags separately.  wpautop then has two separate preg_replace lines doing the &lt;p&gt; fixes.  I&#8217;ve tried to change as little as possible, although sometime might return to do things like extend it to allow nested copies of the same tag.</p>
<p>At the same time as doing the abve bug fix,  I swopped the .*? for attribute matching to [^\]]*?<br />
I guess slightly slower, but doesn&#8217;t risk chomping the entire post as a shortcode attribute :-/</p>
<p><strong>Download: </strong> <a href="http://www.alandix.com/blog/wp-content/uploads/2009/07/shortcodes_wpautop_patch_2009_07_26.diff">shortcodes patch</a><br />
Instructions for installing patches at <a href="http://markjaquith.com/" target="_blank">Mark Jacquith</a>&#8216;s entry on <a href="http://markjaquith.wordpress.com/2005/11/02/my-wordpress-toolbox/" target="_blank">My WordPress Toolbox</a>.</p>
<p>The patch in full:</p>
<pre class="brush: php; title: ;">

Index: wp-includes/shortcodes.php
===================================================================
--- wp-includes/shortcodes.php    (revision 11744)
+++ wp-includes/shortcodes.php    (working copy)
@@ -175,10 +175,40 @@
 $tagnames = array_keys($shortcode_tags);
 $tagregexp = join( '|', array_map('preg_quote', $tagnames) );

-    return '(.?)\[('.$tagregexp.')\b(.*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)';
+    return '(.?)\[('.$tagregexp.')\b([^\]]*?)(?:(\/))?\](?:(.+?)\[\/\2\])?(.?)';
 }

 /**
+ * Retrieve the shortcode regular expression for searching for start tag only.
+ *
+ * @uses $shortcode_tags
+ *
+ * @return string The shortcode start tag search regular expression
+ */
+function get_shortcode_start_regex() {
+    global $shortcode_tags;
+    $tagnames = array_keys($shortcode_tags);
+    $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
+
+    return '\[('.$tagregexp.')\b([^\]]*?)(?:(\/))?\]';
+}
+
+/**
+ * Retrieve the shortcode regular expression for searching for end tag only.
+ *
+ * @uses $shortcode_tags
+ *
+ * @return string The shortcode end tag search regular expression
+ */
+function get_shortcode_end_regex() {
+    global $shortcode_tags;
+    $tagnames = array_keys($shortcode_tags);
+    $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
+
+    return '\[\/('.$tagregexp.')\]';
+}
+
+/**
 * Regular Expression callable for do_shortcode() for calling shortcode hook.
 * @see get_shortcode_regex for details of the match array contents.
 *
Index: wp-includes/formatting.php
===================================================================
--- wp-includes/formatting.php    (revision 11744)
+++ wp-includes/formatting.php    (working copy)
@@ -170,7 +170,8 @@
 if (strpos($pee, '&lt;pre') !== false)
 $pee = preg_replace_callback('!(&lt;pre[^&gt;]*&gt;)(.*?)&lt;/pre&gt;!is', 'clean_pre', $pee );
 $pee = preg_replace( &quot;|\n&lt;/p&gt;$|&quot;, '&lt;/p&gt;', $pee );
-    $pee = preg_replace('/&lt;p&gt;\s*?(' . get_shortcode_regex() . ')\s*&lt;\/p&gt;/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone
+    $pee = preg_replace('/&lt;p&gt;\s*?(' . get_shortcode_start_regex() . ')\s*&lt;\/p&gt;/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone
+    $pee = preg_replace('/&lt;p&gt;\s*?(' . get_shortcode_end_regex() . ')\s*&lt;\/p&gt;/s', '$1', $pee);   // check both start and end tags

 return $pee;
 }
</pre>
<br /><ol class="footnotes"><li id="footnote-1-196">see section &#8220;using dynamic binding&#8221; in <a href="http://www.alandix.com/blog/2009/07/20/whats-wrong-with-dynamic-binding/" target="_blank">What’s wrong with dynamic binding?</a>  [<a href="#footnote-link-1-196">back</a>]</li><li id="footnote-2-196"><a href="http://core.trac.wordpress.org/ticket/10490" target="_blank">TRAC ticket #10490</a>  [<a href="#footnote-link-2-196">back</a>]</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.alandix.com/blog/2009/07/26/fix-for-wordpress-shortcode-bug/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>What&#8217;s wrong with dynamic binding?</title>
		<link>http://www.alandix.com/blog/2009/07/20/whats-wrong-with-dynamic-binding/</link>
		<comments>http://www.alandix.com/blog/2009/07/20/whats-wrong-with-dynamic-binding/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 20:14:19 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[academic]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[literate programming]]></category>
		<category><![CDATA[mobile design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress plugins]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?p=187</guid>
		<description><![CDATA[Dynamic scoping/binding of variables has a bad name, rather like GOTO and other remnants of the Bad Old Days before Structured Programming saved us all1.  But there are times when dynamic binding is useful and looking around it is very common in web scripting languages, event propagation, meta-level programming, and document styles. So is it [...]]]></description>
			<content:encoded><![CDATA[<p>Dynamic scoping/binding of variables has a bad name, rather like GOTO and other remnants of the <em>Bad Old Days</em> before <em>Structured Programming</em> saved us all<sup><a href="#footnote-1-187" id="footnote-link-1-187" title="See the footnote.">1</a></sup>.  But there are times when dynamic binding is useful and looking around it is very common in web scripting languages, event propagation, meta-level programming, and document styles.</p>
<p>So is it really so bad?</p>
<p><span id="more-187"></span></p>
<h3>tiny-screen coding and literate programming</h3>
<p>This issue came to mind because I&#8217;ve been considering what programming language support might be like for tiny screens (and I mean mobile phone not laptop!).  This was prompted by group discussions at the <a href=" Mobile Design Dialog webpage. http://www.cs.swan.ac.uk/mobdesign/ " target="_blank">Mobile Design Dialogue</a> conference last year<sup><a href="#footnote-2-187" id="footnote-link-2-187" title="See the footnote.">2</a></sup> and at a bootcamp session at the <a href="http://www.ukinit.org/02122008/winter-school-interactive-technologies" target="_blank">Winter School on Interactive Technologies</a> in Bangalore in February.</p>
<p>One of my first thoughts was that something akin to <a href="http://en.wikipedia.org/wiki/Literate_programming" target="_blank">literate programming</a> might provide a useful contribution as it allows details to be elided.  For example:</p>
<pre style="padding-left: 30px;">@{obtain email address from form variables}
if @{check email address valid}
then @{produce bad email error message}
else @{send email message}</pre>
<p>The placeholders (such as &#8220;check email address valid&#8221;) are then defined separately.  In <a href="http://www-cs-faculty.stanford.edu/~knuth/lp.html" target="_blank">Knuth&#8217;s original WEB</a> (and Harold Thimbleby&#8217;s <a href="http://comjnl.oxfordjournals.org/cgi/content/abstract/29/3/201" target="_blank">cweb</a>), all the definitions are in a single file, but in a phone-based environment this would be more like a hypertext &#8212; indeed reminiscent of the <a href="http://portal.acm.org/citation.cfm?id=54700" target="_blank">code browsers I worked on</a> when I first came into computer science!</p>
<p>Using this form of named fragments would mean that one could have much smaller snippets of code on screen at any moment of time, basically substituting abstraction for scrolling, hence the reason for thinking this could be good for tiny-screen coding.</p>
<p>However, the semantics of these fragments in literate programming is textual inclusion, basically macro expansion.  Amongst other things this means effectively dynamic scoping; for example, the code &#8220;obtain email address from form variables&#8221; might define a variable &#8220;email&#8221; that is then used in &#8220;send email message &#8220;.</p>
<p>However, the dynamic scoping is quite limited as the code fragments were always (or maybe normally) only used in one place, that is they are merely a way of hiding code.  This is quite different from a piece of code that takes different meaning wherever you place it dependent on what variables it picks up.</p>
<h3>scripts and templates</h3>
<p>As I began to think about this I recalled early templating languages I developed for code generation from data types (somewhat similar to current web scripting languages).  In these I used abstractions that were partly like a procedure, but also, similar to literate programming, in that &#8216;parameters&#8217; could be extracted from context if not given explicitly.  In the example earlier a &#8216;procedure&#8217; like &#8220;check email address valid &#8221; might have an explicit parameter  &#8216;email&#8217;, but this would be taken from the environment if it was not set explicitly.  This reflects the fact that in many situations there is an &#8216;obvious&#8217; parameter, just as in day to day life you just say &#8220;post the letter&#8221;, not &#8220;post the letter that is on the hall table just where you will obviously find it as you go out and where I always put it&#8221;.</p>
<p>While this still seems a little heretical when implemented as part of a programming language (or at least templating script), in fact it is common at the meta-coding level of &#8216;includes&#8217; found in most web scripting including JSP.  For example in PHP one might write:</p>
<pre style="padding-left: 30px;">&lt;?php
foreach ( $messages as $message ) {
    include "format-message.php";
}
?&gt;</pre>
<p>The PHP code in the file &#8221; format-message.php &#8221; is effectively textually included inside the &#8216;foreach&#8217; block and hence &#8216;$message&#8217; would be available to it as a variable.  While the base language is largely lexically scoped, the includes effectively allow an alternative abstraction with dynamic binding.  Purists would might decry the above code, but few have not  used something similar at some point. Indeed the <a href="http://wordpress.org/extend/plugins/" target="_blank">WordPress plugin system</a> depends entirely on careful use of such constructs.</p>
<p>In my websites (e.g. <a href="http://www.hcibook.com/e3/" target="_blank">hcibook.com/e3</a>, <a href="http://www.snipit.org/" target="_blank">snipit.org</a>) I typically have a &#8216;top.php&#8217; function that routes raw URLs to appropriate pages or scripts by use of &#8216;include&#8217;<sup><a href="#footnote-3-187" id="footnote-link-3-187" title="See the footnote.">3</a></sup>.  Often this performs common start-up actions such as establishing database connections, reading Cookies, or parsing URL path information, and, of course, the results of all of this are then available as context for the included scripts.  Sometimes I wrap this in a single global variable $context, but not always (life is short, and the sensible time to code a website shorter!).</p>
<h3>aggregate inheritance</h3>
<p>In a HTML document, the font of a paragraph is set not by some form of object inheritance based on the class of the &lt;p&gt; tag, but instead on the smallest enclosing block that has an explicit font style.  The binding of fonts is in a sense static or even lexical as it is based on where in the document structure the text sits, but it is not about the lexical structure of the CSS, but of the HTML document to which it is applied.</p>
<p>Similarly, in a GUI a mouse click on a button will typically go through a hierarchy where the enclosing elements (screen panel, etc.) get &#8216;first dibs&#8217; to filter or react to the event, eventually dribbling down to the button itself, and then sometimes back up again with the containers getting a chance to do some sort of &#8216;after the event&#8217; processing.  Just like the font on the page, the meaning of &#8216;click on button&#8217; cannot be found solely in the class structure of &#8216;JButton&#8217;, but in the dynamic location where it is found.</p>
<p>I don&#8217;t know a standard term for this, but I have always called it &#8216;<em>aggregate inheritance</em>&#8216; as elements inherit from the containers within which they are aggregated.</p>
<p>While lexical binding is about the structure of the <em>code</em>, normal dynamic binding is about the structure of the <em>process</em> (calling pattern) and aggregate inheritance is about the structure of the <em>data</em>.</p>
<h3>the bad and the worse</h3>
<p>The reasons for eschewing dynamic binding were partly about efficient compilation of block-structured languages, albeit now with complex object inheritance rules, dynamic binding seems tame!  The other reason was semantic as static (and especially lexical) binding allows a limited form of referential transparency, you can tell what a variable means and track the data flow &#8212; locality.</p>
<p>Clearly at one stage the jury was still out, indeed one of the innovations (I believe) in Algol 68 was the inclusion of dynamic binding and as late (sic) as 1986 papers were discussing ways of implementing dynamic binding<sup><a href="#footnote-4-187" id="footnote-link-4-187" title="See the footnote.">4</a></sup>.  However, even Lisp eventually moved to making its default binding static.</p>
<p>Today all major languages use lexical binding &#8212; except they don&#8217;t.  In fact there are numerous workarounds, just like the tortured workarounds we use for GOTO-less code<sup><a href="#footnote-5-187" id="footnote-link-5-187" title="See the footnote.">5</a></sup>.</p>
<p>Simple object inheritance is static, but only so long as no one changes the classes you inherit from!  Of course OO inheritance is itself often criticised, but is generally accepted as OK<sup><a href="#footnote-6-187" id="footnote-link-6-187" title="See the footnote.">6</a></sup>.</p>
<p>In even simple code we also use global variables, which while static in scope certainly allow non-local effects.  These are again especially common in web scripting where context seems particularly important. In fact they are so ubiquitous that special language constructs may be used such as PHP&#8217;s &#8216;<a href="http://uk3.php.net/manual/en/language.variables.superglobals.php" target="_blank">superglobals</a>&#8216; or &#8216;<a href="http://uk3.php.net/manual/en/function.define.php" target="_blank">define</a>&#8216;. On the client side also, Javascript code is often replete with globals.</p>
<p>Those using &#8216;proper&#8217; languages may look down on slack web programming practices.  However, the popular singleton pattern or static Class variables (such as in the Java System class) are but globals by another name, and database access and the filesystem again access a large global name space.</p>
<p>But if the problem of dynamic binding is non-locality, surely the worst culprits are pointers and references.  Pure functional programming takes referential transparency seriously, is side effect free and so avoids updatable references and pointers.  However, few programmers, however pure, are willing to work totally under FP&#8217;s strictures<sup><a href="#footnote-7-187" id="footnote-link-7-187" title="See the footnote.">7</a></sup>.</p>
<p>Non-locality of reference and effect seems, not just common but ubiquitous.  It is dangerous, but that is because it is powerful.  This is true of pointers, but equally of variable binding paradigms, the issue is how to ensure understanding and control of non-locality, not to forbid some kinds whilst allowing free reign to others.</p>
<h3>context and pipelines</h3>
<p>I&#8217;ve used the term &#8216;context&#8217; several times.  In ubicomp and intelligent interfaces issues connected with context are critical in interpreting user activity.  Similarly, natural language processing and vision at first (many years ago), took a very pure layered approach with each level of processing giving its results to the next up in a pipeline: lexical to syntactic to semantic.  However, human interpretation is not so neatly layered<sup><a href="#footnote-8-187" id="footnote-link-8-187" title="See the footnote.">8</a></sup>.  Both NLP and computer vision became a lot more successful when they allowed downwards paths from semantics back down to lower levels.</p>
<p>Compiler and language design took their form partly based on early models of language, indeed any cross-over between layers of processing was deemed problematic<sup><a href="#footnote-9-187" id="footnote-link-9-187" title="See the footnote.">9</a></sup>.  Whilst AI seems to have moved on, programming language design does not; in language and the physical world we assume that terms take meaning from context, why not in programming?</p>
<h3>using dynamic binding</h3>
<p>Recently I was writing <a href="http://codex.wordpress.org/Shortcode_API" target="_blank" title="Wordpress Shortcode API">shortcodes</a> in a WordPress plugin.  Shortcodes are tags in the text of a blog item that can be used as a form of extensible markup.  For example &#8220;[cite key=Dx09]&#8221; in the text might lookup and link to an entry in a bibliographic database<sup><a href="#footnote-10-187" id="footnote-link-10-187" title="See the footnote.">10</a></sup>.  Some of the codes are stand alone, but some come in pairs [/code]</p>
<p>Being a typical hacker, I first of all created a generic helper plug-in (SimpleShortcodeHandler) that made it easy to do simple things like add a span or div with a specific class.  However, I was aware that I might want to add more complicated things like a BibTeX-style bibliography list:</p>
<pre style="padding-left: 30px;">[bibliography format=APA"]
[bibitem key="Dx09 "] A. Dix (2009).
    What's wrong with dynamic binding? [/bibitem]
[bibitem key="Dy68"] Dykstra, E. (1968).
    GoTo … [/bibitem]
[/bibliography]</pre>
<p>The behaviour of the '[bibitem]' codes would clearly need to depend on the format of the bibliography, but the normal shortcode extension mechanism means (as in all good programming!) the code to process each shortcode is in isolation applied to its own text.  As a simple addition, the SimpleShortcodeHandler class keeps a stack of enclosing shortcodes in a post so that the handler for 'bibitem' can query the stack for the most recent enclosing 'bibliography' tag.  That is, I added a workaround for dynamic binding in the program code to enable aggregate inheritance in the blog post.</p>
<p>In fact, the way WordPress processes shortcodes means that the code for 'bibitem' is called within a call to the expansion code for 'bibliography', however, there are intervening calls to WordPress' internal functions, so one cannot simply pass in context information as an additional parameter.</p>
<p>Similar but more complex issues arise when dealing with sessions in web applications, AJAX programming, or other asynchronous remote procedure calls, all of which are basically an extreme case of program  inversion as found following structure clash in old <a href="http://en.wikipedia.org/wiki/Jackson_Structured_Programming" target="_blank">Jackson Structured Programming</a>, or more generally in <a href="http://en.wikipedia.org/wiki/Coroutine" target="_blank">co-routine-based coding</a>.</p>
<h3>adding dynamic binding to the language</h3>
<p>There seem to be two forms of dynamic binding that are commonly used.</p>
<p>The first is <em>shallow dynamic binding</em>, for things like the literate programming and PHP includes.  Here the dynamic binding is almost a form of implicit parameter passing, a convenient shortcut for writing code like:</p>
<pre style="padding-left: 30px;">function do_some_work_to_x(x) { . . . }
// . . . later in the code
x = 3;
do_some_work_to_x(x);</pre>
<p>The expanded code may update variables as well as use their values, but that is then like passing variables by reference.</p>
<p>When using PHP includes that are doing work (e.g. web page templates, as opposed to declaring functions) I usually add a comment block at the top saying what variables it expects to find in its environment.  There is no reason why this should not be made more explicit, whether hand written, or generated by a programming environment.</p>
<p>The second form is <em>deep dynamic binding</em> as in the shortcode stack described above, and indeed in many forms of meta-level programming where there is aggregate inheritance on a data structure.  In these examples, the dynamic binding is to variables that are connected to a module or similar namespace, but where the scoping of the namespace is not the call structure.</p>
<p>In the shortcode example, this simply was a means to pass information up and down to related functions, 'skipping' other 'foreign' functions on the way.</p>
<p>However, in other cases there may be a more complex pattern of scoping the context. A simple example would be for having variable 'constants' in a mathematical function class:</p>
<pre style="padding-left: 30px;">module Math {
  context { precision = 0.0001; base = 10; }
  sqrt(x) {
      y = 1;
      while ( abs(y*y-x)&gt; precision )
          y = (y+x/y)/2;
      return y;
  }
}</pre>
<p>Then in the calling code something like:</p>
<pre style="padding-left: 30px;">Math.push_context;
Math.precision=0.1;
z = sqrt(2);
Math.pop_context;</pre>
<p>To some extent this form of deep context is relatively easy to manage through coding patterns, but the ubiquity in transactional, asynchronous and co-routine programming, suggests deeper infrastructure support would be useful.</p>
<h3>in short</h3>
<p>In dealing with the lack of good mechanisms for dynamic binding, programmers are often forced into workarounds that are worse than if there were a simple way to do it in the language.  The non-locality problems of dynamic binding are also present in other accepted programming constructs such as pointers.  On the other hand, there are many contexts, especially in web programming, where forms of dynamic binding lead to neater, more comprehensible, and hence more robust and maintainable code. Dynamic programming can be dangerous and the overall approach of the past 20 years has been to hope it will go away. However, its ubiquity and utility suggests that including support for safe and appropriate dynamic binding in tools, frameworks or languages (and the three can be conflated) may be a better approach for the future.</p>
<br /><ol class="footnotes"><li id="footnote-1-187">Strangely also the days when major advances in substance seemed to be more important than minor advances in nomenclature  [<a href="#footnote-link-1-187">back</a>]</li><li id="footnote-2-187">Recently Ken Banks, Matt Jones and Gary Marsden who proposed the MDD discussion are amongst the partners in a new project &#8216;<a href="http://mobility.kiwanja.net/" target="_blank">mobility</a>&#8216;, which is planning to work on mobile-based programming tools.  [<a href="#footnote-link-2-187">back</a>]</li><li id="footnote-3-187">Similar to the pattern proposed by <a href="http://josephscott.org/" target="_blank">Joseph Scott</a> in &#8220;<a href="http://josephscott.org/archives/2008/11/php-url-routing-pur/" target="_blank">PHP URL Routing (PUR)</a>&#8220;  [<a href="#footnote-link-3-187">back</a>]</li><li id="footnote-4-187">e.g. Gantenbein, R. E. and Jones, D. W. 1986. Dynamic binding of separately compiled objects under program control. In Proceedings of the 1986 ACM Fourteenth Annual Conference on Computer Science (Cincinnati, Ohio, United States). CSC &#8217;86. ACM, New York, NY, 287-292. DOI= <a href="http://doi.acm.org/10.1145/324634.325436" target="_blank">http://doi.acm.org/10.1145/324634.325436</a>  [<a href="#footnote-link-4-187">back</a>]</li><li id="footnote-5-187">The argument for avoiding where possible GOTOs was of course clearly made by Dijkstra in &#8220;<a href="http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html" target="_blank">Go To Statement Considered Harmful</a>&#8221; (Communications of the ACM, Vol. 11, No. 3, March 1968, pp. 147-148). However, see the <a href="http://david.tribble.com/text/goto.html" target="_blank">example of an LR parser</a> in <a href="http://david.tribble.com/" target="_blank">David Tribble</a>&#8216;s commentary on Dijkstra&#8217;s paper.  The parser would typically be coded now-a-days using a number of state variables, obfuscating the meaning far more than the controlled use of GOTO.  [<a href="#footnote-link-5-187">back</a>]</li><li id="footnote-6-187">Oddly, whilst inheritance is deemed OK, for some reason <a href="http://en.wikipedia.org/wiki/Mixin" target="_blank">mix-ins</a> are avoided in Java and indeed most modern OO languages.  This seems to be due to some of the semantic quagmire of multiple-inheritance, but mix-ins can be made semantically innocuous and would be so useful!  To some extent <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" target="_blank">Aspect-Oriented Programming</a> has some of the feel of mix-ins, and can be used to solve some similar problems; however it too brings issues of locality and transparency.  [<a href="#footnote-link-6-187">back</a>]</li><li id="footnote-7-187">Although <a href="http://labs.google.com/papers/mapreduce.html" target="_blank">Google&#8217;s MapReduce</a> shows functional programming can be very powerful.  Having worked with FP people for many years, I find much of my own code has an FP feel.  [<a href="#footnote-link-7-187">back</a>]</li><li id="footnote-8-187">In particular, semantic levels of processing &#8216;pass down&#8217; context to lower levels so that they &#8216;know what to expect&#8217; and so can disambiguate stimuli.  To some extent we see and hear what we expect, hence various forms of optical illusion.  [<a href="#footnote-link-8-187">back</a>]</li><li id="footnote-9-187">For example, in K&amp;R C, the expression: &#8220;kind (*fun)();&#8221; is syntactically ambiguous unless you know whether &#8216;kind&#8217; is a user defined type. This rare cross-over between syntax and semantics was seen as a weakness in the language.  [<a href="#footnote-link-9-187">back</a>]</li><li id="footnote-10-187">I&#8217;m writing plugin&#8217;s to help write the <a href="http://www.physicality.org/TouchIT/" target="_blank">TouchIT book</a>  [<a href="#footnote-link-10-187">back</a>]</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.alandix.com/blog/2009/07/20/whats-wrong-with-dynamic-binding/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>a simple PHP record sorter class</title>
		<link>http://www.alandix.com/blog/2009/07/03/a-simple-php-record-sorter-class/</link>
		<comments>http://www.alandix.com/blog/2009/07/03/a-simple-php-record-sorter-class/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 11:51:31 +0000</pubDate>
		<dc:creator>alan</dc:creator>
				<category><![CDATA[academic]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://www.alandix.com/blog/?p=179</guid>
		<description><![CDATA[Not for the first time I needed to sort arrays of arrays in PHP (structures like tiny DB tables).  I have previously written little wrapper functions round usort, but decided this time to make a small class. It is a simple, but generic utility, so popping it up in case useful to anyone. The rest [...]]]></description>
			<content:encoded><![CDATA[<p>Not for the first time I needed to sort arrays of arrays in PHP (structures like tiny DB tables).  I have previously written little wrapper functions round <a href="http://uk.php.net/manual/en/function.usort.php" target="_blank">usort</a>, but decided this time to make a small class. It is a simple, but generic utility, so popping it up in case useful to anyone.</p>
<p>The rest of this post has moved to a permanent page at:</p>
<p style="padding-left: 30px;"><a href="http://www.alandix.com/blog/code/sorter/" target="_self">http://www.alandix.com/blog/code/sorter/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.alandix.com/blog/2009/07/03/a-simple-php-record-sorter-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

