<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nocarrier &#187; php</title>
	<atom:link href="http://nocarrier.co.uk/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://nocarrier.co.uk</link>
	<description>PHP and some other bits...</description>
	<lastBuildDate>Sat, 12 Jun 2010 09:11:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>LOLCODE and PHP</title>
		<link>http://nocarrier.co.uk/2010/06/lolcode-and-php/</link>
		<comments>http://nocarrier.co.uk/2010/06/lolcode-and-php/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 09:11:13 +0000</pubDate>
		<dc:creator>blongden</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php lolcode github]]></category>

		<guid isPermaLink="false">http://nocarrier.co.uk/2010/06/lolcode-and-php/</guid>
		<description><![CDATA[As a proof of concept i&#8217;ve been working on a small LOLCODE (lolcode.com) interpreter thing&#8230; for PHP. Yes, this is a basic implementation of LOLCODE (an interpreted language) written in PHP (an interpreted language). At the moment there&#8217;s enough implemented to parse and run the following&#8230; HAI. BTW THIS IS A COMMENT. VISIBLE &#8220;OH HAI, [...]]]></description>
			<content:encoded><![CDATA[<p>As a proof of concept i&#8217;ve been working on a small LOLCODE (lolcode.com) interpreter thing&#8230; for PHP.</p>
<p>Yes, this is a basic implementation of LOLCODE (an interpreted language) written in PHP (an interpreted language).</p>
<p>At the moment there&#8217;s enough implemented to parse and run the following&#8230;</p>
<p>HAI. BTW THIS IS A COMMENT. VISIBLE &#8220;OH HAI, WORLD&#8221;. KTHXBAI.</p>
<p>You can find the source code up on github at <a href="http://github.com/blongden/lolcode-php">github</a> if you want to have a play (just extend \Lol\Token &#8211; see visible.php for an example).  </p>
]]></content:encoded>
			<wfw:commentRss>http://nocarrier.co.uk/2010/06/lolcode-and-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ORM ORLY?</title>
		<link>http://nocarrier.co.uk/2009/11/orm-orly/</link>
		<comments>http://nocarrier.co.uk/2009/11/orm-orly/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 17:41:27 +0000</pubDate>
		<dc:creator>blongden</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://nocarrier.co.uk/?p=34</guid>
		<description><![CDATA[I&#8217;m regularly thinking about how to represent data in a relational database in OO PHP5 that doesn&#8217;t make me walk away feeling like i&#8217;ve just created something that smells bad. The key thing for me is that my code should not care that the data is coming from a relational database. This poses one or [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m regularly thinking about how to represent data in a relational database in OO PHP5 that doesn&#8217;t make me walk away feeling like i&#8217;ve just created something that smells bad.</p>
<p>The key thing for me is that my code should not care that the data is coming from a relational database.  This poses one or two issues.</p>
<ul>
<li> In a relational database, data typically has other &#8216;meta&#8217; data associated with it. Id&#8217;s, timestamps and other snippets of information that are not strictly part of what my object is trying to represent.</li>
<li>If my object is not aware of how it should serialise itself, who is?</li>
</ul>
<p>This of course isn&#8217;t a new problem.  My language of choice (currently PHP5) has many ORM libraries available to it &#8211; Doctrine, Propel and more &#8211; most (or all) of which are loosely based around the Active Record design pattern.</p>
<p>My only problem with these frameworks is that they seem to do too much &#8216;magic&#8217; for me (and I like to retain at least some control over what&#8217;s happening between my app and the database).  Of course, it could also be that I have either not invested enough time in learning to use one of them to it&#8217;s maximum potential.</p>
<p>Despite that, the Active Record pattern appears to be quite a satisfactory way of creating that link between my application, and how it&#8217;s storing it&#8217;s data.  My core objects can exist how I want them to, and their &#8216;datastore&#8217; can be represented by an active record object (who&#8217;s attributes are exactly the same as the columns in the database) on the class itself.  It gives me the degree of separation that i&#8217;ve been looking for.</p>
<p>However there&#8217;s still a piece missing.  My core class still has to contain the relevant logic to load it&#8217;s associated active record.  This suggests some static factory methods (I refer to my earlier comment on things that smell bad!), so i&#8217;m going to create a &#8216;Builder&#8217; for my core object that&#8217;s concerned with marrying up the active record with the core object itself.  Here&#8217;s how it looks (simplified) in a class diagram.</p>
<div id="attachment_35" class="wp-caption alignnone" style="width: 456px"><img class="size-full wp-image-35" title="class-diagram" src="http://nocarrier.co.uk/wp-content/uploads/2009/11/class-diagram.png" alt="ActiveRecord class diagram" width="446" height="336" /><p class="wp-caption-text">ActiveRecord class diagram</p></div>
<p>I&#8217;ve not closed the doors on Doctrine or Propel though, so it&#8217;ll be interesting so see just what an established ORM framework like Doctrine can do for me on top of this basic implementation.</p>
]]></content:encoded>
			<wfw:commentRss>http://nocarrier.co.uk/2009/11/orm-orly/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit testing protected/private methods in a class</title>
		<link>http://nocarrier.co.uk/2009/06/unit-testing-protectedprivate-methods-in-a-class/</link>
		<comments>http://nocarrier.co.uk/2009/06/unit-testing-protectedprivate-methods-in-a-class/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 16:34:27 +0000</pubDate>
		<dc:creator>blongden</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[unittests]]></category>

		<guid isPermaLink="false">http://nocarrier.co.uk/?p=31</guid>
		<description><![CDATA[I think a lot of people when getting into unit testing naturally assume (and are told) that typically, a unit is a single method or function within your code, and each unit should have a unit test. To an extent this is true &#8211; until you are presented with what you do with protected or [...]]]></description>
			<content:encoded><![CDATA[<p>I think a lot of people when getting into unit testing naturally assume (and are told) that typically, a unit is a single method or function within your code, and each unit should have a unit test.</p>
<p>To an extent this is true &#8211; until you are presented with what you do with protected or private methods within classes.</p>
<p>There are two camps of thought on this issue.</p>
<p>One (that I completely and utterly disagree with) says that these methods ARE units in their own right, and should be tested as such (using introspection, or language hacks etc). In PHP this can be accomplished by abusing the __call method in your class (to allow a test suite to call protected or private methods on your class).</p>
<p>The second, is that a &#8216;unit&#8217; is actually a single call to the public interface of your class. The protected and private methods that are called within the class are implementation details and should be allowed to be refactored entirely. Providing the behaviour and the public interface of the class does not change, refactoring does not involve ANY modification to the class itself.</p>
<p>Because the implementation details of a class do not constitute a unit in it&#8217;s own right, testing these methods in isolation are therefore incomplete and actually invalid (as they never have the context in which they are used, and even though your code may show 100% lines of code coverage, the branch coverage will always be far from complete).</p>
<p>So &#8211; if you&#8217;re tempted to create a test for a protected/private method within a class, then you&#8217;re moving away from unit tests and into protecting your implementation using a unit testing framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://nocarrier.co.uk/2009/06/unit-testing-protectedprivate-methods-in-a-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Optimising PHP (a start&#8230;)</title>
		<link>http://nocarrier.co.uk/2009/05/optimising-php-a-start/</link>
		<comments>http://nocarrier.co.uk/2009/05/optimising-php-a-start/#comments</comments>
		<pubDate>Fri, 22 May 2009 09:24:28 +0000</pubDate>
		<dc:creator>blongden</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://nocarrier.co.uk/?p=20</guid>
		<description><![CDATA[Scaling to thousands of users is often a problem for web applications &#8211; and in fact any system that has a sudden large influx of new users. In recent times we have had a situation where we had inherited a fairly simple web application that was massively under performing when a single user accessed a [...]]]></description>
			<content:encoded><![CDATA[<p>Scaling to thousands of users is often a problem for web applications &#8211; and in fact any system that has a sudden large influx of new users. In recent times we have had a situation where we had inherited a fairly simple web application that was massively under performing when a single user accessed a reasonably large dataset from a MySQL database.</p>
<p>Immediately we blamed the database &#8211; it must be the bottleneck &#8211; located on a different server and transferring a result in excess of 10,000 records. But when performing the query from a DB client there was virtually nothing in terms of a delay.</p>
<p>So what was the problem? The code was reasonably simple. An in house database abstraction layer was used to construct a representation of each row returned from the database and stored it in a big array. Common OO style promoted the use of a series of &#8216;setter&#8217; methods to store the data in each constructed object.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$animal</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$mineral</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$vegetable</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setAnimal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$animal</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">animal</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$animal</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setMineral<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mineral</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mineral</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mineral</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setVegetable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$vegetable</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vegetable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vegetable</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This block of code simulated how each of the objects were being built &#8211; using the data as returned from the database.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$test</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAnimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lion'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$test</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMineral</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'stone'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$test</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setVegetable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'potato'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Surprisingly (or perhaps not&#8230;) this code takes around 4.5 seconds to run (obviously depends on hardware!). Quite an overhead when you&#8217;re targeting a 2 seconds per page load time!</p>
<p>Looking at the code &#8211; the constructor is clearly redundant. It&#8217;s not serving any useful purpose. Removing it cut off around half a second of page load time (average).</p>
<p>Removing one of the setters saved another second or so&#8230; removing all the setters (just constructing a blank object 10,000 times cut&#8217;s it down to 1/10th of a second! Could method calls in php be that expensive?</p>
<p>We can create the same result by doing all of this in a constructor &#8211; consider the following.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$animal</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$mineral</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$vegetable</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$animal</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mineral</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vegetable</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">animal</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$animal</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mineral</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mineral</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vegetable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vegetable</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lion'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'stone'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'potato'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This cut&#8217;s things down to just under two seconds &#8211; definitely better &#8211; but not where we want to be. We know that the presence of the constructor (presumably it&#8217;s just the overhead of the method call itself) is costing us some time. What if the class was simply a data container and offered nothing in the way of accessors?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClass
<span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$animal</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$mineral</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$vegetable</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$test</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">animal</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'lion'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$test</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">mineral</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'stone'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$test</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">vegetable</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'potato'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>3/10ths of a second. Ok &#8211; now we are getting somewhere! For 10k results &#8211; this is probably acceptable.</p>
<p>It appears that object creation and method calling is actually quite an expensive operation within PHP &#8211; if we actually had to deal with 100k rows, the fastest way of doing it is always to just use an array. The 10k test completes in less than 1/10th of a second.</p>
<p>A more accurate analysis of just how much object instantiation and method calls cost could be done using xdebug to profile how long each line of code actually takes to execute. But for my purposes, this worked out well.</p>
]]></content:encoded>
			<wfw:commentRss>http://nocarrier.co.uk/2009/05/optimising-php-a-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
