<?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>Cameron McKay &#187; PHP</title>
	<atom:link href="http://cdmckay.org/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Mon, 14 May 2012 21:11:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to do an AJAX search with jQTouch, Part 2</title>
		<link>http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/</link>
		<comments>http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 15:53:25 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1667</guid>
		<description><![CDATA[In the last installment of How to do an AJAX search with jQTouch we looked at how to setup a jQTouch interface with the goal of performing an AJAX search. In this article, we will write the necessary JavaScript to perform that AJAX search, as well as a PHP script to respond to those calls. [...]]]></description>
			<content:encoded><![CDATA[<p>In the last installment of <a href="http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/">How to do an AJAX search with jQTouch</a> we looked at how to setup a jQTouch interface with the goal of performing an AJAX search. In this article, we will write the necessary JavaScript to perform that AJAX search, as well as a PHP script to respond to those calls.</p>
<p>This article assumes you have read <a href="http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/">the first part of this series</a>.</p>
<p><span id="more-1667"></span></p>
<p>Before we start, we should take a step back and consider what we&#8217;re about to do.  Our Searcher app has three parts:</p>
<ul>
<li>the client-side jQTouch UI,</li>
<li>the client-side jQuery AJAX calls,</li>
<li>the server-side PHP script (that could easily be backed by a database)</li>
</ul>
<p>At this point we have finished the jQTouch UI.  However, instead of immediately moving on to writing the jQuery AJAX calls, let&#8217;s start with the PHP script instead.  This will help us understand our AJAX calls when it comes time to write them.</p>
<h3>The PHP Script</h3>
<p>Since our PHP script will be performing a search, let&#8217;s create a file called <code>search.php</code> and add a single line of boilerplate:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></pre></div></div>

<p>There&#8217;s no need for an end tag.  In fact, in files containing only PHP, <a href="http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html">it is recommended that you do not include an end tag</a>:</p>
<blockquote><p>For files that contain only PHP code, the closing tag (&#8220;?&gt;&#8221;) is never permitted. It is not required by PHP, and omitting it prevents the accidental injection of trailing white space into the response.</p></blockquote>
<p>Our Searcher app needs to search for something, so let&#8217;s pretend it&#8217;s an interface for a music player and we&#8217;re searching for songs. In order to express that idea into PHP code, we need the following elements:</p>
<ul>
<li>a Song class to hold the artist name and the title,</li>
<li>an array holding a bunch of Song objects,</li>
<li>a function for searching the Song array,</li>
<li>a way to get a search value from the AJAX request,</li>
<li>a way to set the MIME type to JSON,</li>
<li>a way to encode our Song array as JSON</li>
</ul>
<p>First, we&#8217;ll need a class to hold the songs (place this starting at line 3 in <code>search.php</code>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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;">class</span> Song <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$artist</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$artist</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">artist</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$artist</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s break it down for those not familiar with PHP classes:</p>
<ul>
<li>at line 3, we declare the class name</li>
<li>at lines 4-5, we declare two public fields to represent the artist&#8217;s name and the title of the song,</li>
<li>at lines 7-10, we declare the class constructor that we use to initialize the two fields of a Song object</li>
</ul>
<p>Now that we have a way to model songs in our PHP script, we need to make an array of some dummy songs that we can query using AJAX:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$songs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Say It Ain't So&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Undone&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cake&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Meanwhile, Rick James...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The Stars&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Ageless Beauty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Roy Orbison&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;In Dreams&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now we have something of a faux database setup. How will we search it?  Why, with a search function, of course! Let&#8217;s write that now:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</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;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$songs</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$matches</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$matchArtist</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">artist</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$matchTitle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matchArtist</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$matchTitle</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$s</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$matches</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So what does this function do? Let&#8217;s take a look line-by-line:</p>
<ul>
<li>at lines 22-23, we check to see if the search value is empty (i.e. an empty string or null). If that&#8217;s the case, then we just return all the songs</li>
<li>at line 25, we declare the <code>$matches</code> array. This will contain all the songs that match the search value <code>$value</code></li>
<li>at line 26, we use a foreach loop to iterate through all the Song objects in the <code>$songs</code> array</li>
<li>at lines 28-29, we check to see if the artist name or song title contain the search value <code>$value</code></li>
<li>at line 30-31, we add the Song object to the <code>$matches</code> array if it matches the artist name or song title</li>
<li>at line 33, we return whatever matches (if any) we have found</li>
</ul>
<p>Ok, so now we have a Song class, an array of Song objects, and a function that searches that array. What&#8217;s left in our PHP script? Well, as it stands, we have no way to interface with it. What we need to do now is have some sort of way to respond to the AJAX GET request that our Searcher app will send. To do that, we harness the awesome power of the <code>$_GET</code> <a href="http://php.net/manual/en/language.variables.superglobals.php">superglobal</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>36
37
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$matches</span> <span style="color: #339933;">=</span> search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>On the first line, we extract &#8220;value&#8221; parameter from the <code>$_GET</code> superglobal and assign it to the <code>$value</code> variable.  The second line runs our search function on the <code>$songs</code> database and searches for songs that have an artist or title that contains the <code>$value</code> string.</p>
<p>We&#8217;re almost there!  At this point we&#8217;ve managed to get an array of Song objects that match the passed search value.  All we need to do now is return that PHP array of objects as a JSON-encoded array of objects.  Let&#8217;s do that now:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>39
40
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The first line sets the the <a href="http://en.wikipedia.org/wiki/Internet_media_type">MIME type</a> of an HTTP response.  MIME types are a sort of meta data that is used to describe what sort of content is being returned.  In this case, we are telling jQuery (or whatever client that issued the request) that we are returning content that is JSON formatted.  The second line is a built-in PHP function that converts PHP types into a string containing their JSON counterparts.  Basically, it allows us to encode our PHP objects into JavaScript and allow us to access them in a nearly identical way:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// PHP</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Artist 1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Title 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Artist 2&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Title 2&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Becomes... (after json_encode)</span>
<span style="color: #666666; font-style: italic;">// JavaScript</span>
<span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span> artist<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Artist 1&quot;</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Title 1&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> artist<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Artist 2&quot;</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Title 2&quot;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And we&#8217;re done the PHP script! If we want to test it, we can load execute the PHP file on our server and try different search values.  For example, entering this URL should return a JSON array with two Song objects:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// URL</span>
http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//example.com/jQTouch-ajax-search/search.php?value=Weezer</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Response</span>
<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;artist&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Say It Ain't So&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;artist&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Undone&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span></pre></div></div>

<h3>The jQuery AJAX calls</h3>
<p>The finish line is now in sight.  We&#8217;ve successfully written our jQTouch UI and our PHP script.  The last thing we need to do is write some jQuery AJAX calls to execute our search and load the results into our UI.  In order to do that, we need to do the following:</p>
<ul>
<li>listen for the submission of the search form in the jQTouch UI</li>
<li>when the submission happens, get the search value from the form and set it as a parameter in an AJAX call to <code>search.php</code></li>
<li>register a callback that will populate the jQTouch search results when the AJAX call completes</li>
</ul>
<p>Let&#8217;s start with listening for the submit event on the search form.  First, we&#8217;ll need to create a new JavaScript source file <code>search.js</code> and fill it with the following boilerplate:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Put code here.    </span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What this boilerplate does is give our JavaScript script its own context so that can minimize leaking variables into the global JavaScript namespace.  It also gives us access to the jQuery object in the convenient <code>$</code> form, regardless of whether or not <a href="http://api.jquery.com/jQuery.noConflict/">&#8220;no conflict&#8221; mode is enabled</a>.</p>
<p>Recall in part 1 we defined an HTML search form that looked like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;toolbar&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Search<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;back&quot;</span>&gt;</span>Back<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;rounded&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> placeholder<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Search&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;edgetoedge&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-results&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sep&quot;</span>&gt;</span>Results<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>                
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<p>Note that on line 1 we defined the id of the form as <code>search</code>. We&#8217;ll use that to find the form and register a handler for its submit event so that we can take action when someone tries to perform a search using the jQTouch UI.</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
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> info<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[id=search-text]&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        text.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search-results&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        results.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sep&quot;</span><span style="color: #339933;">,</span>
            text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Results for &quot;'</span> <span style="color: #339933;">+</span> text.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search.php&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#123;</span> value<span style="color: #339933;">:</span> text.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> song<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> song.<span style="color: #660066;">artist</span> 
                        <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; - &quot;</span>
                        <span style="color: #339933;">+</span> song.<span style="color: #660066;">title</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;li&gt;&quot;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>       
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There&#8217;s a lot there, so let&#8217;s break it down line-by-line:</p>
<ul>
<li>starting at line 3 and ending at line 32 we have <a href="http://api.jquery.com/ready/">the jQuery ready handler</a> which is run once the DOM has finished loading</li>
<li>starting at line 5 and ending at line 30 we bind a submit event handler to the submit event for the <code>#search</code> form</li>
<li>at lines 6-7, we retrieve the <code>search-text</code> element, assign it to a variable (for later use on line 16), and then blur (i.e. deselect) the element so that the iPhone keyboard will go away</li>
<li>at lines 9-13, we clear any previous items in the list that may be hanging around from a previous search and then load in a new separator (&#8220;sep&#8221;) that contains the search value</li>
<li>at line 15-27, we perform our AJAX GET request to <code>search.php</code> with the value of the search text field as our value parameter (line 16) and a callback that adds the results to the list (lines 17-26)</li>
<li>finally, at line 29, we return false to prevent the default form behaviour (i.e. submitting the form) from being carried out</li>
</ul>
<p>And we&#8217;re done.  All we need to do now and include <code>search.php</code> in the header of <code>index.html</code> so that it&#8217;ll be run:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;search.js&quot; type=&quot;application/x-javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<h3>Conclusion</h3>
<p>In this, the final installment of <em>How to do an AJAX search with jQTouch</em>, we finished off the example application we started in <a href="http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/">the first part of this series</a>.  </p>
<p>In part 1, we covered building a UI using jQTouch.  In part 2, we helped visualize our problem by pretending that we were writing a front-end UI to a server-side music player.  To do that, we jumped into server side programming with PHP to write a script to serve us JSON-encoded Song objects.  Once we had that setup, we moved back into JavaScript.  In order to make use of the PHP script, we wrote a jQuery handler to respond to a jQTouch form submission.  The handler, once triggered, issued an AJAX call to the PHP script to pull down the appropriate Song objects for display in our UI.</p>
<h3>What Now?</h3>
<p>With respect to the goals of this tutorial, nothing.  However, if you want to pimp up the application a bit more, some things you could do are: </p>
<ul>
<li>Add genres and albums to the PHP Song object and then make the jQTouch UI menu have two additional options: Genres and Albums</li>
<li>Try to make a Now Playing sub-menu that is activated when a Song is picked from the Search screen</li>
</ul>
<h3>Source</h3>
<p>The source code I have written in this article is public domain, and you are free to do what you will with it. Here is <a href="http://cdmckay.org/files/jqtouch-ajax-search-part2.zip">a zip of the source, along with the required jQTouch source</a>. It should work pretty much right out of the box.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>How to query posts by template in WordPress</title>
		<link>http://cdmckay.org/blog/2010/05/13/how-to-query-posts-by-template-in-wordpress/</link>
		<comments>http://cdmckay.org/blog/2010/05/13/how-to-query-posts-by-template-in-wordpress/#comments</comments>
		<pubDate>Thu, 13 May 2010 17:39:37 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1508</guid>
		<description><![CDATA[Ever wanted to filter a query_posts call by template? If you&#8217;re reading this post, you probably have or need to right now. You probably went to the query_posts documentation and scanned for something like &#8220;template=foo&#8221; and were deeply disappointed. Then maybe, in an act of desperation, you started to trudge around the plugin library. Stop! [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to filter a query_posts call by template?  If you&#8217;re reading this post, you probably have or need to right now.  You probably went to <a href="http://codex.wordpress.org/Function_Reference/query_posts">the query_posts documentation</a> and scanned for something like &#8220;template=foo&#8221; and were deeply disappointed.  Then maybe, in an act of desperation, you started to trudge around <a href="http://wordpress.org/extend/plugins/">the plugin library</a>.  Stop!  Stop right there!  You don&#8217;t need a plugin to do this.  Everything you need is already in <a href="http://wordpress.org">WordPress</a>.</p>
<p><span id="more-1508"></span></p>
<p>Let&#8217;s start with a question: how does WordPress associate templates?  The answer: as a secret custom field.  What&#8217;s a custom field, you ask?  They&#8217;re those useful little suckers under the content area in the WP editor:</p>
<p><a href="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/05/wordpress-custom-fields.png" rel="lightbox[1508]"><img class="aligncenter size-full wp-image-1522" title="WordPress Custom Fields" src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/05/wordpress-custom-fields.png" alt="" width="300" height="406" /></a></p>
<p>But if the template is a custom field, you ask, then how come I don&#8217;t see it down there?  Well, I just said they&#8217;re secret.  After poking around WP database, I discovered the template for a page is determined by the custom field name <code>_wp_page_template</code>.  My suspicion is that anything with the prefix _wp is hidden from the WP admin area and therefore secret.</p>
<h3>Harnessing the awesome power of custom fields</h3>
<p>On the bright side, even if you can&#8217;t see the <code>_wp_page_template</code> in the custom fields area, I can assure you it&#8217;s still there.  And if it&#8217;s there, you can use it in any of your nefarious schemes.</p>
<p>Query_posts provides 3 parameters relating to custom fields: meta_key, meta_value, and meta_compare.  With those in mind, imagine you have a template called Profile (profile.php), and you want to exclude any page that is a Profile from our query.  In order to do that, you&#8217;d would make a query_posts call like so:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">query_posts<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'page'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'meta_key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'_wp_page_template'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'meta_value'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'profile.php'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'meta_compare'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'!='</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The main thing to note here is that the template is identified by the name of the PHP file (profile.php) and not by the the name of template (Profile).</p>
<h3>Wrap-up</h3>
<p>Filtering your query_posts query by template is as easy as filtering by any custom field.  No plugin required.  Just remember: use the PHP file name as the template identifier, not the template name.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/05/13/how-to-query-posts-by-template-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP interfaces and optional parameters</title>
		<link>http://cdmckay.org/blog/2010/04/24/php-interfaces-and-optional-parameters/</link>
		<comments>http://cdmckay.org/blog/2010/04/24/php-interfaces-and-optional-parameters/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 03:03:32 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1480</guid>
		<description><![CDATA[Interfaces in object-oriented languages like Java, C# and PHP are useful language constructs for ensuring that objects will respond to certain methods. However, unlike Java and C# (until recently), PHP has optional parameters. As it turns out, this feature has an interesting (and a little bit unexpected) effect on how we can implement interfaces in [...]]]></description>
			<content:encoded><![CDATA[<p>Interfaces in object-oriented languages like <a href="http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html">Java</a>, <a href="http://msdn.microsoft.com/en-us/library/ms173156.aspx">C#</a> and <a href="http://php.net/manual/en/language.oop5.interfaces.php">PHP</a> are useful language constructs for ensuring that objects will respond to certain methods.  However, unlike Java and C# (<a href="http://msdn.microsoft.com/en-us/library/dd264739.aspx">until recently</a>), <a href="http://php.net/manual/en/functions.arguments.php">PHP has optional parameters</a>.  As it turns out, this feature has an interesting (and a little bit unexpected) effect on how we can implement interfaces in PHP.</p>
<p><span id="more-1480"></span></p>
<p>Consider a simple interface for making objects comparable:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> IComparable <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> compareTo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Normally, in Java and C# (before 4.0) the only way to implement this interface would be to make a carbon copy of the method signature (save for the name of the parameter) and then fill in the method body.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> NetString implements IComparable <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> compareTo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #339933;">...</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This satisfies the interface requirements because we need to have a method that responds to a method named <code>compareTo</code> with a single argument.  However, things get interesting when we introduce optional parameters.  That&#8217;s because we can write a method signature that is different from that found in the interface but still satisfies the requirement that the method have the correct name and accept only 1 parameter.</p>
<p>How is that possible?  Well, imagine we wanted our NetString class to optionally ignore case when comparing:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> NetString implements IComparable <span style="color: #009900;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> compareTo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ignoreCase</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #339933;">...</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice how we slipped in another (optional) argument <code>$ignoreCase</code>? Does this code still satisfy the IComparable interface?  Absolutely.  Why?  Because we can still call <code>compareTo($object)</code> in a meaningful way, and that&#8217;s all the matters.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/24/php-interfaces-and-optional-parameters/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Adding Extension Methods to PHP</title>
		<link>http://cdmckay.org/blog/2010/04/23/adding-extension-methods-to-php/</link>
		<comments>http://cdmckay.org/blog/2010/04/23/adding-extension-methods-to-php/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 19:51:16 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1432</guid>
		<description><![CDATA[If you&#8217;ve ever used the .NET Framework version 3.5 of later, you&#8217;ll probably have encountered extension methods. Microsoft describes extension methods as: Extension methods enable you to &#8220;add&#8221; methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever used the .NET Framework version 3.5 of later, you&#8217;ll probably have encountered <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">extension methods</a>.  Microsoft describes extension methods as:</p>
<blockquote><p>Extension methods enable you to &#8220;add&#8221; methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
</p></blockquote>
<p>In this article, I will show you how to write a base class that will allow you to add methods to any PHP class that inherits from it at runtime.  You will be able to call these methods transparently, without any special syntax.</p>
<p><span id="more-1432"></span></p>
<p>For those unfamiliar with .NET, I&#8217;ll explain extension methods in a PHP context.  For example, consider the following code for a limited .NET-style string class in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">final <span style="color: #000000; font-weight: bold;">class</span> NetString <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> toUpper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> NetString<span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> string<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NetString<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$upper</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toUpper</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">string</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// $upper now contains &quot;FOO&quot;</span></pre></div></div>

<p>What if, at some point in the future, we wanted to add a <code>toLower</code> method and we didn&#8217;t have access to the source code?  The first thing that probably comes to mind is to use inheritance.  Unfortuantely, <code>NetString</code> is a final class, so that&#8217;s out of the question.  So what else?  </p>
<p>All we&#8217;re left with is using a function or a static method.  Wouldn&#8217;t it be nice if we could do something like this?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">NetString<span style="color: #339933;">::</span><span style="color: #004000;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;toLower&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> NetString<span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">string</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NetString<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$upper</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toUpper</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">string</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// $upper now contains &quot;FOO&quot;</span>
&nbsp;
<span style="color: #000088;">$lower</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$upper</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toLower</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">string</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// $lower now contains &quot;foo&quot;</span></pre></div></div>

<p>Starting with PHP 5.3, this is possible thanks to closures and some other new features.  The rest of this article will be dedicated to explaining how to setup extension methods in PHP.</p>
<h3>How It Works</h3>
<p>In order to add extension methods to PHP, we need to do a couple of things.  First off, since an extension method is basically a missing method, we need to find some way to catch any calls to missing methods.  Fortunately, PHP provides <a href="http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods">a mechanism for doing this</a>.  It&#8217;s a magic method called <code>__call</code>.</p>
<p>What we need to do is make sure any class that we want to have extension methods have it&#8217;s <code>__call</code> method rerouted to a method dispatcher.  This dispatcher will have to be static so all instances of a class can access it.  In order to ensure that these calls are rerouted to the dispatcher, we&#8217;ll create a common base class that does that automatically for us.  We&#8217;ll call this base class <code>Extensible</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">methodDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> methodDispatcher<span style="color: #009900;">&#40;</span> 
            <span style="color: #000088;">$instance</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now that we have our missed calls rerouted to our dispatcher, we need two more things.  We need a way to add methods to the object (<code>addMethod</code>) and we need somewhere to store them to look them up with the dispatcher (<code>$methodTable</code>).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">methodDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$methodTable</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> methodDispatcher<span style="color: #009900;">&#40;</span> 
            <span style="color: #000088;">$instance</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> addMethod<span style="color: #009900;">&#40;</span><span style="color: #000088;">$methodName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We&#8217;re almost there.  How can we store a method so that we can uniquely identify it?  The easiest way would be in an associative array indexed by class name.  Each value in the that associative array would be another associative array holding all the extension methods (i.e. the closures) for that class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">methodDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$methodTable</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> methodDispatcher<span style="color: #009900;">&#40;</span> 
            <span style="color: #000088;">$instance</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> addMethod<span style="color: #009900;">&#40;</span><span style="color: #000088;">$methodName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$methodTable</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>        
            <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                
&nbsp;
        <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$methodName</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$method</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now that we have all the extension methods stored, how do we invoke them?  Well, if they&#8217;re going to be useful at all, they&#8217;ll need to have access to the instance they&#8217;re attached to.  Like .NET, we&#8217;ll provide that automatically as the first argument.  We also need to pass the arguments that were passed in the call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">methodDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$methodTable</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> methodDispatcher<span style="color: #009900;">&#40;</span> 
            <span style="color: #000088;">$instance</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$methodTable</span><span style="color: #339933;">;</span>        
        <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$instance</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">do</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</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;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_parent_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Method not found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$func</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">array_unshift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000088;">$instance</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$func</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> addMethod<span style="color: #009900;">&#40;</span><span style="color: #000088;">$methodName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
        <span style="color: #000088;">$table</span> <span style="color: #339933;">=&amp;</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$methodTable</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>        
            <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                
&nbsp;
        <span style="color: #000088;">$table</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$methodName</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$method</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And we&#8217;re there.  Now, in order to add extension methods to a class, we just have to ensure that inherits from our Extensible class.  I&#8217;ll admit this is kind of a crappy requirement, but it&#8217;s the best we can do for now.</p>
<p>Here&#8217;s an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">final <span style="color: #000000; font-weight: bold;">class</span> Bug <span style="color: #000000; font-weight: bold;">extends</span> Extensible <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$arms</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arms</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">arms</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$arms</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getArms<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">arms</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Bug<span style="color: #339933;">::</span><span style="color: #004000;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hug&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bug</span><span style="color: #339933;">,</span> <span style="color: #000088;">$otherBug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$bug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; hugs &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$otherBug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Bug<span style="color: #339933;">::</span><span style="color: #004000;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;punch&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bug</span><span style="color: #339933;">,</span> <span style="color: #000088;">$otherBug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$bug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
       <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; punches &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$otherBug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; with &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$bug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getArms</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; fists &quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$doug</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Doug&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fred</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Fred&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fred</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hug</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$doug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$doug</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">punch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fred</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Limitations</h3>
<p>This implementation of extension methods in PHP isn&#8217;t all puppy dogs and ice cream.  It has its share of drawbacks:</p>
<ul>
<li>Classes must inherit from <code>Extensible</code> to be capable of having extension methods.  This sucks, but you can get around it in existing classes by using composition.</li>
<li>Speed.  Although I have not tested it myself, magic methods like <code>__call</code> are apparently quite slow.</li>
<li><del datetime="2010-04-23T19:52:15+00:00">Ignores inheritance.  This particular implementation does not follow the inheritance chain to see if any parent classes have extension methods for a given method call.  This could be easily remedied with a bit of code.</del> See the update below.</li>
</ul>
<h3>Source</h3>
<p>The source code I have written in this article is public domain, and you are free to do what you will with it. <a href="http://cdmckay.org/files/Extensible.zip">Here is the source file.</a></p>
<h3>Update</h3>
<p>I&#8217;ve updated the code so that <code>methodDispatcher</code> will search the inheritance chain for a suitable extension method.  This behaviour is illustrated by this example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #000000; font-weight: bold;">extends</span> Extensible <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> Bar <span style="color: #000000; font-weight: bold;">extends</span> Foo <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
Foo<span style="color: #339933;">::</span><span style="color: #004000;">addMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;customMethod&quot;</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Foo&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Bar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// $result contains the string &quot;Foo&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/23/adding-extension-methods-to-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to do an AJAX search with jQTouch, Part 1</title>
		<link>http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/</link>
		<comments>http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 06:22:35 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[jqtouch]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1371</guid>
		<description><![CDATA[jQTouch is a pretty nifty little jQuery plugin for making websites look like native iPhone apps. Unfortunately, beyond a bunch of examples included with the distribution, there&#8217;s not a whole lot of online documentation written for jQTouch. Having started to use jQTouch for a professional project, I thought I&#8217;d help remedy this deficiency by posting [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.jqtouch.com/">jQTouch</a> is a pretty nifty little <a href="http://jquery.com">jQuery</a> plugin for making websites look like native iPhone apps.  Unfortunately, beyond a bunch of examples included with the distribution, there&#8217;s not a whole lot of online documentation written for jQTouch.  Having started to use jQTouch for a professional project, I thought I&#8217;d help remedy this deficiency by posting what I learned along the way.</p>
<p>We&#8217;ll start with something fairly straightforward by making a jQTouch app that makes an AJAX call to a PHP script to search a database.  It then displays the results of that search in the jQTouch app.  This article is split up into two parts.  The first part deals with the jQTouch UI and the second part deals with the PHP script and the supporting JavaScript.</p>
<p>This is part 1 <a href="http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/">of a two part series</a>.</p>
<p><span id="more-1371"></span></p>
<h3>jQTouch UI</h3>
<p>Let&#8217;s start with our jQTouch UI code.  First, <a href="http://jqtouch.googlecode.com/files/jqtouch-1.0-beta-2-r109.zip">we&#8217;ll need to grab the jQTouch distribution</a>.  Use your favourite archiver to extract two folders: <code>jqtouch</code> and <code>themes</code> and put them in our document root.</p>
<p>Next, we need to create an index page, <code>index.html</code>, in our document root.  We&#8217;ll need some boilerplate to get things started:</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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;viewport&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.com/jsapi&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span> google.load(&quot;jquery&quot;, &quot;1.4.2&quot;); <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jqtouch/jqtouch.min.js&quot;</span> <span style="color: #000066;">charset</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;utf-8&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span> $.jQTouch(); <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span>&gt;</span>@import &quot;jqtouch/jqtouch.min.css&quot;;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span>&gt;</span>@import &quot;themes/jqt/theme.min.css&quot;;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Searcher<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></td></tr></table></div>

<p>Here&#8217;s the breakdown: </p>
<ul>
<li>on line 5, we tell the iPhone not to allow zooming,</li>
<li>on lines 6-7, we load jQuery from the Google CDN,</li>
<li>on line 8, we load the jQTouch JavaScript source,</li>
<li>on line 9, we initialize jQTouch,</li>
<li>on lines 10-11, we load the jQTouch core stylesheet and the theme stylesheet.</li>
</ul>
<h3>The Home Page</h3>
<p>Now that we have the boilerplate setup, it&#8217;s time to start adding some UI elements.  jQTouch makes this extremely simple by handling most of the heavy lifting (add this between the <code>body</code> tags):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;home&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;current&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;toolbar&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Searcher<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;rounded&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;arrow&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#search&quot;</span>&gt;</span>Search<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>Woah, that&#8217;s a lot packed into these 8 lines.  Let&#8217;s break it down:</p>
<ul>
<li>on line 1, we have a div with the id &#8220;home&#8221;.  This is a special id for jQTouch that indicates the entry point (i.e. the first page shown).</li>
<li>on line 2-4, we have the toolbar.  This is something that is repeated for every jQTouch page.  It&#8217;s the part that is shown at the top of any iPhone GUI app and, consequently, any jQTouch app.</li>
<li>on line 5-7, we have the content of this page.  Basically, we are telling jQTouch that we want a list of rounded buttons, one of which is &#8220;Search&#8221;.  The <code>#search</code> href in the anchor tag tells jQTouch what page we want to load when that button is clicked.</li>
</ul>
<p>Before I move on, let&#8217;s look at a screenshot of what we have so far: </p>
<p><a href="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/04/home.jpg" rel="lightbox[1371]"><img src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/04/home.jpg" alt="" title="home" width="320" height="480" class="aligncenter size-full wp-image-1408" /></a></p>
<p>Not too shabby considering we&#8217;ve written all of 8 lines of HTML to make that happen!  Unfortunately, as you&#8217;ve probably noticed, clicking the Search button does nothing but make it turn green.</p>
<h3>The Search Page</h3>
<p>Let&#8217;s hook up the Search button from the previous page to a search page.  This will be where we enter our search text.  Here&#8217;s the code for the search page (it goes right after the code in the last listing):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;toolbar&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Search<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;back&quot;</span>&gt;</span>Back<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;rounded&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> placeholder<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Search&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;edgetoedge&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-results&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sep&quot;</span>&gt;</span>Results<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>                
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<ul>
<li>on line 1, we have the id &#8220;search&#8221;.  This corresponds to the href we had in the link on the home page.</li>
<li>on lines 2-5, we have the toolbar again, this time for the search page.  Of particular note here is the addition of the back button.  A div with class &#8220;back&#8221; is another special case for jQTouch.  It will automatically be linked back to the previous page.</li>
<li>on lines 6-8, we have another familiar face, the unordered button list.  However, this time we have an input with a special name &#8220;search-text&#8221;.  In jQTouch, any text input element that has a name that starts with &#8220;search&#8221; will act as a search input.</li>
<li>on lines 9-11, we have another unordered list of buttons, this time using a different visual class &#8220;edgetoedge&#8221;.  In this list we have one item, a separator (class &#8220;sep&#8221;), that we use to label the list as our results.  Later on, in part 2, we&#8217;ll place our search results in this list.</li>
</ul>
<p>After we add this code to our <code>index.html</code> file, let&#8217;s reload the web page in our iPhone or iPad and try clicking the Search button again.  You&#8217;ll see it brings you to a screen like this:</p>
<p><a href="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/04/search.jpg" rel="lightbox[1371]"><img src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/04/search.jpg" alt="" title="search" width="320" height="480" class="aligncenter size-full wp-image-1409" /></a></p>
<p>You&#8217;ll also notice that if you click the &#8220;Back&#8221; button at the top, it&#8217;ll bring you back to the &#8220;home&#8221; page.</p>
<h3>Conclusion</h3>
<p>In this tutorial we made a jQTouch UI that looks like an iPhone app.  We touched on a few jQTouch concepts:</p>
<ul>
<li>Pages are linked together using ids.</li>
<li>All jQTouch pages have toolbars (which is a div with class &#8220;toolbar&#8221;).  The toolbar contains an h1 element that represents the title of the page and two optional buttons, one of which can be a back button by using the class &#8220;back&#8221;.
<li>Lists of buttons are created by using unordered lists (&#8220;ul&#8221; elements) with special classes like &#8220;rounded&#8221; and &#8220;edgetoedge&#8221;.</li>
<li>Text input elements will act differently based on their name attributes.  For example, a search text input element is defined as such by having a &#8220;search&#8221; prefix in it&#8217;s name attribute.</li>
</ul>
<p>However this is only scratching the surface of jQTouch.  If you want to learn more I highly recommend your check out the <code>demos</code> directory in the jQTouch distribution.  There is also <a href="http://code.google.com/p/jqtouch/">a Google Code page</a> that has a Wiki and links to other tutorials.</p>
<h3>What Now?</h3>
<p>With respect to the UI, we&#8217;re done.  All that remains to be done now is to write a PHP script that we&#8217;ll query with an jQuery AJAX call.  <a href="http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/">This is covered in part 2 of this tutorial</a>.</p>
<h3>Source</h3>
<p>The source code I have written in this article is public domain, and you are free to do what you will with it.  Here is <a href="http://cdmckay.org/files/jqtouch-ajax-search.zip">a zip of the source, along with the required jQTouch source</a>.  It should work pretty much right out of the box.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Convert PHP date format string to .NET DateTime format string</title>
		<link>http://cdmckay.org/blog/2009/07/02/convert-php-date-format-string-to-net-datetime-format-string/</link>
		<comments>http://cdmckay.org/blog/2009/07/02/convert-php-date-format-string-to-net-datetime-format-string/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 20:13:09 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[php .net date datetime dateformat table]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=612</guid>
		<description><![CDATA[I recently had to convert some JavaScript code I&#8217;d written that used a PHP-style date format string to a .NET DateTime-style date format string. It would&#8217;ve been a helluva lot faster to have had some sort of conversation table. After a little searching on Google, I found nothing so I decided to make one myself. [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to convert some JavaScript code I&#8217;d written that used a <a href="http://php.net/manual/en/function.date.php">PHP-style date format string</a> to a <a href="http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx">.NET DateTime-style date format string</a>.</p>
<p>It would&#8217;ve been a helluva lot faster to have had some sort of conversation table.  After a little searching on Google, I found nothing so I decided to make one myself.  Behold, the fruits of my labour!</p>
<p><span id="more-612"></span></p>
<p>To use a PHP date format string:<br />
<code>$time = date("g:i a")</code></p>
<p>To use a .NET date format string (in C#):<br />
<code>string time = DateTime.Now.ToString("h:mm tt")</code></p>
<table>
<tr>
<th colspan="3"><strong>Day</strong></th>
</tr>
<tr>
<th>PHP date()</th>
<th>DateTime</th>
<th>Example</th>
</tr>
<tr>
<td>d</td>
<td>dd</td>
<td><em>01</em> to <em>31</em></td>
</tr>
<tr>
<td>D</td>
<td>ddd</td>
<td><em>Mon</em> through <em>Sun</em></td>
</tr>
<tr>
<td>j</td>
<td>d</td>
<td><em>1</em> to <em>31</em></td>
</tr>
<tr>
<td>l&nbsp;(lowercase&nbsp;L)</td>
<td>dddd</td>
<td><em>Sunday</em> through <em>Saturday</em></td>
</tr>
<tr>
<td>N</td>
<td>-</td>
<td><em>1</em> (for Monday) through <em>7</em> (for Sunday)</td>
</tr>
<tr>
<td>S</td>
<td>-</td>
<td><em>st</em>, <em>nd</em>, <em>rd</em> or <em>th</em></td>
</tr>
<tr>
<td>w</td>
<td>DateTime#DayOfWeek</td>
<td>0 (for Sunday) through 6 (for Saturday)</td>
</tr>
<tr>
<td>z</td>
<td>DateTime#DayOfYear&nbsp;-&nbsp;1</td>
<td><em>0</em> through <em>365</em></td>
</tr>
</table>
<table>
<tr>
<th colspan="3"><strong>Week</strong></th>
</tr>
<tr>
<th>PHP date()</th>
<th>DateTime</th>
<th>Example</th>
</tr>
<tr>
<td>W</td>
<td>-</td>
<td><em>42</em> (the 42nd week in the year)</td>
</tr>
</table>
<table>
<tr>
<th colspan="3"><strong>Day</strong></th>
</tr>
<tr>
<th>PHP date()</th>
<th>DateTime</th>
<th>Example</th>
</tr>
<tr>
<td>F</td>
<td>MMMM</td>
<td><em>January</em> through <em>December</em></td>
</tr>
<tr>
<td>m</td>
<td>MM</td>
<td><em>01</em> through <em>12</em></td>
</tr>
<tr>
<td>M</td>
<td>MMM</td>
<td><em>Jan</em> through <em>Dec</em></td>
</tr>
<tr>
<td>n</td>
<td>M</td>
<td><em>1</em> through <em>12</em></td>
</tr>
<tr>
<td>t</td>
<td>DateTime#DaysInMonth()</td>
<td><em>28</em> through <em>31</em></td>
</tr>
</table>
<table>
<tr>
<th colspan="3"><strong>Year</strong></th>
</tr>
<tr>
<th>PHP date()</th>
<th>DateTime</th>
<th>Example</th>
</tr>
<tr>
<td>L</td>
<td>DateTime#IsLeapYear()</td>
<td><em>1</em> if it is a leap year, <em>0</em> otherwise</td>
</tr>
<tr>
<td>o</td>
<td>yyyy (?)</td>
<td>ISO-8601 year number</td>
</tr>
<tr>
<td>Y</td>
<td>yyyy</td>
<td><em>1999</em> or <em>2003</em></td>
</tr>
<tr>
<td>y</td>
<td>yy</td>
<td><em>99</em> or <em>03</em></td>
</tr>
</table>
<table>
<tr>
<th colspan="3"><strong>Time</strong></th>
</tr>
<tr>
<th>PHP date()</th>
<th>DateTime</th>
<th>Example</th>
</tr>
<tr>
<td>a</td>
<td>-</td>
<td><em>am</em> or <em>pm</em></td>
</tr>
<tr>
<td>A</td>
<td>tt</td>
<td><em>AM</em> or <em>PM</em></td>
</tr>
<tr>
<td>B</td>
<td>-</td>
<td><em>000</em> through <em>999</em></td>
</tr>
<tr>
<td>g</td>
<td>h</td>
<td><em>1</em> through <em>12</em></td>
</tr>
<tr>
<td>G</td>
<td>H</td>
<td><em>0</em> through <em>23</em></td>
</tr>
<tr>
<td>h</td>
<td>hh</td>
<td><em>01</em> through <em>12</em></td>
</tr>
<tr>
<td>H</td>
<td>HH</td>
<td><em>00</em> through <em>23</em></td>
</tr>
<tr>
<td>i</td>
<td>mm</td>
<td><em>00</em> to <em>59</em></td>
</tr>
<tr>
<td>s</td>
<td>ss</td>
<td><em>00</em> through <em>59</em></td>
</tr>
<tr>
<td>u</td>
<td>fffff</td>
<td><em>54321</em></td>
</tr>
</table>
<table>
<tr>
<th colspan="3"><strong>Time Zone</strong></th>
</tr>
<tr>
<th>PHP date()</th>
<th>DateTime</th>
<th>Example</th>
</tr>
<tr>
<td>e</td>
<td>-</td>
<td><em>UTC</em>, <em>GMT</em>, <em>Atlantic/Azores</em></td>
</tr>
<tr>
<td>I&nbsp;(capital&nbsp;i)</td>
<td>DateTime#IsDaylightSavingTime()</td>
<td><em>1</em> if Daylight Saving Time, <em>0</em> otherwise</td>
</tr>
<tr>
<td>O</td>
<td>-</td>
<td><em>+0200</em></td>
</tr>
<tr>
<td>P</td>
<td>zzz</td>
<td><em>+02:00</em></td>
</tr>
<tr>
<td>T</td>
<td>-</td>
<td><em>EST</em>, <em>MDT</em></td>
</tr>
<tr>
<td>Z</td>
<td>-</td>
<td><em>-43200</em> through <em>50400</em></td>
</tr>
</table>
<table>
<tr>
<th colspan="3"><strong>Full Date/Time</strong></th>
</tr>
<tr>
<th>PHP date()</th>
<th>DateTime</th>
<th>Example</th>
</tr>
<tr>
<td>c</td>
<td>-</td>
<td>2004-02-12T15:19:21+00:00</td>
</tr>
<tr>
<td>r</td>
<td>-</td>
<td>Thu, 21 Dec 2000 16:01:07 +0200</td>
</tr>
<tr>
<td>U</td>
<td>-</td>
<td>Seconds since the Unix Epoch</td>
</tr>
</table>
<p><em>(Please note that just because I have no entry for the DateTime version of one of the PHP date strings, it doesn&#8217;t mean you can&#8217;t get that information using .NET easily.  It just means that it&#8217;s not in DateTime or not a one-liner.)</em></p>
<p>This table highlights some pretty interesting differences between the .NET API (i.e. a carefully designed API) and the PHP API (i.e. an ad-hoc API).  For example, if I asked you what <code>g</code> stood for in a PHP date format string, would you be able to say (without looking at a chart) &#8220;hours with no leading zeroes?&#8221;</p>
<p>What&#8217;s cool about the .NET API is that they try to make the format strings mean something.  For example, <code>d</code> means &#8220;day number with no leading zeroes&#8221;, <code>dd</code> means &#8220;day number with leadings zeros&#8221;, <code>ddd</code> means &#8220;short form day name&#8221;, <code>dddd</code> means &#8220;long form day name&#8221;.  That is, as you add d&#8217;s, you get progressive larger date string as a results.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/07/02/convert-php-date-format-string-to-net-datetime-format-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Consuming a J2EE Web Service with PHP SOAP</title>
		<link>http://cdmckay.org/blog/2009/03/05/consuming-a-j2ee-web-service-with-php-soap/</link>
		<comments>http://cdmckay.org/blog/2009/03/05/consuming-a-j2ee-web-service-with-php-soap/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 22:39:59 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[nusoap]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsdl]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=303</guid>
		<description><![CDATA[I recently had to do some work for a company which involved using PHP to consume a J2EE-based WSDL web service. The project was fairly straightforward, involving only a one-way call to the web service, i.e. no result needed to be collected. What is WSDL? Before we start, let&#8217;s talk a bit about what WSDL [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to do some work for a company which involved using <a href="http://www.php.net">PHP</a> to consume a <a href="http://java.sun.com/javaee/">J2EE</a>-based <a href="http://www.w3.org/TR/wsdl">WSDL</a> web service.  The project was fairly straightforward, involving only a one-way call to the web service, i.e. no result needed to be collected.</p>
<p><span id="more-303"></span></p>
<h3>What is WSDL?</h3>
<p>Before we start, let&#8217;s talk a bit about what <a href="http://en.wikipedia.org/wiki/Web_Services_Description_Language">WSDL</a> is.  <em>(Warning: I am very new to WSDL, SOAP and web services in general, so feel free to correct me if I get any of these details wrong).</em></p>
<p>WSDL stands for Web Service Description Language.  As the name suggests, it is an XML language used for describing web services.  By describe, I mean a WSDL file contains information like the functions that a client can call, and data types it uses, and what exceptions it might throw.  This is useful for clients that might contact the web service.  Exactly why that information is useful will become evident in examples below.</p>
<h3>What is SOAP?</h3>
<p>At one point, a long time ago, in a web far far away, <a href="http://en.wikipedia.org/wiki/SOAP_(protocol)">SOAP</a> stood for Simple Object Access Protocol.  Now it stands for nothing.  SOAP is a protocol for exchanging structured information on the web.  Like WSDL, SOAP uses XML.  In fact, SOAP is a protocol often used to exchange WSDL information, although one can also use straight HTTP to do it as well.</p>
<p>All we really need to know (right now) is that a SOAP library permits us to communicate with a WSDL web service that has SOAP bindings (which would be almost any we would encounter in the wild).</p>
<h3>Which library/module to use?</h3>
<p>In order to determine which library to use, I did some googling.  What I found were two options: the <a href="http://sourceforge.net/projects/nusoap/">NuSOAP</a> library and the <a href="http://ca.php.net/manual/en/book.soap.php">PHP SOAP</a> module.</p>
<p>So which one should we use? I initially leaned towards NuSOAP due to the fact that it was an external library, and thus would probably work with almost any PHP installation.  Unfortunately, after perusing the NuSOAP Sourceforge website, I noticed the library hadn&#8217;t been updated since 2005 (a bad sign).  In fact, after further reading, I discovered that, as of PHP 5, the NuSOAP namespace also conflicted with the PHP SOAP module namespace (and hadn&#8217;t yet been fixed).  As a result, I decided to go with the PHP SOAP module.</p>
<h3>The Code</h3>
<p>The first thing anyone needs to do when consuming a WSDL service with PHP is to create a new client object.  By creating a client object, PHP basically reads the server&#8217;s WSDL file and determines which functions are available to us.  A SoapClient is created using the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #666666; font-style: italic;">// The path to the WSDL web service.</span>
  <span style="color: #000088;">$wsdl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://path/to/webservice.wsdl&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// The connection options.</span>
  <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'features'</span> <span style="color: #339933;">=&gt;</span> 
    SOAP_USE_XSI_ARRAY_TYPE <span style="color: #339933;">+</span> SOAP_SINGLE_ELEMENT_ARRAYS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Create a new SoapClient.</span>
  <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapClient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wsdl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;SoapClient creation successful&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So what&#8217;s going on at line 7?  Those two features are basically used to make the SoapClient act as we&#8217;d expect.  For example, if you don&#8217;t include the SOAP_USE_XSI_ARRAY_TYPE feature, then you&#8217;ll find you get a lot of casting exceptions when dealing with a Java-based webservice that uses any complex data structures.  If you don&#8217;t include the SOAP_USE_XSI_ARRAY_TYPE feature, then PHP will turn 1-element arrays return types into objects, which is not what you want 90% of the time and often gives confusing results.</p>
<p>Now let&#8217;s actually consume a web service.  Let&#8217;s pretend we&#8217;re using the J2EE service provided by this <a href="http://java.sun.com/developer/technicalArticles/J2EE/j2ee_ws/">article</a>.  Here are the contents of the WSDL file from that article:</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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;definitions</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyFirstService&quot;</span> <span style="color: #000066;">targetNamespace</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span> <span style="color: #000066;">xmlns:tns</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/wsdl/&quot;</span> <span style="color: #000066;">xmlns:xsd</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:soap</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/wsdl/soap/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;types</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFace_add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;int_1&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:int&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;int_2&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:int&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/message<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFace_addResponse&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;result&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:int&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/message<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;portType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFace&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span> <span style="color: #000066;">parameterOrder</span>=<span style="color: #ff0000;">&quot;int_1 int_2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;tns:MathFace_add&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;output</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;tns:MathFace_addResponse&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/operation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/portType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFaceBinding&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;tns:MathFace&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:binding</span> <span style="color: #000066;">transport</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/soap/http&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;rpc&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:operation</span> <span style="color: #000066;">soapAction</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:body</span> <span style="color: #000066;">encodingStyle</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;</span> </span>
<span style="color: #009900;">		    <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;encoded&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/input<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;output<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:body</span> <span style="color: #000066;">encodingStyle</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;</span> </span>
<span style="color: #009900;">		    <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;encoded&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/output<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/operation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyFirstService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;port</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFacePort&quot;</span> <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;tns:MathFaceBinding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:address</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">&quot;http://localhost:8080/math-service/math&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/port<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/definitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>What can we tell about the web service by looking at this file?  Well the first thing you should notice is lines 12-14.  In those lines, the web service defines an operation (&#8220;add&#8221;) that takes two arguments (&#8220;int_1 int_2&#8243;).  As a result, we know that the web service exposes an add operation that takes two integer arguments.  Let&#8217;s use that knowledge to invoke the add operation in PHP using the SOAP module:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #666666; font-style: italic;">// The path to the WSDL web service.</span>
  <span style="color: #000088;">$wsdl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://localhost:8080/math-service/math?wsdl&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// The connection options.</span>
  <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'features'</span> <span style="color: #339933;">=&gt;</span> 
    SOAP_USE_XSI_ARRAY_TYPE <span style="color: #339933;">+</span> SOAP_SINGLE_ELEMENT_ARRAYS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Create a new SoapClient.</span>
  <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapClient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wsdl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;SoapClient creation successful&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Call the add operation.</span>
  <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// This should print 4.</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$response</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>How&#8217;d the PHP object $client know to have an add method?  It simply read the WSDL file the same way we did, and realized that the web service exposed that operation.</p>
<p>As you can see, consuming a J2EE web service from a PHP client is not too complicated.  Once you understand the rudiments of a WSDL file and the PHP SOAP module, calling remote functions becomes a snap.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/03/05/consuming-a-j2ee-web-service-with-php-soap/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

