<?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/tag/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>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>My solution to the problem of VAT&#8230;</title>
		<link>http://nocarrier.co.uk/2008/11/my-solution-to-the-problem-of-vat/</link>
		<comments>http://nocarrier.co.uk/2008/11/my-solution-to-the-problem-of-vat/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 20:15:28 +0000</pubDate>
		<dc:creator>blongden</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://nocarrier.co.uk/?p=8</guid>
		<description><![CDATA[After lot&#8217;s of people spent a lot of time at work refactoring bad/old code after Alistair Darling announced that VAT would be reduced to 15% instead of 17.5% (and that people needed to &#8216;contact their software vendors to update the VAT field&#8230; riiight), I thought i&#8217;d stub out my own design for handling Price and [...]]]></description>
			<content:encoded><![CDATA[<p>After lot&#8217;s of people spent a lot of time at work refactoring bad/old code after Alistair Darling announced that VAT would be reduced to 15% instead of 17.5% (and that people needed to &#8216;contact their software vendors to update the VAT field&#8230; riiight), I thought i&#8217;d stub out my own design for handling Price and Tax (whether it&#8217;s VAT or not shouldn&#8217;t really matter). Here&#8217;s a start. It follows the idea that where possible, a class should be immutable. Don&#8217;t modify an existing cost, create a new one and return that. Anyway &#8211; here&#8217;s the code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Price
<span style="color: #009900;">&#123;</span>
	protected <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	protected <span style="color: #000088;">$currency</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;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$currency</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'GBP'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">currency</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$currency</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> getValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCurrency<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">currency</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> __toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$this-&gt;value}</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s all you should need for the price. Think about the money in your pocket &#8211; it has two property&#8217;s. It has a value, and it has a currency. I cannot change either without spending it (or converting it at a bureau de change), but then I would get a set of new coins in return. It has no concept of whether or not it is inclusive or exclusive of tax. Thats up to something else&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> TaxMan
<span style="color: #009900;">&#123;</span>
	protected static <span style="color: #000088;">$vat</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'GBP'</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">15.0</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	protected <span style="color: #000088;">$currency</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</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;">$currency</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$currency</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$vat</span><span style="color: #009900;">&#41;</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;">currency</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$currency</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cannot build a TaxMan for this currency'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</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> getVat<span style="color: #009900;">&#40;</span>Price <span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Price<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$price</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$vat</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">currency</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</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;">currency</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></td></tr></table></div>

<p>So we can pass in a price object and get a new value representing how much vat should be added&#8230; Something like this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$price</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Price<span style="color: #009900;">&#40;</span><span style="color:#800080;">8.00</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$taxman</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TaxMan<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GBP'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$vat</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$taxman</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getVat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$price</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Price<span style="color: #009900;">&#40;</span><span style="color: #000088;">$price</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$vat</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$price</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCurrency</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is not completely finished however (and I may come back to it to complete it), as it&#8217;s completely possible for multiple different tax levels within a currency (Currency can be used in multiple countries, or different products have varying levels of VAT associated with them). This will solve some of the problem &#8211; but there&#8217;s more work to be done.</p>
]]></content:encoded>
			<wfw:commentRss>http://nocarrier.co.uk/2008/11/my-solution-to-the-problem-of-vat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
