<?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>Squirrel Hacker &#187; gist</title>
	<atom:link href="http://blog.seanja.com/tag/gist/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.seanja.com</link>
	<description>I have come forward in time from the year 1986 to share this with you</description>
	<lastBuildDate>Mon, 06 Feb 2012 12:38:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Orms and Circular References</title>
		<link>http://blog.seanja.com/2010/05/orms-and-circular-references/</link>
		<comments>http://blog.seanja.com/2010/05/orms-and-circular-references/#comments</comments>
		<pubDate>Thu, 27 May 2010 05:17:54 +0000</pubDate>
		<dc:creator>SeanJA</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[circular relationships]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[class car]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Constructor]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[Ford]]></category>
		<category><![CDATA[Ford Motor Company]]></category>
		<category><![CDATA[Ford Pinto]]></category>
		<category><![CDATA[gist]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[Method]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[static functions]]></category>

		<guid isPermaLink="false">http://blog.seanja.com/?p=998</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://blog.seanja.com/2010/05/orms-and-circular-references/' addthis:title='Orms and Circular References '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>In my spare time I have been working on an ORM&#8230; not one meant to be used in the real world (well&#8230; not yet anyway). I currently have hasOne and hasMany working like so: &#60;?php &#160; /** * @property int $id * @property string $type * @property int $car_id */ class wheelBoilerplate extends orm&#123; protected [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.seanja.com/2010/05/orms-and-circular-references/' addthis:title='Orms and Circular References ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>
Related posts:<ol>
<li><a href='http://blog.seanja.com/2011/06/around-the-world-lyrics/' rel='bookmark' title='Around the World lyrics'>Around the World lyrics</a></li>
<li><a href='http://blog.seanja.com/2009/02/links-for-2009-02-11/' rel='bookmark' title='links for 2009-02-11'>links for 2009-02-11</a></li>
<li><a href='http://blog.seanja.com/2009/02/templating/' rel='bookmark' title='Templating'>Templating</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://blog.seanja.com/2010/05/orms-and-circular-references/' addthis:title='Orms and Circular References '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>In my spare time I have been working on an ORM&#8230; not one meant to be used in the real world (well&#8230; not yet anyway). I currently have hasOne and hasMany working like so:</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>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * @property int $id
 * @property string $type
 * @property int $car_id
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> wheelBoilerplate <span style="color: #000000; font-weight: bold;">extends</span> orm<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</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;">addProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'type'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'default'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'monster truck'</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</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;">hasOne</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'car'</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;">class</span> wheel <span style="color: #000000; font-weight: bold;">extends</span> wheelBoilerplate<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * @property int $id
 * @property int $wheel_count
 * @property array $wheels
 * @property string $type
 */</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> carBoilerplate <span style="color: #000000; font-weight: bold;">extends</span> orm<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</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;">addProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wheel_count'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'default'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'type'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'int'</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</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;">addProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'default'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'monster truck'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'type'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</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;">hasMany</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wheels'</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;">class</span> car <span style="color: #000000; font-weight: bold;">extends</span> carBoilerplate<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$car</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> car<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">#=&gt; 'monster truck'</span></pre></div></div>

<p>The problem as you can probably guess, is you now have 4 of the same car loaded into memory, and 4 wheels, 2 of which are the same. So, when you do this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Ford Pinto'</span><span style="color: #339933;">;</span></pre></div></div>

<p>You are only actually changing one instance of all of the instances of car that you have created in memory. Not so good for keeping things straight, which one do you save? What happens if you save one over top of the other?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Ford Pinto'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$car</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>
<span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">car</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>You have just overwritten your changes in the database. I didn&#8217;t think that was a good thing to let someone do (even accidentally). That is where this class came from:</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>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> factory <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Holds all of the items in the factory
	 * @var array
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> static <span style="color: #000088;">$created</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: #009933; font-style: italic;">/**
	 * Create/Load an object from the factory
	 * @param string $className
	 * @param int $id
	 * @return orm
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> Create<span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$className</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</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: #339933;">!</span><span style="color: #990000;">isset</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;">$created</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$created</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$className</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: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$created</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Load an object from the factory
	 * @param string $className the object's class
	 * @param int $id the id of the object you are loading
	 * @return orm or null
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> Load<span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #339933;">,</span> <span style="color: #000088;">$id</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$className</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</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;">$created</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>? <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$created</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#93;</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>
	<span style="color: #009933; font-style: italic;">/**
	 * Add an object to the factory, has no effect if the object is already part of the factory
	 * @param string $className
	 * @param orm $class
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> Add<span style="color: #009900;">&#40;</span>orm <span style="color: #339933;">&amp;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$className</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">class_name</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: #339933;">!</span><span style="color: #990000;">isset</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;">$created</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$created</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$class</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Destroy an instance of something in the factory and the object itself
	 * @param orm $class the class being destroyed
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> Destroy<span style="color: #009900;">&#40;</span>orm <span style="color: #339933;">&amp;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$className</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">class_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">unset</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;">$created</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$id</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$class</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>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I haven&#8217;t had the chance to actually merge them together, but this is how it should work:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$car</span> <span style="color: #339933;">=</span> factory<span style="color: #339933;">::</span><span style="color: #004000;">Create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'car'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</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: #666666; font-style: italic;">//#=&gt; factory::Create('wheel', 1);</span>
<span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</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: #666666; font-style: italic;">//#=&gt; factory::Create('wheel', 2);</span>
<span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//#=&gt; factory::Create('wheel', 3);</span>
<span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//#=&gt; factory::Create('wheel', 4);</span>
&nbsp;
<span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">car</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//#=&gt; factory::Load('car', 1);</span>
<span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</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: #666666; font-style: italic;">//#=&gt; factory::Load('wheel', 1);</span>
&nbsp;
<span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">wheels</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Ford Pinto'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$car</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">type</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//#=&gt; 'Ford Pinto'</span></pre></div></div>

<p>Now, no matter which of the cars that you load (as long as it has the same ID), it will be the same one instead of just another instance of the same one. Changes are made to all of them, because they are all the same instance of that car.</p>
<p>The factory class and some sample usage code are <a href="http://gist.github.com/409112">on github</a>, if&#8217;n you want to look at it, or fork it, or whatever.</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://blog.seanja.com/2010/05/orms-and-circular-references/' addthis:title='Orms and Circular References ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div><p>Related posts:<ol>
<li><a href='http://blog.seanja.com/2011/06/around-the-world-lyrics/' rel='bookmark' title='Around the World lyrics'>Around the World lyrics</a></li>
<li><a href='http://blog.seanja.com/2009/02/links-for-2009-02-11/' rel='bookmark' title='links for 2009-02-11'>links for 2009-02-11</a></li>
<li><a href='http://blog.seanja.com/2009/02/templating/' rel='bookmark' title='Templating'>Templating</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.seanja.com/2010/05/orms-and-circular-references/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

