<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>techPortal</title>
	
	<link>http://techportal.ibuildings.com</link>
	<description>Tutorials for better PHP</description>
	<lastBuildDate>Tue, 20 Jul 2010 19:20:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ibuildingstechportal" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="ibuildingstechportal" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Creating Pluggable Applications Using Data Sourcing</title>
		<link>http://techportal.ibuildings.com/2010/07/12/creating-pluggable-applications-using-data-sourcing/</link>
		<comments>http://techportal.ibuildings.com/2010/07/12/creating-pluggable-applications-using-data-sourcing/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 09:41:27 +0000</pubDate>
		<dc:creator>Ivo Jansch</dc:creator>
				<category><![CDATA[Planet PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[data sourcing]]></category>
		<category><![CDATA[federation]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[SOAP]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=2145</guid>
		<description><![CDATA[The first versions of most projects are self-contained applications. They work as-is, without any connection to other applications. It often isn&#8217;t until a later release that there is focus on interoperability. They build import/export functionality into their applications or add webservices that allow other applications to interact with it. While this is an important steps [...]]]></description>
			<content:encoded><![CDATA[<p>The first versions of most projects are self-contained applications. They work as-is, without any connection to other applications. It often isn&#8217;t until a later release that there is focus on interoperability. They build import/export functionality into their applications or add webservices that allow other applications to interact with it.</p>
<p>While this is an important steps toward application interoperability, there is often an important step missing. Most interoperable applications lack one final feature that allows full seamless integration: <strong>data sourcing</strong>, or the ability to get the data it needs from elsewhere.</p>
<p><span id="more-2145"></span></p>
<p>With data sourcing, we are not just importing data into our applications, we are using outside sources as the source for that data without creating redundancy. A simple example is the data sourcing of the user information. Most applications have their own user table. Applications that feature data sourcing of users make it possible to tell the system to get the user data not from its internal database but from a different source, for example the database of another application, an LDAP server or a web service that provides the user data. In the case of user data, if you have five applications that each have a database of users, it would be a lot simpler to integrate those applications if you could use one of them as the master source for the user data and configure the others to refer to it. </p>
<p>The principle is applicable to more than just users. Groups comes to mind (particularly groups within an organization that you may want to use within your applications), and friends is another common topic (aren&#8217;t you tired of befriending all your friends on every new social website?). In the case of ecommerce systems it would be great if you could use data sourcing to get the actual product data from different systems. <a href="http://www.magentocommerce.com/">Magento</a>, the popular ecommerce application, has product import/export functionality, but there is no easy way to tell it to connect to a web service to get the data for products instead of looking at its own database. This makes it hard to plug Magento as an ecommerce module into a larger system; most implementations you will find have Magento at its core and other, more flexible, systems plugging into it.</p>
<p>Data sourcing can help applications such as Magento reach a wider audience and especially help it be used in enterprise scenarios where there are many components that together form a big system. </p>
<p>The concept of data sourcing is comparable to <a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection</a>; instead of hardwiring the dependencies within the software, you tell it its dependencies so that at runtime it can connect to the correct components and get its data.</p>
<h2>Two Flavours of Data Sourcing</h2>
<p>When you want to use data sourcing, there are in essence two ways to do this.</p>
<h3>Synchronisation</h3>
<p>The first one is <strong>synchronisation</strong>. This means that the data is still local to the application, but it is (periodically or on the fly) synchronized with external applications. For the application being plugged into a system, this generally means that hardly any modifications are necessary; a script needs to be written that simply synchronises the data between the sources.</p>
<p>While this works in some situations, it is undesirable in most situations. If you have multiple applications you will end up with multiple copies of the data, and run the risk of having data out of sync between the sources. It can also lead to ownership or privacy issues. One application should be the owner of a data set; if that data set is exported to other applications then you lose a certain amount of control over it.</p>
<h3>Federation</h3>
<p>The better option is <strong>federation</strong>. Federation basically means that you will get the data from its source when you need it. You could still cache for performance reason but there is no mass synchronisation going on. This is the method we will be looking at in the rest of this article, as it is the one that is the most interesting, but also the one that requires work in the applications that want to make use of federation.</p>
<h2>Implementing Data Sourcing</h2>
<p>Imagine you have built an application that shows you a person&#8217;s wishlist. You may have a query in there somewhere that joins the wishlist table with the user table and a few auxiliary tables with categorisation information. If the application is built like that, and you decide to install the application within an environment with multiple applications, it will be hard to make the application use user accounts from another application; you will have to not only rewrite all the queries, you will also have to find a way to connect the application data with the external user accounts. There are a few things that will help you make your application ready for data sourcing, so let&#8217;s take a look at them.</p>
<h3>Models</h3>
<p>If your application is set up according to the Model/View/Controller (MVC) paradigm, then your business logic is already isolated within your models. This will make it easier to source the data from outside sources. It&#8217;s a matter of taking the user model and changing that to get the users from elsewhere. Using models within an application is a recommendation that I would make regardless of whether you plan on doing data sourcing, but it is particularly helpful when abstracting data away from your core application. Here is a simple code snippet that illustrates the idea, without giving detail:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> UserModel
<span style="color: #009900;">&#123;</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> getUser<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
       <span style="color: #666666; font-style: italic;">// Classic db code replaced by external data source</span>
       <span style="color: #666666; font-style: italic;">// return db::getrows(&quot;select * from users where id=:1&quot;, $id); </span>
&nbsp;
       <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSoapClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this example, the application will no longer fetch its data from the database, but use a SOAP client to retrieve the data. Of course it is very easy if the SOAP server has the exact same interface as our model, but that will rarely be the case when you&#8217;re talking to external services.   If it differs, you may have to do some parameter and result set transformations, but you get the idea.</p>
<h3>Protocol Abstraction</h3>
<p>In the above example we are connecting to a SOAP service, which allows us to get the users from another application if it has a SOAP service, but what happens if your application is used in a scenario where there is no SOAP server, or the SOAP server uses a different set of methods? If we want to be flexible, we have to abstract the data source from our model. This is where the &#8216;Data Mapper&#8217; design pattern, presented by Martin Fowler in his book <a href="http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420">Patterns of Enterprise Application Architecture</a>, comes in handy. This pattern abstracts the data source away from the model. Our above example, adapted to include this idea, could look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> UserModel
<span style="color: #009900;">&#123;</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> getUser<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</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;">getDataMapper</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You could then provide several data mapper implementations for all the external sources your application supports:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> DataMapper_User_Mysql implements DataMapper_User <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> DataMapper_User_SomeSOAPService 
                         <span style="color: #000000; font-weight: bold;">extends</span> DataMapper_User_SOAP_Abstract 
                         implements DataMapper_User <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note the use of &#8216;SomeSoapService&#8217; in the name and not just &#8216;SOAP&#8217;. SOAP is a standard but every SOAP service can implement their own methods, so we would have to have a separate mappers for different services. To avoid reinventing the wheel across different SOAP services we can derive from a general abstract SOAP class that takes care of the SOAP bits that are standard across all SOAP services. Later we will see how REST and the use of standards make this easier.</p>
<h3>REST</h3>
<p>REST is a nicer protocol than SOAP because it&#8217;s not only easier to implement, it has a lot less overhead and is easier to standardize. Because REST basically uses resource urls and the HTTP verbs to operate on them, they make data sourcing easier. For example, consider the following resource urls: </p>
<ul>
<li>http://example.com/users</li>
<li>http://example.com/users/42</li>
</ul>
<p></p>
<p>The first resource is a collection of users. We can retrieve the users using an HTTP GET request, we can add users using HTTP PUT, etc. The second resource is an individual user, which we can retrieve by performing a GET request, delete by performing a DELETE request etc.</p>
<p>Applying this to your application might mean that in the end, all you need to do is add some configuration into your application, rather than needing to modify code, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$datasources</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'userservice'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'SomePeopleService'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'groupservice'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'SomeGroupService'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Your REST data mapper then performs simple REST calls to retrieve it&#8217;s data through these services. Of course, you would still have to know what results to expect and how to map the results to your specific implementation, so you still might end up using multiple REST data mappers. The use of finer grained standards helps here. REST makes things easy; knowing that the response will be JSON will make things even easier; knowing that the JSON response is formatted according to the <A href="http://code.google.com/apis/opensocial/">OpenSocial</a> standard makes it peanuts. Which brings us to the final topic.</p>
<h2>Standards</h2>
<p>Federating access to applications for centrally managed users is very common. Large organisations deal with this all the time and nowadays, with so many social networking applications, you can see most websites use it. Services allow you to login using your Twitter or Facebook account for example. </p>
<p>Standards have been created to make it easy to work with centrally managed users. Here are a few that will be interesting for you to look at if you are looking to improve your applications in this area:</p>
<ul>
<li><a href="http://openid.net">OpenID</a>; a standard for having users log in using an account from one provider to access another provider. In essence it allows users to have one username/password for multiple services. Zend Devzone has a <a href="http://devzone.zend.com/article/3581">nice tutorial on OpenID</a> to get you started.</p>
<li><a href="http://oauth.net/">OAuth</a>; OAuth is a protocol designed to handle the actual authentication process across applications. OAuth allows an application to grant access to particular resources to an identity that was provided by another web site. There is a <a href="http://code.google.com/p/oauth-php/">library on google code</a> to make it easier to integrate oauth in your applications.
<li><a href="http://en.wikipedia.org/wiki/Security_Assertion_Markup_Language">SAML</a>; a more enterprise grade way to deal with authenticating users, that includes things such as identify verification and digital signatures so services know that the person that&#8217;s logging in is actually the person they think they are dealing with. If you want to work with SAML, you will want to look at <a href="http://rnd.feide.no/simplesamlphp">simpleSAMLphp</a>, which implements the most important parts of the SAML protocol in a relatively easy to understand PHP wrapper.
<li><a href="http://www.opensocial.org/">OpenSocial</a>; a standard targeted at social networking, which provides interoperability for users, groups, status updates etc. The <a href="http://code.google.com/p/opensocial-php-client/">OpenSocial PHP client</a> on google code is a good starting point.
</ul>
<p></p>
<p>For users, groups, privileges and general user data there are plenty of standards to choose from, and all of them have PHP implementations that can help get you started. For other data you may want to develop your own protocols and interfaces, and I hope to have given you enough information to get you started building applications that can get their data from external sources. If more applications are built this way, then the web will become much more pluggable!</p>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=Creating%20Pluggable%20Applications%20Using%20Data%20Sourcing&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Creating%20Pluggable%20Applications%20Using%20Data%20Sourcing%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F&amp;t=Creating%20Pluggable%20Applications%20Using%20Data%20Sourcing" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F&amp;title=Creating%20Pluggable%20Applications%20Using%20Data%20Sourcing&amp;bodytext=The%20first%20versions%20of%20most%20projects%20are%20self-contained%20applications.%20They%20work%20as-is%2C%20without%20any%20connection%20to%20other%20applications.%20It%20often%20isn%27t%20until%20a%20later%20release%20that%20there%20is%20focus%20on%20interoperability.%20They%20build%20import%2Fexport%20functionality%20i" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F&amp;title=Creating%20Pluggable%20Applications%20Using%20Data%20Sourcing" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F&amp;title=Creating%20Pluggable%20Applications%20Using%20Data%20Sourcing&amp;notes=The%20first%20versions%20of%20most%20projects%20are%20self-contained%20applications.%20They%20work%20as-is%2C%20without%20any%20connection%20to%20other%20applications.%20It%20often%20isn%27t%20until%20a%20later%20release%20that%20there%20is%20focus%20on%20interoperability.%20They%20build%20import%2Fexport%20functionality%20i" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F&amp;title=Creating%20Pluggable%20Applications%20Using%20Data%20Sourcing&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=The%20first%20versions%20of%20most%20projects%20are%20self-contained%20applications.%20They%20work%20as-is%2C%20without%20any%20connection%20to%20other%20applications.%20It%20often%20isn%27t%20until%20a%20later%20release%20that%20there%20is%20focus%20on%20interoperability.%20They%20build%20import%2Fexport%20functionality%20i" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F&amp;title=Creating%20Pluggable%20Applications%20Using%20Data%20Sourcing" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F12%2Fcreating-pluggable-applications-using-data-sourcing%2F&amp;title=Creating%20Pluggable%20Applications%20Using%20Data%20Sourcing" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/07/12/creating-pluggable-applications-using-data-sourcing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Test Driven Challenge – Winners and Results</title>
		<link>http://techportal.ibuildings.com/2010/07/08/the-test-driven-challenge-%e2%80%93-winners-and-results/</link>
		<comments>http://techportal.ibuildings.com/2010/07/08/the-test-driven-challenge-%e2%80%93-winners-and-results/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 08:58:49 +0000</pubDate>
		<dc:creator>Ivo Jansch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[competition]]></category>
		<category><![CDATA[minification]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=2168</guid>
		<description><![CDATA[In May and June we ran another one of our PHP coding contest. We love to play with PHP and offer people a chance to show off their coding skills in exchange for prizes! This time the contest revolved around creating a class that satisfies a given unit test case. The test case contained tests [...]]]></description>
			<content:encoded><![CDATA[<p>In May and June we ran another one of our PHP coding contest. We love to play with PHP and offer people a chance to show off their coding skills in exchange for prizes!</p>
<p>This time the contest revolved around creating a class that satisfies a <a href="http://ibuildings.com/downloads/challenge/test_driven_challenge.zip">given unit test case</a>. </p>
<p>The test case contained tests for 7 mathematical operations. The contestants did not only have to guess the actual operation, but also provide a class that implements the operation in as few lines as possible. </p>
<h3>Two contests for the prize of one</h3>
<h4>Contest 1 &#8211; Smallest solution adhering to decent coding practices</h4>
<p>The main contest was just that; because there was some discussion on twitter regarding the &#8216;fewest lines as possible&#8217; we updated the contest with a rule about &#8216;good coding practice&#8217; and we ignored coding standard issues; so a brace on the same line or on the next line was considered equally good, and we accepted both regular if constructs and the ternary operator. Some contestants removed all methods from their code and replaced it by a single __call construct, but our jury decided that that is not considered a decent coding practice. </p>
<p><span id="more-2168"></span></p>
<h4>Contest 2 &#8211; Smallest possible code size</h4>
<p>To cater to those that liked to disregard good coding practices and just create something as small as possible, we had a no-prize side contest for just fame and glory. The entries for this contest are very interesting, we&#8217;ll share the top 10 entries we received.</p>
<h3>The winners!</h3>
<p>As with our previous contests, we have 3 categories. From all the contestants in each category that had all the operations right and had the shortest code we randomly picked a winner. Basically all challenges could be written with one line of actual algorithm code, but you&#8217;d be surprised how many needed almost a hundred lines of code to write the class.</p>
<p>Here are our category winners:</p>
<ul>
<li>Senior category: David Frame from the United Kingdom</p>
<li>Medium category: Piotr Młocek from Poland
<li>Junior category: Luca Lancioni from Italy</ul>
<p>
<p>Congratulations for winning in your category!</p>
<p>The iPad prize was then randomly raffled amongst the three category winners, and the winner is&#8230;..</p>
<p><strong>David Frame</strong>. Congrats with your iPad!</p>
<p>The other 2 winners get a ticket to the <a href="http://www.phpconference.nl">Dutch PHP Conference 2011</a>.</p>
<h3>The solution</h3>
<p>Here is a sample class that implements all the algorithms. It&#8217;s not a shortest one but it is more clear what the algorithms were in this version:</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>
<span style="color: #000000; font-weight: bold;">class</span> NumberCruncher 
<span style="color: #009900;">&#123;</span> 
&nbsp;
  <span style="color: #666666; font-style: italic;">// Simple addition</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationA<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>	
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Simple multiplication</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationB<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Square / 2</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationC<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Factorial (10*9*8*7*6...)</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationD<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</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: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   
          <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> 
      <span style="color: #009900;">&#125;</span> 
      <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">operationD</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Fibonacci (0 1 1 2 3 5 8 13 21 34 55 89)</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationE<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</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: #000088;">$x</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">operationE</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">operationE</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Composite of B and C</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationF<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</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;">operationC</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">operationB</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Composite of D and E</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationG<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</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;">operationD</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">operationE</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The composite were the most difficult ones and were the ones where most contestants went wrong. operationF was also possible without composite by just providing the simple formula it was made up of, and that&#8217;s of course fine as well. operationD and operationE have variants that do not use recursion, but the ones that used recursion were generally shorter (even though it might come at a performance penalty, but that wasn&#8217;t a criterium in this contest).</p>
<h3>Remarkable</h3>
<p>Andy Thompson submitted a nice implementation of operationD. The array_product is a fairly unknown function in PHP, but it&#8217;s very suitable for this kind of operation:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">function</span> operationD<span style="color: #009900;">&#40;</span><span style="color: #000088;">$d</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">array_product</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$d</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></pre></div></div>

<p>Another interesting thing to notice is that there are multiple ways to calculate the <a href="http://en.wikipedia.org/wiki/Fibonacci_number">Fibonacci sequence</a>. Most contestants used a simple recursive formula like the one above. Matthias Steimle was one of the few contestants that used the so-called &#8216;<a href="http://en.wikipedia.org/wiki/Golden_ratio">Golden Ratio</a>&#8216; (a seemingly magic number that helps calculate the Fibonnaci Sequence and played an important role in Dan Brown&#8217;s Da Vinci Code). The golden ratio is the square root of 5, plus 1, divided by 2, and it calculates Fibonnaci numbers in Matthias&#8217; entry like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationE<span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
      <span style="color: #b1b100;">return</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span></pre></div></div>

<h3>The code minimization contest</h3>
<p>It is amazing how small a piece of code can get. We didn&#8217;t look for actual solutions to the algorithms; anything /<br />
To make it interesting, let&#8217;s walk through the top 10 entries starting with the longest. (Some linebreaks were added by yours truly to prevent horizontal scrolling).</p>
<h4>10. Peter Lindqvist (276 bytes)</h4>
<p>Here&#8217;s the number 10 entry:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #000000; font-weight: bold;">class</span> NumberCruncher<span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$p</span><span style="color: #339933;">=</span><span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">';'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'+=4;*=3;*=$r/2;=o($r);=f($r);*=$r*
4.5;=o($r)-f($r)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>eval<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$r=$a[0];$r'</span><span style="color: #339933;">.</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">65</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_callable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'f'</span><span style="color: #009900;">&#41;</span>?<span style="color: #0000ff;">';'</span><span style="color: #339933;">:</span><span style="color: #0000ff;">';function 
f($i){return$i&gt;1?f($i-1)+f($i-2):$i;}function o($i){return$i&lt;2?1:$i*o($i-1);}'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>return<span style="color: #000088;">$r</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>As you can see, minified code becomes quite unreadable, but in Peter&#8217;s entry you can still recognize the operations from the algorithm. Peter&#8217;s solution is based on using very short function names, a __call interceptor so you only need one function and a bit of eval&#8217;d code.</p>
<h4>9. Robert Gramm (271 bytes)</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000000; font-weight: bold;">class</span> NumberCruncher<span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000088;">$s</span><span style="color: #339933;">=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span><span style="color: #000088;">$s</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'A'</span>?<span style="color: #000088;">$x</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'B'</span>?<span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'C'</span>?<span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #000088;">$x</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'D'</span>?<span style="color: #000088;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">2</span>?<span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span><span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">D</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">-</span>
<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'E'</span>?<span style="color: #000088;">$x</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">2</span>?<span style="color: #000088;">$x</span><span style="color: #339933;">:</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">E</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">E</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'F'</span>?<span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #000088;">$x</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">9</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'G'</span>?<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">D</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">E</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Similar techniques, and slightly more efficient.</p>
<h4>8. Olivier Garcia (225 bytes)</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #000000; font-weight: bold;">class</span> NumberCruncher<span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #339933;">,</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$b</span><span style="color: #339933;">=</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'v+4 v*3 v*$v/2 
v&gt;1?$v*$this-&gt;D($v-1):1 v&gt;0?ceil(exp($v/2-1)):0 v*$v*4.5 this-&gt;D($v)-$this-&gt;E($v)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$v</span><span style="color: #339933;">=</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #990000;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'return$'</span><span style="color: #339933;">.</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">65</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">';'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h4>7. Paul Mitchell (214 bytes)</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #000000; font-weight: bold;">class</span> NumberCruncher<span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$f</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$n</span><span style="color: #339933;">=</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #000088;">$d</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;self::D(<span style="color: #006699; font-weight: bold;">$n</span>&quot;</span><span style="color: #339933;">;</span><span style="color: #000088;">$e</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;self::E(<span style="color: #006699; font-weight: bold;">$n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$a</span><span style="color: #339933;">=</span><span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;*<span style="color: #006699; font-weight: bold;">$n</span>*4.5 *0+<span style="color: #006699; font-weight: bold;">$d</span>)-<span style="color: #006699; font-weight: bold;">$e</span>) +4 *3 *<span style="color: #006699; font-weight: bold;">$n</span>/2 ?<span style="color: #006699; font-weight: bold;">$n</span>*<span style="color: #006699; font-weight: bold;">$d</span>-1):1 &lt;2?<span style="color: #006699; font-weight: bold;">$n</span>:<span style="color: #006699; font-weight: bold;">$e</span>-2)+<span style="color: #006699; font-weight: bold;">$e</span>-1)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\$</span>r=<span style="color: #006699; font-weight: bold;">$n</span>${a[ord(strrev(<span style="color: #006699; font-weight: bold;">$f</span>))%7]};&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>return<span style="color: #000088;">$r</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h4>6. Michiel Hakvoort (210 bytes)</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> NumberCruncher <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;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$t</span><span style="color: #339933;">=</span><span style="color: #990000;">debug_backtrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$f</span><span style="color: #339933;">=</span><span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'file'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#s\(\s*(-?\d+(?:\.\d+)?)#'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$f</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'line'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This entry deserves bonus points for being smaller than the previous entries, but still remain readable. This is the first entry we encounter in the top 10 that makes use of debug_backtrace to get information from the unittests, where previous entries used to still actually calculate the algorithms. So you can see things getting more dirty from here. :)</p>
<h4>5. James Andres (206 bytes)</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">gzinflate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'=~NÑ
~B@^PE^?Ed@Í¤´~^ÜLJ­·~ BT^L^E~[`w~MTü÷v´z~Z9ç^~F)~ZL^HíÒ&gt;ò~
RG¼Å¢*ùpWSÖOÔÒ´È~ZÆ~D÷^R:k~@C`^@Ú[~CÁ~Q¶~Eg^C*~H&amp;^@\y
b~B~]^[º&gt;9éìc^SÐq-^U%Sä~E~@&gt;^EÉ^\ØÿÝ£ÒI~U~T~H~TXlH~\gAg,ç×
¥@^F «Z0À^@ºëúÆ| ^OD~[^KÉéau~YñR¶^\µò~U5¦þ~E^A| ^_~Yn±qü^@'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A completely different approach. James zipped his code and used a runtime gzinflate and eval to unzip the code and run it! This makes it possible to pass the unit test using only 206 bytes! Arpad Ray also had an approach where he used zip, but since Arpad entered another entry that was even smaller than his zip version, only James&#8217; zip version made the top 10. (If you have trouble running the above code, I&#8217;m pretty sure WordPress will have ruined the character encoding, and I added newlines.)</p>
<h4>4. Joris van de Sande (154 bytes)</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #000000; font-weight: bold;">class</span> NumberCruncher<span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>static<span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>preg_match_all<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\(\s*(.+?)
\s*,/'</span><span style="color: #339933;">,</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'argv'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$m</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>return<span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a debatable entry, because Joris assumes there&#8217;s a command line argument, which depending on how you run the unit test may not be present. Joris brings us to a range of entries that is more than 25% smaller than the previous one.</p>
<h4>3. Devis Lucato (153 bytes)</h4>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #000000; font-weight: bold;">class</span> NumberCruncher<span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$t</span><span style="color: #339933;">=</span><span style="color: #990000;">debug_backtrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$l</span><span style="color: #339933;">=</span><span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">file</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#([-.\d]+)\s*,#'</span><span style="color: #339933;">,</span><span style="color: #000088;">$l</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>line<span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>return<span style="color: #000088;">$r</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>With a gain of just 1 byte over number 4, this code again cleverly uses debug_backtrace and an efficient file/preg_match combination.</p>
<h4>2. Tom van Looy (151 bytes)</h4>
<p>2 more bytes down!</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> <span style="color: #000000; font-weight: bold;">class</span> NumberCruncher<span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$f</span><span style="color: #339933;">,</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #b1b100;">return</span> 
<span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtok</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'('</span><span style="color: #339933;">,</span><span style="color: #990000;">strstr</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">join</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'Test.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$f</span>(<span style="color: #006699; font-weight: bold;">$p[0]</span>)&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</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: #0000ff;">','</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;">&#125;</span></pre></div></div>

<p>I do have to mention that this one didn&#8217;t run out of the box because Tom&#8217;s code assumes the test class is in the same directory. If you look at the testcase you&#8217;ll see that this is not the case. Since the technique is clear however it&#8217;s still a nice entry.</p>
<h4>1. Arpad Ray (133 bytes)</h4>
<p>And kudos to Arpad for completely squashing the competition by using 18 bytes less than the runner up. As you can see, the winning entry even fits in a tweet! Arpad managed to perfect the simplicity of parsing the testcase using debug_backtrace and a simple split.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span><span style="color: #000000; font-weight: bold;">class</span> NumberCruncher<span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$b</span><span style="color: #339933;">=</span><span style="color: #990000;">debug_backtrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$f</span><span style="color: #339933;">=</span><span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">file</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>return<span style="color: #339933;">+</span><span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'\('</span><span style="color: #339933;">,</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>line<span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Conclusion</h3>
<p>We had great fun running this contest, we hope you enjoyed it too. Keep an eye on <a href="http://ibuildings.com/challenge">http://ibuildings.com/challenge</a> or <a href="http://twitter.com/ibuildings">follow our Twitter account</a> to make sure you don&#8217;t miss the next one!</p>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=The%20Test%20Driven%20Challenge%20%E2%80%93%20Winners%20and%20Results&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=The%20Test%20Driven%20Challenge%20%E2%80%93%20Winners%20and%20Results%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F&amp;t=The%20Test%20Driven%20Challenge%20%E2%80%93%20Winners%20and%20Results" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F&amp;title=The%20Test%20Driven%20Challenge%20%E2%80%93%20Winners%20and%20Results&amp;bodytext=In%20May%20and%20June%20we%20ran%20another%20one%20of%20our%20PHP%20coding%20contest.%20We%20love%20to%20play%20with%20PHP%20and%20offer%20people%20a%20chance%20to%20show%20off%20their%20coding%20skills%20in%20exchange%20for%20prizes%21%0D%0A%0D%0AThis%20time%20the%20contest%20revolved%20around%20creating%20a%20class%20that%20satisfies%20a%20given%20" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F&amp;title=The%20Test%20Driven%20Challenge%20%E2%80%93%20Winners%20and%20Results" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F&amp;title=The%20Test%20Driven%20Challenge%20%E2%80%93%20Winners%20and%20Results&amp;notes=In%20May%20and%20June%20we%20ran%20another%20one%20of%20our%20PHP%20coding%20contest.%20We%20love%20to%20play%20with%20PHP%20and%20offer%20people%20a%20chance%20to%20show%20off%20their%20coding%20skills%20in%20exchange%20for%20prizes%21%0D%0A%0D%0AThis%20time%20the%20contest%20revolved%20around%20creating%20a%20class%20that%20satisfies%20a%20given%20" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F&amp;title=The%20Test%20Driven%20Challenge%20%E2%80%93%20Winners%20and%20Results&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=In%20May%20and%20June%20we%20ran%20another%20one%20of%20our%20PHP%20coding%20contest.%20We%20love%20to%20play%20with%20PHP%20and%20offer%20people%20a%20chance%20to%20show%20off%20their%20coding%20skills%20in%20exchange%20for%20prizes%21%0D%0A%0D%0AThis%20time%20the%20contest%20revolved%20around%20creating%20a%20class%20that%20satisfies%20a%20given%20" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F&amp;title=The%20Test%20Driven%20Challenge%20%E2%80%93%20Winners%20and%20Results" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F07%2F08%2Fthe-test-driven-challenge-%25e2%2580%2593-winners-and-results%2F&amp;title=The%20Test%20Driven%20Challenge%20%E2%80%93%20Winners%20and%20Results" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/07/08/the-test-driven-challenge-%e2%80%93-winners-and-results/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>DPC 2010: Sessions and Slides</title>
		<link>http://techportal.ibuildings.com/2010/06/14/dpc-2010-sessions-and-slides/</link>
		<comments>http://techportal.ibuildings.com/2010/06/14/dpc-2010-sessions-and-slides/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 14:07:06 +0000</pubDate>
		<dc:creator>Lorna Mitchell</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[dpc10]]></category>
		<category><![CDATA[dutch]]></category>
		<category><![CDATA[dutch php conference]]></category>
		<category><![CDATA[ibuildings]]></category>
		<category><![CDATA[joind.in]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[speakers]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=2107</guid>
		<description><![CDATA[The Dutch PHP Conference is over for another year &#8211; we had a wonderful time hosting you all in Amsterdam and we were delighted so many people were able to join us for 3 days of serious technical learning (with some beer on the side of course!). While the event itself has finished, the content [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://phpconference.nl">Dutch PHP Conference</a> is over for another year &#8211; we had a wonderful time hosting you all in Amsterdam and we were delighted so many people were able to join us for 3 days of serious technical learning (with some beer on the side of course!).  While the event itself has finished, the content will live on in our <a href="http://techportal.ibuildings.com/dpc-radio/">DPC Radio</a> podcast series, where we&#8217;ll be broadcasting audio from the main tracks at the conference through the year.  Plenty of the speakers have already uploaded their slides, read on for a breakdown of ratings and links to slides for those talks that have them &#8211; and don&#8217;t forget to rate all the talks you saw on <a href="http://joind.in/event/dpc10">joind.in</a>! (You can leave general event info too &#8211; click on the comments tab on the events page)</p>
<p><span id="more-2107"></span></p>
<table>
<tr>
<th>Talk</th>
<th>Speaker</th>
<th>Rating and Links</th>
</tr>
<tr>
<td><a href="http://joind.in/1525">Quality Assurance in PHP Projects</a><br /><img src="http://joind.in/inc/img/rating-5.gif"></td>
<td>Sebastian Bergmann </td>
<td><a href="http://www.slideshare.net/sebastian_bergmann/quality-assurance-in-php-projects-4462517">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1531">Zend Framework Application Patterns &#8211; Part 2</a><br /><img src="http://joind.in/inc/img/rating-5.gif"></td>
<td>Matthew Weier O&#8217;Phinney and Rob Allen </td>
<td><a href="http://www.slideshare.net/weierophinney/zend-framework-workshop-4480868">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1535">Premature Optimization Mistakes</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Ilia Alshanetsky </td>
<td><a href="http://ilia.ws/files/Dutch_PHP_Conference_2010_OPM.pdf">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1536">Technical Debt</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Elizabeth Naramore </td>
<td><a href="http://naramore.net/slides/DPC10-techdebt.pdf">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1756">Scrum in the Wild</a> (uncon)<br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Mike van Riel </td>
<td><a href="http://www.slideshare.net/mikevanriel/scrum-in-the-wild-at-dpc10">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1538">Working with Zend_Form</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Rob Allen </td>
<td><a href="http://akrabat.com/talks/#zendform">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1537">The Cake Is A Lie</a><br /><img src="http://joind.in/inc/img/rating-3.gif"></td>
<td>Sebastian Bergmann </td>
<td><a href="http://www.slideshare.net/sebastian_bergmann/the-cake-is-a-lie-4475889">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1539">The Art of Scalability</a><br /><img src="http://joind.in/inc/img/rating-3.gif"></td>
<td>Lorenzo Alberton </td>
<td><a href="http://www.alberton.info/talks/show/id/3">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1540">Graphics with Cairo</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Michael MacLean </td>
<td><a href="http://mgdm.net/talks/dpc10/cairo.pdf">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1541">PHP through the eyes of a hoster</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Thijs Feryn </td>
<td><a href="http://slidesha.re/cgELph">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1758">Multithreading in PHP</a> (uncon)<br /><img src="http://joind.in/inc/img/rating-3.gif"></td>
<td>Dimitriy Remerov </td>
<td><a href="http://www.slideshare.net/dimitriyremerov/multithreading-in-php">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1544">Database version control without pain</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Harrie Verveer </td>
<td><a href="http://tinyurl.com/dpc-dbversioncontrol">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1542">Writing Re-usable, RESTful Web Services with Zend Framework</a><br /><img src="http://joind.in/inc/img/rating-5.gif"></td>
<td>Matthew Weier O&#8217;Phinney Matthew Weier O&#8217;Phinney </td>
<td><a href="http://www.slideshare.net/weierophinney/building-restful-zend-framework-applications">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1547">Advanced Git</a><br /><img src="http://joind.in/inc/img/rating-5.gif"></td>
<td>David Soria Parra </td>
<td><a href="http://www.slideshare.net/segv/advanced-git">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1545">Testing untestable code</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Stephan Hochdörfer </td>
<td><a href="http://www.slideshare.net/shochdoerfer/dpc10-testing-untestable-code">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1549">Agile PHP Software Development</a><br /><img src="http://joind.in/inc/img/rating-2.gif"></td>
<td>Thorsten Rinne </td>
<td><a href="http://www.slideshare.net/mayflowergmbh/agile-php-software-development">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1770">Starting Continuous Integration, Why and How?</a> (uncon)<br /><img src="http://joind.in/inc/img/rating-5.gif"></td>
<td>Martin de Keijzer </td>
<td><a href="http://www.slideshare.net/Martin82/continuous-integration-why-and-how-dpc-uncon-quickprep-version">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1551">Stress-free deployment</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Rob Allen </td>
<td><a href="http://akrabat.com/talks/#sfd">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1553">Real World Dependency Injection</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Stephan Hochdörfer </td>
<td><a href="http://www.slideshare.net/shochdoerfer/dpc10-real-world-dependency-injection">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1651">Embracing Constraints with CouchDB</a><br /><img src="http://joind.in/inc/img/rating-5.gif"></td>
<td>David Zuelke </td>
<td><a href="http://www.slideshare.net/Wombert/embracing-constraints-with-couchdb">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1554">APC &#038; Memcache the High Performance Duo</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Ilia Alshanetsky </td>
<td><a href="http://ilia.ws/files/Dutch_PHP_Conference_APC_MEM2010.pdf">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1555">Where in the world: Geo data in PHP</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Marcus Deglos </td>
<td><a href="http://www.slideshare.net/manarth/where-in-the-world">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1558">Technical Writing 101</a><br /><img src="http://joind.in/inc/img/rating-5.gif"></td>
<td>Elizabeth Naramore </td>
<td><a href="http://naramore.net/slides/DPC10-tech-writing.pdf">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1556">In Search Of&#8230; Integrating Site Search</a><br /><img src="http://joind.in/inc/img/rating-5.gif"></td>
<td>Ian Barber </td>
<td><a href="http://www.slideshare.net/IanBarber/in-search-of">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1766">Content Driven ACL in the Model Layer (ZF)</a> (uncon)<br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Jeroen Keppens </td>
<td><a href="http://www.slideshare.net/jkeppens/content-driven-zendacl-in-the-model-layer">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1557">Design for Scalability with Hierarchical MVC</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Sam de Freyssinet </td>
<td><a href="http://www.slideshare.net/samsoir/design-for-scalability-with-hmvc">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1767">iPhone Apps With HTML5</a> (uncon)<br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Thorsten Rinne </td>
<td><a href="http://www.slideshare.net/mayflowergmbh/i-phone-apps-mit-html5">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1768">Building on Cairo</a> (uncon)<br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Michael MacLean </td>
<td><a href="http://mgdm.net/talks/dpc10/building-on-cairo.pdf">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1561">Domain NoSQL: Next Generation Models</a><br /><img src="http://joind.in/inc/img/rating-4.gif"></td>
<td>Matthew Weier O&#8217;Phinney </td>
<td><a href="http://www.slideshare.net/weierophinney/domain-nosql-next-generation-domain-modelling">Slides</a></td>
</tr>
<tr>
<td><a href="http://joind.in/1559">Reviewing PHP Code</a><br /><img src="http://joind.in/inc/img/rating-5.gif"></td>
<td>Sebastian Bergmann </td>
<td><a href="http://www.slideshare.net/sebastian_bergmann/php-code-review-4488307">Slides</a></td>
</tr>
</table>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=DPC%202010%3A%20Sessions%20and%20Slides&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=DPC%202010%3A%20Sessions%20and%20Slides%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F&amp;t=DPC%202010%3A%20Sessions%20and%20Slides" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F&amp;title=DPC%202010%3A%20Sessions%20and%20Slides&amp;bodytext=The%20Dutch%20PHP%20Conference%20is%20over%20for%20another%20year%20-%20we%20had%20a%20wonderful%20time%20hosting%20you%20all%20in%20Amsterdam%20and%20we%20were%20delighted%20so%20many%20people%20were%20able%20to%20join%20us%20for%203%20days%20of%20serious%20technical%20learning%20%28with%20some%20beer%20on%20the%20side%20of%20course%21%29.%20%20Whil" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F&amp;title=DPC%202010%3A%20Sessions%20and%20Slides" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F&amp;title=DPC%202010%3A%20Sessions%20and%20Slides&amp;notes=The%20Dutch%20PHP%20Conference%20is%20over%20for%20another%20year%20-%20we%20had%20a%20wonderful%20time%20hosting%20you%20all%20in%20Amsterdam%20and%20we%20were%20delighted%20so%20many%20people%20were%20able%20to%20join%20us%20for%203%20days%20of%20serious%20technical%20learning%20%28with%20some%20beer%20on%20the%20side%20of%20course%21%29.%20%20Whil" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F&amp;title=DPC%202010%3A%20Sessions%20and%20Slides&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=The%20Dutch%20PHP%20Conference%20is%20over%20for%20another%20year%20-%20we%20had%20a%20wonderful%20time%20hosting%20you%20all%20in%20Amsterdam%20and%20we%20were%20delighted%20so%20many%20people%20were%20able%20to%20join%20us%20for%203%20days%20of%20serious%20technical%20learning%20%28with%20some%20beer%20on%20the%20side%20of%20course%21%29.%20%20Whil" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F&amp;title=DPC%202010%3A%20Sessions%20and%20Slides" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F14%2Fdpc-2010-sessions-and-slides%2F&amp;title=DPC%202010%3A%20Sessions%20and%20Slides" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/06/14/dpc-2010-sessions-and-slides/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Web Services for PHP Developers</title>
		<link>http://techportal.ibuildings.com/2010/06/01/web-services-for-php-developers/</link>
		<comments>http://techportal.ibuildings.com/2010/06/01/web-services-for-php-developers/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 07:03:13 +0000</pubDate>
		<dc:creator>Lorna Mitchell</dc:creator>
				<category><![CDATA[Planet PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonrpc]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[sugarcrm]]></category>
		<category><![CDATA[webservice]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xmlrpc]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=2086</guid>
		<description><![CDATA[In this world of sharing data, increasing numbers of sites and applications are making information available over web services. Whether we are building a service as a feature of our own development, or pulling in the information published by others, we will need to understand the different service types and how to work with them [...]]]></description>
			<content:encoded><![CDATA[<p>In this world of sharing data, increasing numbers of sites and applications are making information available over web services.  Whether we are building a service as a feature of our own development, or pulling in the information published by others, we will need to understand the different service types and how to work with them in PHP.  This article aims to give you the tools to do just that.</p>
<p><span id="more-2086"></span></p>
<h3>Starting at the Beginning: HTTP</h3>
<p>HTTP (HyperText Transport Protocol) is the language of the web, the communication channel over which we send our data.  As web developers, we often don&#8217;t need to really notice it is there but for web services there are some bits and pieces we should be aware of so let us take a few moments to refresh our memories.</p>
<p>When we surf the Internet, we make a series of <strong>requests</strong>, and receive <strong>responses</strong> in return containing the data we asked for.  There is nothing special about web services; they work in exactly the same way except that the data in the response is usually marked up for machine-consumption rather than for a browser.  When we use a browser, we are aware of the URL that we are requesting and we know that forms send additional POST data.  We also observe the HTML response, by viewing the source of the rendered page in the browser (or using cURL, if you&#8217;re really hardcore!)</p>
<p>Most web developers are also aware that there are other headers available, controlling cookies, caching and so on, but for web services these form an integral part of the information that is getting sent to and fro between client and server. We observe the headers in one of a number of ways probably the simplest is to use <a href="http://curl.haxx.se/">cURL</a> from your command line to observe the traffic.  Here&#8217;s an example of a request to the <a href="http://www.google.co.uk"> google</a> homepage:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ curl <span style="color: #660033;">-I</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.google.co.uk
HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.1</span> <span style="color: #000000;">200</span> OK
Date: Sat, <span style="color: #000000;">29</span> May <span style="color: #000000;">2010</span> <span style="color: #000000;">10</span>:<span style="color: #000000;">39</span>:<span style="color: #000000;">35</span> GMT
Expires: <span style="color: #660033;">-1</span>
Cache-Control: private, max-age=<span style="color: #000000;">0</span>
Content-Type: text<span style="color: #000000; font-weight: bold;">/</span>html; <span style="color: #007800;">charset</span>=ISO-<span style="color: #000000;">8859</span>-<span style="color: #000000;">1</span>
Set-Cookie: <span style="color: #007800;">PREF</span>=<span style="color: #007800;">ID</span>=375fcb45b134bfca:<span style="color: #007800;">TM</span>=<span style="color: #000000;">1275129575</span>:<span style="color: #007800;">LM</span>=<span style="color: #000000;">1275129575</span>:<span style="color: #007800;">S</span>=g68f-bMGCmCJluRj; <span style="color: #007800;">expires</span>=Mon, <span style="color: #000000;">28</span>-May-<span style="color: #000000;">2012</span> <span style="color: #000000;">10</span>:<span style="color: #000000;">39</span>:<span style="color: #000000;">35</span> GMT; <span style="color: #007800;">path</span>=<span style="color: #000000; font-weight: bold;">/</span>; <span style="color: #007800;">domain</span>=.google.co.uk
Server: gws
X-XSS-Protection: <span style="color: #000000;">1</span>; <span style="color: #007800;">mode</span>=block
Transfer-Encoding: chunked</pre></div></div>

<p>Some particular headers are key when working with web services, here is an outline of some useful ones:</p>
<p><strong>Cookie</strong> (request) and <strong>Set-Cookie</strong> (response).  These control the cookies that the server gives to the client, and which the client provides on every request.  Most PHP sessions work via cookies, and these are handled invisibly by our browsers.  When we work with servers, we may have to manually receive, store and provide these when making requests and parsing responses.  Many web services are stateless, and so don&#8217;t use cookies, but some do use them and it is useful to be aware of these.  To be consuming the web from PHP is a bit of a conceptual shift, since usually we&#8217;re serving the web, but all the same principles apply.</p>
<p><strong>Status Codes</strong> (response).  Take a look at the headers shown in the response above: the first line shows &#8220;HTTP/1.1 200 OK&#8221;.  This tells us the version of HTTP that is in use (usually 1.1 although 1.0 does exist and is still used) and also the status of the response.  The status code is possibly the most important piece of information in the HTTP response header and this is something that will get a mention again and again in the course of this article.  The one shown here, 200, is good news and means that everything operated as expected.  The response codes are separated into classes and consist of three digits.  Anything starting with a 2 means everything was fine.  Something starting with a 3 indicates a redirect of some kind, with a 4 means there was a client error, and with a 5 there was a server error.  Some common status codes:</p>
<table>
<tr>
<th>Code&nbsp;&nbsp;</th>
<th>Meaning</th>
</tr>
<tr>
<td>200</td>
<td>OK</td>
</tr>
<tr>
<td>302</td>
<td>Found</td>
</tr>
<tr>
<td>301</td>
<td>Moved</td>
</tr>
<tr>
<td>401</td>
<td>Not Authorised</td>
</tr>
<tr>
<td>403</td>
<td>Forbidden</td>
</tr>
<tr>
<td>404</td>
<td>Not Found</td>
</tr>
<tr>
<td>500</td>
<td>Internal Server Error</td>
</tr>
</table>
<p>For a full list of status codes, there is a great reference <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes">on wikipedia</a> showing various ones (some quite frivolous, in particular look for HTTP 418)</p>
<p><strong>Accept</strong> (request) and <strong>Content-Type</strong> (response).  These headers control the kind of content that is included in the response.  The client, when making the request, can indicate what format it would like using the Accept header.  When responding, the server should set the Content-Type in the response header so that the client will know how to interpret the response.</p>
<p>Having the server include metadata about the response type is really useful and also means that we can use this to check that we got the response we expected.  When I run into problems with web services it can often be because in the event of an error, the service returns some default web output as text/html, whereas I have PHP code trying to decode it as XML or JSON &#8230; and of course my code then fails horribly because the response format isn&#8217;t what I was expecting!  Checking the content type of the response before decoding can help to warn us that there has been a problem that we may need to handle.</p>
<h3>The Power of HTTP</h3>
<p>In the example mentioned above, where checking the content type of a response before decoding can alert us to the problem, we nicely illustrated some of the main advantages of using the functionality given to us by HTTP.   If we don&#8217;t take the time to set the headers correctly, or we don&#8217;t inspect them, we may not be aware that there is a problem or in the worst case our code may just not be able to handle what happened elegantly.  Setting these headers correctly in a service, and checking them appropriately when receiving a response, can really help us to write robust services and consumers.  If we were to rely purely on information being in the body, as a request parameter or perhaps by decoding the response and then checking for a status code within it, we have lost some of the built-in functionality of HTTP.  It is, by design, an envelope protocol to help us understand what is inside without necessarily having to do the work to decode it, or to try to decode it and then fail.  Seeing an error status code, a zero content length header, or an unexpected content type will all help us to understand what we have received before we even try to unpack it, which is fantastic!</p>
<h3>RPC Services</h3>
<p>One of the most common, and in my opinion, easiest service types to relate to is the RPC (Remote Procedure Call).  These web services actually feel more like distributed libraries than any kind of new-fangled service-oriented technology &#8230; and that is because that is all they are!  We make a request to a given endpoint, passing a method name and some arguments, and we get the response back.  The easiest way to illustrate what this looks like in PHP is to look at an example.  <a href="http://flickr.com">Flickr</a> has a nice RPC service, so let us take a look at an example making a call to their API.</p>
<p>We&#8217;re going to call their search method, and look for all photos tagged with &#8220;ibuildings&#8221;.  Flickr has a fairly prescriptive request/response format, so <a href="http://www.flickr.com/services/api/request.xmlrpc.html">read</a> their <a href="http://www.flickr.com/services/api/response.xmlrpc.html">docs</a> if you are interested in understanding how this lot goes together.  Working with the flickr API is probably a whole article in itself so I&#8217;ll try to keep that aspect to a minimum in this post.</p>
<p>Here&#8217;s the code, using the <a href="http://uk2.php.net/manual/en/book.curl.php">PHP curl extension</a> to handle the requests for us.</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>
<span style="color: #666666; font-style: italic;">// build request format</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;?xml version=&quot;1.0&quot; ?&gt;&lt;methodCall /&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// method name</span>
<span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'methodName'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'flickr.photos.search'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// parameters list</span>
<span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'params'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'param'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'value'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'struct'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// add api key</span>
<span style="color: #000088;">$child</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'member'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'api_key'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'value'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'b3ed3314454a875f35d519900c819708'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// add tag to search for</span>
<span style="color: #000088;">$child</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$params</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'member'</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'tags'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'value'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'ibuildings'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// make request</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://api.flickr.com/services/xmlrpc/'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$response_xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now if we inspect $response_xml we find a list of photo tags of results that match (if you&#8217;re interested, the content of this is the same as <a href="http://www.flickr.com/search/?q=ibuildings&#038;s=rec">this flickr search</a>) and we could use these results to display on another site or as part of an application.</p>
<p>RPC services in general are a nice way to start working with web services &#8211; they are a good fit for most applications, and they are easy for us to understand since we already know very well how to work with functions and arguments.  They may not be the &#8220;coolest&#8221; technology around but they are simple and personally I think that counts for a lot!  Using RPC services also means that we can easily work with our existing code, in the event that we aren&#8217;t always developing shiny new applications from scratch.  RPC makes it easy to publish existing libraries as a remotely-accessible service for another system, and also to switch out an existing local library class for a remote one, since the shape of their interfaces is so similar.</p>
<p>The example I showed here with flickr was an XML-RPC service but it is also very common to see and use JSON-RPC and indeed other formats such as serialised PHP arrays/objects are equally valid in this setting.  Choosing the right format depends entirely on your application and each has its advantages and disadvantages.  Time for a quick tangent to look at these!</p>
<h3>Data Formats</h3>
<p>When we think of web services, we commonly think of SOAP and other types of <strong>XML</strong>.  In fact these are really useful with web services and are relatively common as a result; we showed an XML example earlier in this post, which handled the XML using <a href="http://uk2.php.net/manual/en/book.simplexml.php">PHP&#8217;s SimpleXML extension</a>.  XML is ideal for communication between machines because it is quite verbose, which means it is precise and leaves little room for interpretation or ambiguity.  XML isn&#8217;t terribly easy to read without help from machines, which can make debugging a bit harder than working with a simpler format.  It is also fairly large in size terms for the data it represents, which isn&#8217;t important in most applications but at some edge cases this could make it a less appealing option.</p>
<p>Another very common format in use in web services is <strong>JSON</strong>.  While this format is named after its origins (JavaScript Object Notation), don&#8217;t be misled by the title.  JSON is in fact easily read and written from almost all programming languages &#8211; although for something like JavaScript or an iPhone where relatively limited string handling is available, JSON is an excellent choice.  It is lightweight, small for the data it represents and easy to parse although the small size does mean that it is less comprehensive than some of the other formats.  In particular JSON doesn&#8217;t include information about data types so it can be unclear what format the data was in when it was encoded.  Particularly exasperating is that it doesn&#8217;t know whether something was an object or an associative array &#8211; of course we can work around this but we must be aware of it.</p>
<p>Other possibilities include a serialised PHP format, which is a great choice if you know that client and server will both always run PHP.  This format has a lot of the advantages of JSON but does include some type information with the data, which can make it more useful.</p>
<h3>SOAP:  A Special Case of XML-RPC</h3>
<p>With the adoption of PHP rising in the enterprise, it is increasingly common for PHP applications to come into contact with other systems within an organisation written in languages such as Java and .NET.  In fact I am seeing more and more situations where PHP is being used as the &#8220;glue&#8221; to integrate between disparate systems written in these and other &#8220;enterprise&#8221; languages.  In terms of API access, these types of applications will commonly support SOAP through preference, and indeed even within the PHP ecosystem, we see SOAP implementations from various projects aimed more at business than the casual user.  In particular, both <a href="http://www.sugarcrm.com/">SugarCRM</a> and <a href="http://www.magentocommerce.com/">Magento</a> provide remote access via SOAP APIs.</p>
<p>Back in PHP 5, a <a href="http://uk2.php.net/manual/en/book.soap.php">SOAP extension</a> was included in PHP by default, and this makes working with SOAP services, especially those with a WSDL, trivial.  As an example, take a look at this example of pulling information from Magento Ecommerce:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// connect to soap server</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: #0000ff;">'http://magento.local/api/v2_soap?wsdl=1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// log in</span>
    <span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">login</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lorna'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$list</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customerCustomerList</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$session</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I did say it was trivial!  All we need to do is instantiate a SoapClient object, giving the WSDL.  We can then call the methods outlined in the WSDL, passing the arguments as we would for a function.</p>
<blockquote><p><i>Note: This is an example of the Magento V2 API, for more information about Magento&#8217;s API, <a href="http://www.magentocommerce.com/wiki/doc/webservices-api/api">visit their wiki</a>.  This is another topic that would make a whole article by itself. </i></p></blockquote>
<h3>Strategies for Debugging</h3>
<p>As I mentioned earlier, the XML-based formats in general, and SOAP in particular, can be tricky to work with if you need to actually debug them.  Part of this is that if your code doesn&#8217;t handle SOAP faults or something unexpected goes wrong, it can be really hard to understand what is happening.  There is very little more annoying than spending all morning destroying your own code to find the error, only to discover the server has stopped responding correctly!  To help with this, I have a few strategies that I would like to share.</p>
<p>First of all, our good friend <strong>curl</strong>.  I worked on one project where I was producing a web service that was consumed by an application my colleagues were building.  I told them that I would only accept bug reports against my code if they sent me a replication case which used curl.  This was not welcome news to them, but they confessed later that about half of the &#8220;bugs&#8221; they found turned out to be in their own code when they looked closer.  Eliminating the consuming code as a source of problems is really important when working with remote services.</p>
<p>Another key approach, and something I use frequently myself alongside my development, is to use a <strong>proxy</strong>.  Personally I use <a href="http://www.wireshark.org">Wireshark</a> to show me what is happening &#8220;on the wire&#8221; while I work with web services.  I have also heard strong recommendations from industry peers for <a href="http://www.charlesproxy.com">Charles</a> which is non-free but very inexpensive and seems to be well-loved by its users.  Using a proxy, you can check exactly what is getting sent and received without making any alterations to your own code or changing the calls you make, which can very quickly help you diagnose errors.</p>
<h3>RESTful Services</h3>
<p>Perhaps the purest form of the web services over HTTP is REST.  If you are not familiar with RESTful principles then there is a <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">great overview on Wikipedia</a> that explains it really well.  Many of the sites publishing information over web services will provide RESTful interfaces and these are easy to understand and simple to consume.</p>
<p>Good examples of these are Flickr who have a great RESTful API alongside all their other offerings, and <a href="http://twitter.com">twitter</a> who use a similar scheme for the URL design throughout their site as well as across their service.  This is really interesting, because you can use their services then almost seamlessly alongside their site.  Consider this URL:</p>
<p><a href="http://search.twitter.com/search?q=techportal">http://search.twitter.com/search?q=techportal</a></p>
<p>Opening this in the browser will show the search results for &#8220;techportal&#8221; on twitter.  To request these from PHP in a JSON format is almost as simple, see this example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://search.twitter.com/search.json?q=techportal'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can inspect $json and see that we have the same data in JSON format.  The <a href="http://apiwiki.twitter.com/Twitter-API-Documentation">twitter API documentation</a> is rather good so if you&#8217;re looking for a good project to practise with, then this could be an ideal place to start!</p>
<h3>Web Services</h3>
<p>I hope this article has given a good overview of what web services are and how they look when they come in to form part of our code.  There is so much more on all of these various topics, and I am sure some of you will want to read more.  There are some links for you in the resources section below, and I hope that you will also add your own stories and resources in the comments &#8211; thanks for reading!</p>
<h3>Resources</h3>
<p><a href="http://www.flickr.com/services/api/">flickr API</a><br />
<a href="http://apiwiki.twitter.com/Twitter-API-Documentation">twitter API</a><br />
<a href="http://www.magentocommerce.com/wiki/doc/webservices-api/api">magento API</a><br />
<a href="http://www.sugarcrm.com/wiki/index.php?title=SOAP_Documentation">sugarcrm API</a><br />
<a href="http://curl.haxx.se/">curl</a> &#8211; command line http tool<br />
<a href="http://www.wireshark.org/">wireshark</a> and <a href="http://www.charlesproxy.com/">charles</a> &#8211; debugging proxies</p>
<p>Need to convince your managers about the value of Web Services?  Send them over to our excellent whitepaper <a href="http://ibuildings.com/whitepapers/introducing-service-apis">Introducing Service APIs</a> to explain how services fit in to modern application architecture.</p>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=Web%20Services%20for%20PHP%20Developers&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Web%20Services%20for%20PHP%20Developers%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F&amp;t=Web%20Services%20for%20PHP%20Developers" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F&amp;title=Web%20Services%20for%20PHP%20Developers&amp;bodytext=In%20this%20world%20of%20sharing%20data%2C%20increasing%20numbers%20of%20sites%20and%20applications%20are%20making%20information%20available%20over%20web%20services.%20%20Whether%20we%20are%20building%20a%20service%20as%20a%20feature%20of%20our%20own%20development%2C%20or%20pulling%20in%20the%20information%20published%20by%20others%2C" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F&amp;title=Web%20Services%20for%20PHP%20Developers" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F&amp;title=Web%20Services%20for%20PHP%20Developers&amp;notes=In%20this%20world%20of%20sharing%20data%2C%20increasing%20numbers%20of%20sites%20and%20applications%20are%20making%20information%20available%20over%20web%20services.%20%20Whether%20we%20are%20building%20a%20service%20as%20a%20feature%20of%20our%20own%20development%2C%20or%20pulling%20in%20the%20information%20published%20by%20others%2C" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F&amp;title=Web%20Services%20for%20PHP%20Developers&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=In%20this%20world%20of%20sharing%20data%2C%20increasing%20numbers%20of%20sites%20and%20applications%20are%20making%20information%20available%20over%20web%20services.%20%20Whether%20we%20are%20building%20a%20service%20as%20a%20feature%20of%20our%20own%20development%2C%20or%20pulling%20in%20the%20information%20published%20by%20others%2C" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F&amp;title=Web%20Services%20for%20PHP%20Developers" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F06%2F01%2Fweb-services-for-php-developers%2F&amp;title=Web%20Services%20for%20PHP%20Developers" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/06/01/web-services-for-php-developers/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Code and Release Management</title>
		<link>http://techportal.ibuildings.com/2010/05/25/code-and-release-management/</link>
		<comments>http://techportal.ibuildings.com/2010/05/25/code-and-release-management/#comments</comments>
		<pubDate>Tue, 25 May 2010 08:10:31 +0000</pubDate>
		<dc:creator>Cal Evans</dc:creator>
				<category><![CDATA[DPCRadio]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[eli white]]></category>
		<category><![CDATA[management]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[versioning]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=1492</guid>
		<description><![CDATA[Speaker: Eli White Programmers seem to have a fairly good grasp on &#8216;how to write code&#8217;, but not always what to do with it once it&#8217;s been written. This talk with delve into details about how to manage your codebase, and how to manage releases (often intertwined). It will cover topics of coding standards and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://techportal.ibuildings.com/wp-content/uploads/audio/dpcradio/dpc-podcast.jpg" align="left" width="15%" style="margin:10px;"/>Speaker: <a href="http://eliw.com">Eli White</a></p>
<p>Programmers seem to have a fairly good grasp on &#8216;how to write code&#8217;, but not always what to do with it once it&#8217;s been written. This talk with delve into details about how to manage your codebase, and how to manage releases (often intertwined). It will cover topics of coding standards and focus heavily on how you choose to use versioning tools to handle your development and release processes. It will have a distinct Subversion(SVN) flavor to the talk, but the concepts will not be specific to SVN.</p>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=Code%20and%20Release%20Management&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Code%20and%20Release%20Management%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F&amp;t=Code%20and%20Release%20Management" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F&amp;title=Code%20and%20Release%20Management&amp;bodytext=Speaker%3A%20Eli%20White%0D%0A%0D%0AProgrammers%20seem%20to%20have%20a%20fairly%20good%20grasp%20on%20%27how%20to%20write%20code%27%2C%20but%20not%20always%20what%20to%20do%20with%20it%20once%20it%27s%20been%20written.%20This%20talk%20with%20delve%20into%20details%20about%20how%20to%20manage%20your%20codebase%2C%20and%20how%20to%20manage%20releases%20%28ofte" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F&amp;title=Code%20and%20Release%20Management" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F&amp;title=Code%20and%20Release%20Management&amp;notes=Speaker%3A%20Eli%20White%0D%0A%0D%0AProgrammers%20seem%20to%20have%20a%20fairly%20good%20grasp%20on%20%27how%20to%20write%20code%27%2C%20but%20not%20always%20what%20to%20do%20with%20it%20once%20it%27s%20been%20written.%20This%20talk%20with%20delve%20into%20details%20about%20how%20to%20manage%20your%20codebase%2C%20and%20how%20to%20manage%20releases%20%28ofte" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F&amp;title=Code%20and%20Release%20Management&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=Speaker%3A%20Eli%20White%0D%0A%0D%0AProgrammers%20seem%20to%20have%20a%20fairly%20good%20grasp%20on%20%27how%20to%20write%20code%27%2C%20but%20not%20always%20what%20to%20do%20with%20it%20once%20it%27s%20been%20written.%20This%20talk%20with%20delve%20into%20details%20about%20how%20to%20manage%20your%20codebase%2C%20and%20how%20to%20manage%20releases%20%28ofte" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F&amp;title=Code%20and%20Release%20Management" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F25%2Fcode-and-release-management%2F&amp;title=Code%20and%20Release%20Management" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/05/25/code-and-release-management/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
<enclosure url="http://techportal.ibuildings.com/wp-content/uploads/audio/dpcradio/episode_022.mp3" length="128299389" type="audio/mpeg" />
		</item>
		<item>
		<title>The Elephpant Challenge – Winners and Results</title>
		<link>http://techportal.ibuildings.com/2010/05/17/the-elephpant-challenge-winners-and-results/</link>
		<comments>http://techportal.ibuildings.com/2010/05/17/the-elephpant-challenge-winners-and-results/#comments</comments>
		<pubDate>Mon, 17 May 2010 13:56:27 +0000</pubDate>
		<dc:creator>Ivo Jansch</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[elephpant]]></category>
		<category><![CDATA[travelling salesman]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=1971</guid>
		<description><![CDATA[We recently wrapped up the Ibuildings Elephpant Challenge, a contest where a PHP Elephpant traveled around the world visiting a given set of landmarks. Contestants had to write a script that calculated the shortest route for the Elephpant. The contest has several side goals: entries were not not only validated against the given landmarks and [...]]]></description>
			<content:encoded><![CDATA[<p>We recently wrapped up the Ibuildings Elephpant Challenge, a contest where a <A href="http://www.flickr.com/search/?q=elephpant&#038;w=all&#038;s=int">PHP Elephpant</a> traveled around the world visiting a given set of landmarks. Contestants had to write a script that calculated the shortest route for the Elephpant. The contest has several side goals: entries were not not only validated against the given landmarks and a second secret set of landmarks, they also were scored based on their performance, code complexity and code size.</p>
<p>In this post, the winners will be announced and we will make some observations based on the contest. </p>
<p><span id="more-1971"></span></p>
<h3>The solution</h3>
<p>The shortest route based on the <a href="http://www.ibuildings.com/downloads/challenge/elephpant_landmarks.csv">given CSV file</a> is <strong>41799 kilometers and 231 meters</strong>, calculated with the Haversine Formula. The route is: Big Ben; Area 51; Zendcon; Golden Gate Bridge; Sydney Opera; Harbour, Tokyo; Mount Everest; Taj Mahal, Eiffeltower; Elephpant Heaven.</p>
<p>The secret CSV file we used can be <a href="http://www.ibuildings.com/downloads/challenge/elephpant_landmarks_secret.csv">found here</a>. The shortest route now is <strong>48701 km and 353 meters</strong> (Sheffield; php|tek; Mexico City; Apple Cupertino; Vancouver; Hawaii; Chinese Wall; Cape Town; Tower of Pisa; Manneke Pis).</p>
<h3>The Winners</h3>
<p>Let us have a look at the winners. We had 3 categories; Junior developers, Medium developers and Senior developers. The contest was apparently harder than we anticipated because none of the junior candidates managed to find the correct distance. Two candidates came really close (Elias Sorensen and Matthew Haynes at 42089 and 42090 km on the initial CSV respectively).</p>
<p>In the medium category, there were a few candidates that had the initial CSV right, but only one candidate that also had the correct solution to the secret CSV file. Since we only score on performance, complexity and code size among the candidates with the right solution (see further down for a few observations on this), this person automatically became the winner:</p>
<p>Category Winner Medium: <strong>Andy Thompson</strong></p>
<p>Andy&#8217;s average performance on 10 landmarks was 333.79 seconds and he used 38 lines of code. </p>
<p>In the senior category, we had many more correct solutions and a variety of algorithms. The top 5 entries with the correct routes:</p>
<table border="1">
<tr>
<th>Contestant</th>
<th>Performance / Complexity / Lines of Code</th>
<th>Total score</th>
</tr>
<tr>
<td>1. <strong>Michiel Brandenburg</strong></td>
<td>50.25 seconds / 14 / 46</td>
<td>26</td>
</tr>
<tr>
<td>2. Remi Woler</td>
<td>11.05 seconds / 19 / 125</td>
<td>23</td>
</tr>
<tr>
<td>3. Joris van de Sande</td>
<td>69.91 seconds / 14 / 48</td>
<td>22</td>
</tr>
<tr>
<td>4. Ben Spencer</td>
<td>19.98 seconds / 28 / 61 </td>
<td>20</td>
</tr>
<tr>
<td>5. Bastian Gorke</td>
<td>68.99 seconds / 30 / 164</td>
<td>14</td>
</tr>
</table>
<p>Among the 2 category winners we raffled an iPad and a ticket to the <a href="http://phpconference.nl">Dutch PHP Conference</a>. Michiel won the iPad, Andy won the DPC ticket.</p>
<h3>Observations</h3>
<p>We made a few observations when evaluating the contest and we thought we&#8217;d share them.</p>
<p><strong>Adhering to specifications</strong><br />
We automated the test so that we could easily produce results. To do this we had very specific requirements; the script should be called &#8216;contest.php&#8217;, accept 1 command line input parameter indicating the input CSV file, and required the output to be a list of landmark names, one on each line.</p>
<p>It is amusing to see that we received scripts called index.php or ibuildings.php, scripts that had CSV output, and we even received two scripts that given a CSV file of 10 landmarks, would produce a route that would only visit 9 of them. True; elephpants are picky when it comes to choosing places to visit, but the elephpant was required to visit them all. This should not be a problem for an elephpant, especially with landmarks such as Elephpant Heaven in the list; interesting that nobody wondered what Elephpant Heaven is. You will see what we meant when you&#8217;re around; it&#8217;s not far from the Dutch PHP Conference venue. </p>
<p><strong>Making choices</strong><br />
We noted that Remi was almost twice as fast as Ben, and 5 times faster than Michiel, but complexity and code size were just as important and since Michiel had top score in both categories, he became the overall winner. When I look at the various entries, it&#8217;s striking that almost everybody chose to optimize performance as if that were the main goal; I guess it&#8217;s more fun to focus on performance. Some candidates used really really fast algorithms (producing results at around 0.24 seconds for both CSV files!), but those algorithms did not guarantee accurate results (ranging from &#8216;close&#8217; at 42090km to &#8216;very wrong&#8217; at 76536km. In a real world situation those algorithms might be the best choice if performance is essential and accuracy is not required. In our case however we mentioned that winners must produce the correct result; a requirement like that should be key in selecting an approach to any software problem.</p>
<p><strong>Secrets</strong><br />
The reason we used a secret CSV file to validate the entries was to prevent optimizations that were aware of the route; this proved to be useful since there were indeed contestants that had a very fast correct solution but only because they had knowledge about the landmarks and had for example &#8216;started their elephpant in a certain direction&#8217;. These contestants fell through on the secret CSV file, where they were still performing very fast but not producing the shortest route.</p>
<p><strong>The Traveling Salesman</strong><br />
Most contestants had recognized immediately that this problem is essentially the <a href="http://en.wikipedia.org/wiki/Travelling_salesman_problem">&#8216;Traveling Salesman Problem&#8217;</a>, which is used in many computer science courses to teach algorithms and optimization. </p>
<p>We had entries from every country in Europe, still it&#8217;s intersting to note that the top 3 were all Dutch candidates. Maybe the Dutch have more traveling salesmen? :)</p>
<p><strong>Running a contest can be hard!</strong><br />
One observation was that we completely underestimated the time it would take to get the results. While some had written very efficient algorithms, others had chosen brute force attacks that ran for quite a while. Since we wanted to get accurate timing results, we ran scripts multiple times and calculated averages. After the first run had run for 3 days and still only had 25% of the entries processed, I changed the script to not run multiple times if the initial result wasn&#8217;t under 3 minutes. It eventually still took my test server a few days to go over all the entries.</p>
<p>Although we did not state how many landmarks there would be in the secret CSV file, a larger file could have made the contest virtually impossible. To illustrate how brute force solutions are very difficult to scale: a script that would run for 3 minutes on 10 landmarks could run for 3 hours with just 2 landmarks more.</p>
<p>Another difficulty was the lines of code count and the complexity numbers. We used proper tools to generate the numbers, but the lines of code counter we used would ignore files called .inc and the complexity calculator would ignore code not in a function and so on. So we had to fiddle quite significantly to get accurate numbers there. A lesson for future contests.</p>
<h3>Conclusion</h3>
<p>For us as organizers, this was a fun challenge, and seeing the buzz on Twitter surrounding the contest it certainly seems the contestants had fun as well.</p>
<p>Let me finish up by saying: Winners: congratulations! And everybody else, thanks for participating, I hope you had fun!</p>
<p>In the meantime the next challenge, dubbed the Test Driven Challenge, has been launched; develop a class that satisfies a given set of testcases. <a href="http://www.ibuildings.com/challenge">This new challenge</a> will run until June 30 and the prize is once more an iPad. This one is slightly easier than the previous challenge, to give the junior and medium developers another shot at the iPad!</p>
<p>Have fun!</p>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=The%20Elephpant%20Challenge%20-%20Winners%20and%20Results&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=The%20Elephpant%20Challenge%20-%20Winners%20and%20Results%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F&amp;t=The%20Elephpant%20Challenge%20-%20Winners%20and%20Results" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F&amp;title=The%20Elephpant%20Challenge%20-%20Winners%20and%20Results&amp;bodytext=We%20recently%20wrapped%20up%20the%20Ibuildings%20Elephpant%20Challenge%2C%20a%20contest%20where%20a%20PHP%20Elephpant%20traveled%20around%20the%20world%20visiting%20a%20given%20set%20of%20landmarks.%20Contestants%20had%20to%20write%20a%20script%20that%20calculated%20the%20shortest%20route%20for%20the%20Elephpant.%20The%20contes" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F&amp;title=The%20Elephpant%20Challenge%20-%20Winners%20and%20Results" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F&amp;title=The%20Elephpant%20Challenge%20-%20Winners%20and%20Results&amp;notes=We%20recently%20wrapped%20up%20the%20Ibuildings%20Elephpant%20Challenge%2C%20a%20contest%20where%20a%20PHP%20Elephpant%20traveled%20around%20the%20world%20visiting%20a%20given%20set%20of%20landmarks.%20Contestants%20had%20to%20write%20a%20script%20that%20calculated%20the%20shortest%20route%20for%20the%20Elephpant.%20The%20contes" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F&amp;title=The%20Elephpant%20Challenge%20-%20Winners%20and%20Results&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=We%20recently%20wrapped%20up%20the%20Ibuildings%20Elephpant%20Challenge%2C%20a%20contest%20where%20a%20PHP%20Elephpant%20traveled%20around%20the%20world%20visiting%20a%20given%20set%20of%20landmarks.%20Contestants%20had%20to%20write%20a%20script%20that%20calculated%20the%20shortest%20route%20for%20the%20Elephpant.%20The%20contes" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F&amp;title=The%20Elephpant%20Challenge%20-%20Winners%20and%20Results" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F17%2Fthe-elephpant-challenge-winners-and-results%2F&amp;title=The%20Elephpant%20Challenge%20-%20Winners%20and%20Results" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/05/17/the-elephpant-challenge-winners-and-results/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>State &amp; Ajax – How to Maintain Browser and Application State in an Asynchronous World</title>
		<link>http://techportal.ibuildings.com/2010/05/11/state-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world/</link>
		<comments>http://techportal.ibuildings.com/2010/05/11/state-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world/#comments</comments>
		<pubDate>Tue, 11 May 2010 08:00:49 +0000</pubDate>
		<dc:creator>Cal Evans</dc:creator>
				<category><![CDATA[DPCRadio]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[paul reinheimer]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=1389</guid>
		<description><![CDATA[Speaker: Paul Reinheimer This talk will examine the two greatest problems in Ajax development (except for that pesky browser issue): Exactly what that “Asynchronous” word means, what problems it creates, and how they can be effectively managed, next the YUI Browser History object will be examined, finally handing control of Ajax applications back to the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://techportal.ibuildings.com/wp-content/uploads/audio/dpcradio/dpc-podcast.jpg" align="left" width="15%" style="margin:10px;"/>Speaker: <a href="http://blog.preinheimer.com/">Paul Reinheimer</a><br />
This talk will examine the two greatest problems in Ajax development (except for that pesky browser issue): Exactly what that “Asynchronous” word means, what problems it creates, and how they can be effectively managed, next the YUI Browser History object will be examined, finally handing control of Ajax applications back to the user via their familiar back button.</p>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=State%20%26%20Ajax%20-%20How%20to%20Maintain%20Browser%20and%20Application%20State%20in%20an%20Asynchronous%20World%20&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=State%20%26%20Ajax%20-%20How%20to%20Maintain%20Browser%20and%20Application%20State%20in%20an%20Asynchronous%20World%20%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F&amp;t=State%20%26%20Ajax%20-%20How%20to%20Maintain%20Browser%20and%20Application%20State%20in%20an%20Asynchronous%20World%20" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F&amp;title=State%20%26%20Ajax%20-%20How%20to%20Maintain%20Browser%20and%20Application%20State%20in%20an%20Asynchronous%20World%20&amp;bodytext=Speaker%3A%20Paul%20Reinheimer%0D%0AThis%20talk%20will%20examine%20the%20two%20greatest%20problems%20in%20Ajax%20development%20%28except%20for%20that%20pesky%20browser%20issue%29%3A%20Exactly%20what%20that%20%E2%80%9CAsynchronous%E2%80%9D%20word%20means%2C%20what%20problems%20it%20creates%2C%20and%20how%20they%20can%20be%20effectively%20managed%2C%20" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F&amp;title=State%20%26%20Ajax%20-%20How%20to%20Maintain%20Browser%20and%20Application%20State%20in%20an%20Asynchronous%20World%20" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F&amp;title=State%20%26%20Ajax%20-%20How%20to%20Maintain%20Browser%20and%20Application%20State%20in%20an%20Asynchronous%20World%20&amp;notes=Speaker%3A%20Paul%20Reinheimer%0D%0AThis%20talk%20will%20examine%20the%20two%20greatest%20problems%20in%20Ajax%20development%20%28except%20for%20that%20pesky%20browser%20issue%29%3A%20Exactly%20what%20that%20%E2%80%9CAsynchronous%E2%80%9D%20word%20means%2C%20what%20problems%20it%20creates%2C%20and%20how%20they%20can%20be%20effectively%20managed%2C%20" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F&amp;title=State%20%26%20Ajax%20-%20How%20to%20Maintain%20Browser%20and%20Application%20State%20in%20an%20Asynchronous%20World%20&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=Speaker%3A%20Paul%20Reinheimer%0D%0AThis%20talk%20will%20examine%20the%20two%20greatest%20problems%20in%20Ajax%20development%20%28except%20for%20that%20pesky%20browser%20issue%29%3A%20Exactly%20what%20that%20%E2%80%9CAsynchronous%E2%80%9D%20word%20means%2C%20what%20problems%20it%20creates%2C%20and%20how%20they%20can%20be%20effectively%20managed%2C%20" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F&amp;title=State%20%26%20Ajax%20-%20How%20to%20Maintain%20Browser%20and%20Application%20State%20in%20an%20Asynchronous%20World%20" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F05%2F11%2Fstate-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world%2F&amp;title=State%20%26%20Ajax%20-%20How%20to%20Maintain%20Browser%20and%20Application%20State%20in%20an%20Asynchronous%20World%20" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/05/11/state-ajax-how-to-maintain-browser-and-application-state-in-an-asynchronous-world/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
<enclosure url="http://techportal.ibuildings.com/wp-content/uploads/audio/dpcradio/episode_010.mp3" length="42781663" type="audio/mpeg" />
		</item>
		<item>
		<title>Tips for Working with DateTime in PHP</title>
		<link>http://techportal.ibuildings.com/2010/04/29/tips-for-working-with-datetime-in-php/</link>
		<comments>http://techportal.ibuildings.com/2010/04/29/tips-for-working-with-datetime-in-php/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 15:00:22 +0000</pubDate>
		<dc:creator>Lorna Mitchell</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[tipstricks]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=1882</guid>
		<description><![CDATA[Recently we published a post about tips for dates and times in PHP, today I thought I&#8217;d follow on from Michael and add some more tips. In PHP 5.2 there is an object oriented approach to working with dates and times &#8211; the DateTime class! In the newer PHP 5.3 versions even more excellent functionality [...]]]></description>
			<content:encoded><![CDATA[<p>Recently we published a <a href="http://techportal.ibuildings.com/2010/01/26/tips-for-php-date-and-time-functions/">post about tips for dates and times in PHP</a>, today I thought I&#8217;d follow on from Michael and add some more tips.  In PHP 5.2 there is an object oriented approach to working with dates and times &#8211; the <a href="http://uk2.php.net/manual/en/class.datetime.php">DateTime class</a>!  In the newer PHP 5.3 versions even more excellent functionality was added.  This is a short post to introduce this functionality with a focus on the methods available in PHP 5.2 showing some simple examples of how we can more easily manipulate and display dates without needing to work with timestamps.<br />
<span id="more-1882"></span></p>
<h3>Creating a DateTime Object</h3>
<p>The DateTime class can accept any argument understood by the function <a href="http://uk2.php.net/manual/en/function.strtotime.php">strtotime()</a> and turn it into an object that we can work with.  This means that we can use almost any format and supply it to the constructor, e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$datetime</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2010-05-03 17:26:32'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>One thing which the manual doesn&#8217;t show you is that you can also create using a timestamp, very useful if you are working with them already, perhaps you store datetimes that way or accept them as incoming parameters in that format.  To do that you can use the following syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$datetime</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@<span style="color: #006699; font-weight: bold;">{$timestamp}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>(where $timestamp is a unix timestamp).  It is also possible to set the timestamp of an existing DateTime object using the <a href="http://uk2.php.net/manual/en/datetime.settimestamp.php">setTimestamp()</a> method of the class.</p>
<h3>Timezones and DateTime</h3>
<p>Since we are now representing our point in time with an object, we can store multiple pieces of information about it really easily.  In particular we can store a timezone against the object, using the method <a href="http://uk2.php.net/manual/en/datetime.settimezone.php">DateTime::setTimezone()</a>.  The timezones themselves are also an object &#8211; of type <a href="http://uk2.php.net/manual/en/class.datetimezone.php">DateTimeZone</a> and represent timezones such as &#8220;Europe/Amsterdam&#8221; or &#8220;America/Lima&#8221;.  These will throw an exception if they receive a string they don&#8217;t recognise into their constructor.</p>
<h3>Very Easy Formatting</h3>
<p>All our existing experience of working with dates in PHP will still be very useful to us working with the DateTime objects since to output our datetime value in a human-readable format, we use the <a href="http://www.php.net/manual/en/datetime.format.php">DateTime::format()</a> method &#8211; and this accepts the same arguments as the <a href="http://uk2.php.net/manual/en/function.date.php">date()</a> method does, so it will look quite familiar!  There are some nice additions though, for example some constants that allow us to very easily output the correct format for all sorts of uses.  A good illustration is the time format for cookies, which we would output something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$datetime</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">format</span><span style="color: #009900;">&#40;</span>DATE_COOKIE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Unambiguous Storage</h3>
<p>Storing the serialised DateTime objects is a great way to avoid the uncertainties that can arise between storing dates and times and using them on machines with different timezones used locally.  It was actually solving a problem like this that I first started using this class so frequently.   For more information about the problems (and solutions) around accurate date storage, I recommend Derick Rethans&#8217; blog post <a href="http://derickrethans.nl/storing-date-time-in-database.html">Storing Date/Times in Databases</a> &#8211; its an excellent read and explains this issue in some detail.</p>
<p>Have you started using the DateTime class in PHP?  I&#8217;m interested to know who is using it, or not, and why.</p>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=Tips%20for%20Working%20with%20DateTime%20in%20PHP&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Tips%20for%20Working%20with%20DateTime%20in%20PHP%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F&amp;t=Tips%20for%20Working%20with%20DateTime%20in%20PHP" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F&amp;title=Tips%20for%20Working%20with%20DateTime%20in%20PHP&amp;bodytext=Recently%20we%20published%20a%20post%20about%20tips%20for%20dates%20and%20times%20in%20PHP%2C%20today%20I%20thought%20I%27d%20follow%20on%20from%20Michael%20and%20add%20some%20more%20tips.%20%20In%20PHP%205.2%20there%20is%20an%20object%20oriented%20approach%20to%20working%20with%20dates%20and%20times%20-%20the%20DateTime%20class%21%20%20In%20the%20newe" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F&amp;title=Tips%20for%20Working%20with%20DateTime%20in%20PHP" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F&amp;title=Tips%20for%20Working%20with%20DateTime%20in%20PHP&amp;notes=Recently%20we%20published%20a%20post%20about%20tips%20for%20dates%20and%20times%20in%20PHP%2C%20today%20I%20thought%20I%27d%20follow%20on%20from%20Michael%20and%20add%20some%20more%20tips.%20%20In%20PHP%205.2%20there%20is%20an%20object%20oriented%20approach%20to%20working%20with%20dates%20and%20times%20-%20the%20DateTime%20class%21%20%20In%20the%20newe" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F&amp;title=Tips%20for%20Working%20with%20DateTime%20in%20PHP&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=Recently%20we%20published%20a%20post%20about%20tips%20for%20dates%20and%20times%20in%20PHP%2C%20today%20I%20thought%20I%27d%20follow%20on%20from%20Michael%20and%20add%20some%20more%20tips.%20%20In%20PHP%205.2%20there%20is%20an%20object%20oriented%20approach%20to%20working%20with%20dates%20and%20times%20-%20the%20DateTime%20class%21%20%20In%20the%20newe" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F&amp;title=Tips%20for%20Working%20with%20DateTime%20in%20PHP" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F29%2Ftips-for-working-with-datetime-in-php%2F&amp;title=Tips%20for%20Working%20with%20DateTime%20in%20PHP" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/04/29/tips-for-working-with-datetime-in-php/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Everything you always wanted to know about UTF-8 (but never dared to ask)</title>
		<link>http://techportal.ibuildings.com/2010/04/27/everything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask/</link>
		<comments>http://techportal.ibuildings.com/2010/04/27/everything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 07:00:24 +0000</pubDate>
		<dc:creator>Cal Evans</dc:creator>
				<category><![CDATA[DPCRadio]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[utf-8]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=1430</guid>
		<description><![CDATA[Speaker: Juliette Reinders Folmer With PHP6 in the works and said to be fully UTF-8 compliant, the interest in the community in using UTF-8 has grown. On top of that, with PHP entering the – often multilingual – enterprise web playing field, the benefits UTF-8 can bring are getting more attention. Unfortunately most developers at [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://techportal.ibuildings.com/wp-content/uploads/audio/dpcradio/dpc-podcast.jpg" align="left" width="15%" style="margin:10px;"/>Speaker: Juliette Reinders Folmer</p>
<p>With PHP6 in the works and said to be fully UTF-8 compliant, the interest in the community in using UTF-8 has grown. On top of that, with PHP entering the – often multilingual – enterprise web playing field, the benefits UTF-8 can bring are getting more attention.</p>
<p>Unfortunately most developers at one point or another run into problems implementing UTF-8.</p>
<p>In this talk I will cover UTF-8 from the basic linguistics, through client-side aspects to all the steps you need to take to tackle the most common (and some more obscure) issues when using UTF-8 in a database driven PHP application.</p>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=Everything%20you%20always%20wanted%20to%20know%20about%20UTF-8%20%28but%20never%20dared%20to%20ask%29&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Everything%20you%20always%20wanted%20to%20know%20about%20UTF-8%20%28but%20never%20dared%20to%20ask%29%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F&amp;t=Everything%20you%20always%20wanted%20to%20know%20about%20UTF-8%20%28but%20never%20dared%20to%20ask%29" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F&amp;title=Everything%20you%20always%20wanted%20to%20know%20about%20UTF-8%20%28but%20never%20dared%20to%20ask%29&amp;bodytext=Speaker%3A%20Juliette%20Reinders%20Folmer%0D%0A%0D%0AWith%20PHP6%20in%20the%20works%20and%20said%20to%20be%20fully%20UTF-8%20compliant%2C%20the%20interest%20in%20the%20community%20in%20using%20UTF-8%20has%20grown.%20On%20top%20of%20that%2C%20with%20PHP%20entering%20the%20%E2%80%93%20often%20multilingual%20%E2%80%93%20enterprise%20web%20playing%20field%2C%20t" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F&amp;title=Everything%20you%20always%20wanted%20to%20know%20about%20UTF-8%20%28but%20never%20dared%20to%20ask%29" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F&amp;title=Everything%20you%20always%20wanted%20to%20know%20about%20UTF-8%20%28but%20never%20dared%20to%20ask%29&amp;notes=Speaker%3A%20Juliette%20Reinders%20Folmer%0D%0A%0D%0AWith%20PHP6%20in%20the%20works%20and%20said%20to%20be%20fully%20UTF-8%20compliant%2C%20the%20interest%20in%20the%20community%20in%20using%20UTF-8%20has%20grown.%20On%20top%20of%20that%2C%20with%20PHP%20entering%20the%20%E2%80%93%20often%20multilingual%20%E2%80%93%20enterprise%20web%20playing%20field%2C%20t" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F&amp;title=Everything%20you%20always%20wanted%20to%20know%20about%20UTF-8%20%28but%20never%20dared%20to%20ask%29&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=Speaker%3A%20Juliette%20Reinders%20Folmer%0D%0A%0D%0AWith%20PHP6%20in%20the%20works%20and%20said%20to%20be%20fully%20UTF-8%20compliant%2C%20the%20interest%20in%20the%20community%20in%20using%20UTF-8%20has%20grown.%20On%20top%20of%20that%2C%20with%20PHP%20entering%20the%20%E2%80%93%20often%20multilingual%20%E2%80%93%20enterprise%20web%20playing%20field%2C%20t" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F&amp;title=Everything%20you%20always%20wanted%20to%20know%20about%20UTF-8%20%28but%20never%20dared%20to%20ask%29" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F27%2Feverything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask%2F&amp;title=Everything%20you%20always%20wanted%20to%20know%20about%20UTF-8%20%28but%20never%20dared%20to%20ask%29" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/04/27/everything-you-always-wanted-to-know-about-utf-8-but-never-dared-to-ask/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
<enclosure url="http://techportal.ibuildings.com/wp-content/uploads/audio/dpcradio/episode_012.mp3" length="47006809" type="audio/mpeg" />
		</item>
		<item>
		<title>Writing a Custom Magento Module</title>
		<link>http://techportal.ibuildings.com/2010/04/20/writing-a-custom-magento-module/</link>
		<comments>http://techportal.ibuildings.com/2010/04/20/writing-a-custom-magento-module/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 10:00:13 +0000</pubDate>
		<dc:creator>Matt Setter</dc:creator>
				<category><![CDATA[Planet PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[ecommerce]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[magento]]></category>
		<category><![CDATA[module]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://techportal.ibuildings.com/?p=1853</guid>
		<description><![CDATA[The aim of this tutorial is to provide a good reference guide to keep with you when developing custom modules with ecommerce solution Magento. It details some of the key sections of how to build a custom Magento module, some of the concepts, assumptions and key points that that you are likely to encounter on [...]]]></description>
			<content:encoded><![CDATA[<p>The aim of this tutorial is to provide a good reference guide to keep with you when developing custom modules with ecommerce solution <a href="http://www.magentocommerce.com/">Magento</a>. It details some of the key sections of how to build a custom Magento module, some of the concepts, assumptions and key points that that you are likely to encounter on a regular basis when building ecommerce installations with this software.</p>
<p>While Magento does not have a great reputation for documentation, there are some good resources available, including the <a href="http://www.magentocommerce.com/wiki/">Magento Wiki </a>and php|architect’s Guide to Programming Magento. I used both of these when working for <a href="http://www.sessiondigital.com/">Session Digital</a> as part of <a href="http://ibuildings.com">Ibuildings</a> and I highly recommend them to you, along with the <a href="http://www.magentocommerce.com/boards">Magento Forums</a>. Looking through these splintered resources can be time-consuming so in this article I give you the key details when building a module in Magento.<br />
<span id="more-1853"></span></p>
<p>To accompany this article, you will find at the end of the page series of recommended resource links, and a download containing the example module that is built in the examples shown, available in a variety of formats.  Hopefully these will help you continue to develop successfully with Magento!</p>
<h3>The Magento Way</h3>
<p>For those, seasoned in customising or maintaining other people&#8217;s software, you know that each has its own approach to things. You may not always agree with it or understand the logic behind it, but if you are going to work with it, you need to know what it is unless you want to go grey prematurely!</p>
<p>In this vein, before you make your next, or first, Magento module, it is helpful to understand some key concepts in how Magento is constructed. Magento is based on the <a href="http://framework.zend.com">Zend Framework</a> and follows a simple code discovery, or resolution order, when compiling. There are three directories in which it finds code. These are core, community and local. Firstly, the core code modules are considered.  Secondly, if code in the community modules are found that supercede it, they are used.  Finally if code is found in the local directory, that supercedes the community directory, this is used. Said another way, code modules that you develop and place in the local directory, have priority over community and the Magento core.</p>
<p>This has a number of benefits from a developmental point of view. Chief amongst these is to allow for a seamless upgrade path. You can develop new modules, integrate third-party modules from the community in to your install and know, with a reasonable amount of confidence, that when you upgrade to the next release of Magento your hard work will survive intact. So remember never to touch the core code as that can break, or severely hamper, future upgrades. Too often I see in the forums, complaints about upgrades breaking, yet references to changing or hacking core code.</p>
<p>This philosophy of extending existing functionality means that you can take a community module to achieve the majority of your requirements and then supplement the missing functionality with a local module. You get a core of the work and only update where it&#8217;s missing what your project requires.  Working in this way keeps development time and costs down, which is always good!</p>
<p>If you keep these things in mind whilst developing, it will help provide a simpler, more hassle-free development process as well as a more maintainable end-product.</p>
<h3>Directory Structure</h3>
<p>We have covered the ethos of Magento so we shall start getting our hands dirty and cover the filesystem structure of a standard module, by creating a our own module. Let’s say that you want to spruce up your Magento shop by combining your <a href="http://flickr.com">Flickr</a> feed of customers using your products on the front page of your site. What we will do in this tutorial is create a simple module that links to an existing Flickr account and render a configurable set of images.</p>
<p>To start us off we will assume the following:</p>
<ul>
<li><strong>Your Company:</strong> Widgets Inc</li>
<li><strong>Your Module:</strong> FlickrFeed</li>
<li>You have a <strong>Flickr</strong> account with the accompanying <strong>API</strong> key</li>
</ul>
<p></p>
<h3>Key Points</h3>
<p>The most important parts of a module are these:</p>
<ul>
<li>app/code/local/</li>
<li>app/etc/modules/</li>
</ul>
<p></p>
<p>In  <strong>app/code/local</strong> you will store all your modules. In <strong>app/etc/modules</strong>, you store your module specification file. The specification file is essential as it tells Magento that your module exists, making it available to use and configure. The local directory is where you will start to build your module repository. Analagous to <em>PHP namespaces</em>, <em>C++ includes</em>, or a <em>Java package</em>, the local directory provides a clear, professional and consistent method of organising and distributing your modules.</p>
<h3>Key Files</h3>
<p><strong>app/Mage.php</strong> This file cannot be underestimated when developing with Magento. It provides a series of static methods that make working with Magento much more straightforward. There is some debate about the merits of this approach, but when working with it, these utility methods make life simpler. Some common methods you can find here are:</p>
<p><strong>Mage::log</strong> Access the system logger</p>
<p><strong>Mage::getModel</strong> Access a system model object</p>
<p><strong>Mage::getStoreConfig</strong> Access the system configuration settings</p>
<h3>Standard Module Layout</h3>
<p>All modules follow the same structure. I’ll illustrate it using the company and module outlined above:</p>
<pre>WidgetsInc/FlickrFeed/
  Adminhtml (Contains  the configuration for the admin menus)
    Model
      Block (Contains front-end output)
        Admin
  controllers (Controllers for interacting with your module)
  etc (The main configuration directory)
    config.xml (Configuration options and default settings for your module)
    system.xml (Administration settings and admin form options for the module)
  Helper (Helpers for the module, covering areas such as translation)
    Data.php
  Model
    Mysql4 (The top ORM model directory)
      <your_module>
    Observer.php (Observers able to respond to system events)
  sql
    <your_module>_setup (Contains setup and update scripts for your module)</pre>
<p>In order to build the FlickrFeed module, we&#8217;ll need to add a number of files.  The following sections will demonstrate each of these in turn.</p>
<h3>config.xml</h3>
<p>This is the core config file for your module. A minimalist version is available in the example download which you can find at the end of this post. Now let’s take you through what it means.</p>
<p>Here is the core definition of your module.</p>
<ul>
<li><strong>version</strong> is essential as it links in with the <em><strong>install</strong></em> and <em><strong>upgrade</strong></em> scripts.</li>
<li><strong>depends</strong> lists the dependencies of your module. If the dependencies here are not satisfied, as you would assume, your module will not be enabled.</li>
<li><strong>codePool</strong> specifies in what resource your module is located.</li>
</ul>
<p></p>
<p>As you can assume, if these aren’t satisfied, the module will not load.</p>
<p>The models section sets up the data source connections and defines how to refer to it.</p>
<p>The resources section provides access to the system read, write and setup functionality for the module. If this section is not present, your module will be able to do very little.</p>
<p>The adminhtml section links the module in to the admin functionality, such as forms and configuration setups.</p>
<p>The routers section sets up the routes so that you can access the module.  Unsurprisingly, default set up the defaults for your module that can be accessed as required.</p>
<h3>system.xml</h3>
<p>This file allows you to define an admin form for setting the key options for the feed. If you have a look at the one contained in the sample module, you’ll see an element <strong>groups</strong>. These enble you to combine the fields to display in to logical groupings, which will be rendered in a fieldsets containing the form field elements.</p>
<p>After the groups, the next element provides the initialisation of the section and then under the element fields, you list the fields that are to be displayed in this grouping. Fields and fieldsets can be disabled by default, or specifically in the website and/or store through show_in_default, show_in_website and show_in_store. To auto-translate fields, list them in the attribute translate for each group or selection element.</p>
<h3>Managing Model Information</h3>
<p>Next we need to look at the model files that will all us to work retrieve information from the module. These files are:</p>
<pre>Model
  Feed.php
  Mysql4
    Feed.php
    Feed/
      Collection/
      Collection.php</pre>
<p></p>
<p>In Model/Mysql4/Feed.php  is the main model class, extending the core model class, making all of its functionality available. Model/Mysql4/Feed/Collection.php overrides the core model collection class, as with Feed.php, makes all the collection functionality available.</p>
<h4>Working with the Magento Data Model</h4>
<p>Now for the following example, let&#8217;s assume that we have a table structure similar to the following:</p>
<table>
<tr>
<th> Field         </th>
<th> Type             </th>
<th> Null </th>
<th> Key </th>
<th> Default </th>
<th> Extra          </th>
</tr>
<tr>
<td> address_id    </td>
<td> int(10) unsigned </td>
<td> NO   </td>
<td> PRI </td>
<td> NULL    </td>
<td> auto_increment </td>
</tr>
<tr>
<td> address1      </td>
<td> varchar(50)      </td>
<td> NO   </td>
<td>     </td>
<td> NULL    </td>
<td>                </td>
</tr>
<tr>
<td> address2      </td>
<td> varchar(50)      </td>
<td> NO   </td>
<td>     </td>
<td> NULL    </td>
<td>                </td>
</tr>
<tr>
<td> city_id       </td>
<td> int(10) unsigned </td>
<td> YES  </td>
<td>     </td>
<td> NULL    </td>
<td>                </td>
</tr>
<tr>
<td> state_county  </td>
<td> varchar(100)     </td>
<td> YES  </td>
<td>     </td>
<td> &#8211;       </td>
<td>                </td>
</tr>
<tr>
<td> post_code     </td>
<td> varchar(30)      </td>
<td> NO   </td>
<td>     </td>
<td> NULL    </td>
<td>                </td>
</tr>
<tr>
<td> country       </td>
<td> varchar(50)      </td>
<td> YES  </td>
<td>     </td>
<td> uk      </td>
<td>                </td>
</tr>
<tr>
<td> pobox         </td>
<td> varchar(50)      </td>
<td> YES  </td>
<td>     </td>
<td> &#8211;       </td>
<td>                </td>
</tr>
<tr>
<td> created_date  </td>
<td> datetime         </td>
<td> NO   </td>
<td>     </td>
<td> NULL    </td>
<td>                </td>
</tr>
<tr>
<td> last_modified </td>
<td> datetime         </td>
<td> NO   </td>
<td>     </td>
<td> NULL    </td>
<td>                </td>
</tr>
</table>
<p>
During a normal interaction with your model, you&#8217;re likely to retrieve, persist (save/update) and delete information from that model. So let&#8217;s look at how you can do this with Magento.</p>
<h3>Retrieving Data</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// get a handle on all the available feeds</span>
<span style="color: #000088;">$collection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flickrfeed/feed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$collection</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>count<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// store the retrieved feeds</span>
    <span style="color: #000088;">$feedOutput</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: #666666; font-style: italic;">// iterate over the retrieved retrieved collection</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$collection</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$flickrFeedItem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$feedOutput</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$flickrFeedItem</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></p>
<h3>Filtering the information</h3>
<p>Now what if you did not require all the available data at once? There are so many situations where you do not want every record available. Magento has plenty of ways of filtering data but for this example we&#8217;ll use: <strong>addFieldToFilter(&lt;field_name&gt;, &lt;field_value&gt;)</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$collection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flickrfeed/feed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCollection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                               <span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFieldToFilter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post_code'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'SE11'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></p>
<p>After adding to our previous call to getCollection, we have filtered down our returned records to just those with a post_code value of SE11.  With the fluid interface we can call this as many times as we need to in order to meet our filtering requirements.</p>
<h3>Persisting Data</h3>
<p>We will also need to save data and update records, so here is a simple code example to illustrate:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flickrfeed/feed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></p>
<p>In this instance, you&#8217;re calling the setData method on your model, which itself extends <strong>Mage_Core_Model_Mysql4_Abstract</strong>. The <strong>$data</strong> variable will be an associative array where the keys are the names of the columns in which you want the data saved. Then, using the fluent interface, further call the <strong>save()</strong> method and the data will then be saved by your model. Now, like all OOP classes, you don&#8217;t need to accept the parent setData() and save() methods; you can override these should you wish to. It is likely that the default may be quite acceptable in the majority of cases however.</p>
<h3>Deleting Data</h3>
<p>Now, let&#8217;s look at deleting records. Have a look at the code snippet below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// get a handle on all the available feeds</span>
<span style="color: #000088;">$collection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flickrfeed/feed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCollection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$collection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// iterate over the retrieved retrieved collection</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$collection</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$flickrFeedItem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        try <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$flickrFeedItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// note that the item couldn't be deleted.</span>
            Mage<span style="color: #339933;">::</span><span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span>
            <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't delete record. [<span style="color: #009933; font-weight: bold;">%s</span>]&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">var_export</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_item</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            Zend_Log<span style="color: #339933;">::</span><span style="color: #004000;">ERR</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p></p>
<p>This example shows a simple way to delete records. After retrieving a collection, iterate over it using foreach and call delete on each item. Assuming that the method does not throw an exception, your records are deleted. We can also make things easier by creating a method for bulk deletion of records.  In the model class, in our case <strong>FlickrFeed_Model_Mysql4_Feed</strong>, add a deleteAll() method similar to below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deleteAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
      <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">// note that the item couldn't be deleted.</span>
          Mage<span style="color: #339933;">::</span><span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Attempting to clear all records&quot;</span><span style="color: #339933;">,</span> Zend_Log<span style="color: #339933;">::</span><span style="color: #004000;">INFO</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #666666; font-style: italic;">// get a handle on all the available feeds</span>
          <span style="color: #000088;">$collection</span> <span style="color: #339933;">=</span> Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flickrfeed/feed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getCollection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #666666; font-style: italic;">// attempt to delete all of them</span>
          <span style="color: #666666; font-style: italic;">// not the most ideal way. </span>
          <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$collection</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              try <span style="color: #009900;">&#123;</span> 
                  <span style="color: #000088;">$item</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>delete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
              <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// note that the item couldn't be deleted.</span>
                Mage<span style="color: #339933;">::</span><span style="color: #990000;">log</span><span style="color: #009900;">&#40;</span>
                  <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Couldn't delete record. [<span style="color: #009933; font-weight: bold;">%s</span>]&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">var_export</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_item</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
                  Zend_Log<span style="color: #339933;">::</span><span style="color: #004000;">ERR</span>
                  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p></p>
<p>Now all you need to do is call:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Mage<span style="color: #339933;">::</span><span style="color: #004000;">getModel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'flickrfeed/feed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteAll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p></p>
<h3>Logging</h3>
<p>Since delete() can throw an exception if something goes wrong, this is a good time to show another Magento utility method: <strong>log</strong>. As you would assume, this gives quick access to the system log. Just pass in the message that you want logged and the log level, exactly as you would when working with Zend_Log, upon which this is based. Mage has a number of other methods, including getBaseUrl(), getStoreConfig(), getVersion() and dispatchEvent() that I highly encourage you to review.</p>
<h3>Installing and Upgrading Modules</h3>
<p>As mentioned earlier, one of the key aspects of modules is the value of version in <strong>/etc/config.xml</strong>. Whenever Magento looks at your module this value is checked; usually when you look at <strong>Admin</strong> <strong>-&gt; system -&gt; configuration -&gt; advanced</strong> and click &#8216;<strong>Save Config</strong>&#8216;.</p>
<p>The version value is compared against the value of <strong>core_resource.version</strong> in the database, where <strong>core_resource.code</strong> matches the name of your module. If the value in the file is higher than in the table column, then Magento looks for files in <strong>sql/flickrfeed_setup/</strong> with a filename being equal to or higher than that of that found in config.xml.</p>
<p>If you want to re-run setup/upgrade routines during testing or migration, simply remove references to your modules in core_resource and then, in &#8216;<strong>advanced</strong>&#8216;, click &#8216;<strong>Save Config</strong>&#8216; again. Magento will run all your scripts again.</p>
<p>We can now create the files for managing install and updates. Under your module, create the following file structure:</p>
<pre>sql</pre>
<pre>  flickrfeed_setup</pre>
<pre>  mysql4-install-0.1.0.php</pre>
<p>At this point you have the basis of your module. It will install and you will be able to call it!</p>
<h3>Automation with Cron</h3>
<p>Magento honours the tradition of using tools to use jobs so we you don&#8217;t have to by linking to <a href="http://en.wikipedia.org/wiki/Cron">Cron</a> on UNIX based systems. For those unfamiliar with this utility, it is a UNIX daemon that wakes every minute, checks if it is time to run a job, if so, spawns them, and returns to sleep, waking a minute later to run all over again.</p>
<p>To get access to this automation in Magento, have a look at the snippet from config.xml listed below.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;crontab<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jobs<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;publish_product_photos<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #808080; font-style: italic;">&lt;!-- run every 2 minutes for testing purposes --&gt;</span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;schedule<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;cron_expr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>*/2 * * * *<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cron_expr<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/schedule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;run<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;model<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>flickrfeed/observer::publishProductPhotos<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/model<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/run<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/publish_product_photos</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jobs<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/crontab<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p></p>
<p>Place this snippet inside the root level of <strong>config.xml</strong>. This will cause <strong>flickrfeed/observer::publishProductPhotos </strong> to be executed every two minutes. For maintenance sake, we&#8217;ve labeled the task <strong>publish_product_photos</strong>. Inside that module, you can put in any Magento/PHP code that you need to automate. You can imagine that in this function, you could be performing actions such as checking for product photos that have not been published and publishing them.</p>
<h3>Observing Application Events</h3>
<p>Working with Magento can involve working with a lot of other code, whether that&#8217;s the Magento core, or third-party modules. To extend this is not always feasible and can be unecessary, leading to increased development overhead. Why not save your time and development budgets by simply listening for the events occurring and respond when they occur? Your module has the benefits of being self-contained and extremely loosely coupled. You are not necessarily bound to make changes when the module maintainer, or Varien, updates their code.</p>
<p>Now, let&#8217;s look at how we would observe an event. Magento calls events via the <strong>Mage::dispatchEvent</strong>. Given that there is not a comprehensively documented events list, here&#8217;s a few examples.</p>
<p>/app/code/core/mage/</p>
<ul>
<li>./Sales/Model/Order/Invoice.php:
<ul>
<li>sales_order_invoice_pay</li>
<li>sales_order_invoice_cancel</li>
</ul>
</li>
<li>./Sales/Model/Convert/Quote
<ul>
<li>sales_convert_quote_to_order</li>
<li>sales_convert_quote_address_to_order</li>
<li>sales_convert_quote_address_to_order_address</li>
<li>sales_convert_quote_payment_to_order_payment</li>
<li>sales_convert_quote_item_to_order_item</li>
</ul>
</li>
<li>./Sales/Model/Order.php
<ul>
<li>sales_order_place_after</li>
</ul>
</li>
<li>./Catalog/Model/Convert/Adapter/Product.php
<ul>
<li>catalog_product_import_after</li>
</ul>
</li>
</ul>
<p></p>
<p>To find a comprehensive list, you can run the following from the shell:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-rn</span> Mage::dispatchEvent app<span style="color: #000000; font-weight: bold;">/</span>code<span style="color: #000000; font-weight: bold;">/</span>core<span style="color: #000000; font-weight: bold;">/*</span> <span style="color: #660033;">--include</span>=<span style="color: #ff0000;">'*.php'</span></pre></div></div>

<p>You could refine this to make the output simpler to read if you wanted to. Alternatively, just search for Mage::dispatchEvent in your favourite editor. In our example we can observe <strong>catalog_product_import_after</strong>. Below is a snippet from config.xml.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;events<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #808080; font-style: italic;">&lt;!--listen for the product save action--&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;catalog_product_import_after<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;observers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;update_flickr_feed<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>model<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>FlickrFeed_Model_Observer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>publishCategoryPhotos<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/update_flickr_feed<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/observers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/catalog_product_import_after<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/events<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Put this section inside <strong>config -&gt; adminhtml</strong> in <strong>/etc/config.xml</strong> of your module. When the catalog_product_import_after event is called, the system will call FlickrFeed::publishCategoryPhotos. What could be simpler? In your module&#8217;s method, you can put any valid Magento/PHP code that suits your needs. In this case, you could use access to the FlickrApi to publish a selection of photos from the latest category that you&#8217;ve just added.</p>
<p>All you need to do is determine the event you want to listen for, determine if there&#8217;s a suitable system event thrown and setup your module in the above fashion.</p>
<h3>Deployment</h3>
<p>When you are ready to deploy the module from your local development to your production server, just take the module directory you have built and copy it to the same directory on your production server. One word of caution, if you&#8217;re moving from one version of Magento to another, or from Community Edition to Enterprise, you may encounter some issues. So to minimise your deployment issues, aim to use for your development platform an edition and version of Magento as close as possible to your live version.</p>
<h3>Download the Sample Code</h3>
<p>We provide for you a zip file containing the <a href='http://techportal.ibuildings.com/wp-content/uploads/2010/04/FlickrFeed-Code.zip'>FlickrFeed Code</a>for you to download and try out if you would like to.</p>
<h3>Magento Connect</h3>
<p>Provided by Magento, <a href="http://www.magentocommerce.com/magento-connect" target="_blank">Magento Connect</a> is where you can find a large and diverse range of modules, or extensions, to Magento. They allow you to quickly and simply extend your installation with new payment gateways, Google Maps integration and a lot more. Modules come in two flavours, free or commercially paid for, with varying support options available.</p>
<p>There is not much difference between the two formats. The modules are all hosted on magento-connect, but when a module is offered commercially, the seller, not Varien, handles payment. A magento-connect account is required, through which you can manage the details of the module.</p>
<h2>Resources for Further Reading</h2>
<p>An assortment of links to other sites and posts which contain information to help you on your quest to master Magento:</p>
<ul>
<li><a href="http://alanstorm.com/" target="_blank">http://alanstorm.com/</a></li>
<li><a href="http://www.exploremagento.com/magento">http://www.exploremagento.com/magento</a></li>
<li><a href="http://classyllama.com/development/magento-development/">http://classyllama.com/development/magento-development/</a></li>
<li><a href="http://inchoo.net/ecommerce/magento/">http://inchoo.net/ecommerce/magento/</a></li>
<li><a href="http://www.magentocommerce.com/wiki/modules_reference/danish/mage_adminhtml/extensions_custom/how_to_add_setup_pricing_to_a_product" target="_blank">Magento &#8211; Wiki &#8211; How to Add Setup Pricing to a Product</a></li>
<li><a href="http://inchoo.net/ecommerce/magento/magento-custom-emails/" target="_blank">Magento custom emails</a></li>
<li><a href="http://classyllama.com/development/magento-development/enable-templateblock-hints-in-admin-panel/" target="_blank">Enable Template/Block Hints in Admin Panel » Classy Llama Studios – Specializing in Magento eCommerce, Magento Development, and Magento Design</a></li>
<li><a href="http://www.spinonesolutions.com/2009/09/magento-custom-module-part-i/" target="_blank">Magento – Part I : Custom Module &#8211; SpinOneSolutions</a></li>
<li><a  href="http://www.exploremagento.com/magento/override-a-magento-core-block-class.php/comment-page-1#comment-913" target="_blank">Override a Magento Core Module &#8211; Magento Custom Module &#8211; Magento Development | Explore Magento</a></li>
<li><a  href="http://www.magentonews.co.uk/magento-development/overriding-core-models-with-a-custom-module/" target="_blank">Overriding Core Models with a Custom Module | Magento News</a></li>
<li><a href="http://yviktorov.github.com/magento/2009/05/23/override-email-templates.html" target="_blank">How to override email templates in Magento :: Yevgeniy Viktorov&#8217;s blog</a></li>
</ul>
<p></p>
<h3>In Summary</h3>
<p>I hope this tutorial is helpful to you in building custom modules and finding your way around Magento.  Do let us know in the comments what custom modules you&#8217;ve been building!</p>



Like it? Share it!


	<a rel="nofollow"  href="mailto:?subject=%20Writing%20a%20Custom%20Magento%20Module&amp;body=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F" title="email"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=%20Writing%20a%20Custom%20Magento%20Module%20-%20http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F" title="Twitter"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F&amp;t=%20Writing%20a%20Custom%20Magento%20Module" title="Facebook"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F&amp;title=%20Writing%20a%20Custom%20Magento%20Module&amp;bodytext=The%20aim%20of%20this%20tutorial%20is%20to%20provide%20a%20good%20reference%20guide%20to%20keep%20with%20you%20when%20developing%20custom%20modules%20with%20ecommerce%20solution%20Magento.%20It%20details%20some%20of%20the%20key%20sections%20of%20how%20to%20build%20a%20custom%20Magento%20module%2C%20some%20of%20the%20concepts%2C%20assumpti" title="Digg"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F&amp;title=%20Writing%20a%20Custom%20Magento%20Module" title="StumbleUpon"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F&amp;title=%20Writing%20a%20Custom%20Magento%20Module&amp;notes=The%20aim%20of%20this%20tutorial%20is%20to%20provide%20a%20good%20reference%20guide%20to%20keep%20with%20you%20when%20developing%20custom%20modules%20with%20ecommerce%20solution%20Magento.%20It%20details%20some%20of%20the%20key%20sections%20of%20how%20to%20build%20a%20custom%20Magento%20module%2C%20some%20of%20the%20concepts%2C%20assumpti" title="del.icio.us"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F&amp;title=%20Writing%20a%20Custom%20Magento%20Module&amp;source=techPortal+Tutorials+for+better+PHP&amp;summary=The%20aim%20of%20this%20tutorial%20is%20to%20provide%20a%20good%20reference%20guide%20to%20keep%20with%20you%20when%20developing%20custom%20modules%20with%20ecommerce%20solution%20Magento.%20It%20details%20some%20of%20the%20key%20sections%20of%20how%20to%20build%20a%20custom%20Magento%20module%2C%20some%20of%20the%20concepts%2C%20assumpti" title="LinkedIn"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F&amp;title=%20Writing%20a%20Custom%20Magento%20Module" title="Reddit"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F" title="Technorati"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.dzone.com/links/add.html?url=http%3A%2F%2Ftechportal.ibuildings.com%2F2010%2F04%2F20%2Fwriting-a-custom-magento-module%2F&amp;title=%20Writing%20a%20Custom%20Magento%20Module" title="DZone"><img src="http://techportal.ibuildings.com/wp-content/plugins/sociable/images/dzone.png" title="DZone" alt="DZone" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://techportal.ibuildings.com/2010/04/20/writing-a-custom-magento-module/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
