<?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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Pot Stuck</title>
	
	<link>http://www.potstuck.com</link>
	<description />
	<lastBuildDate>Thu, 29 Dec 2011 02:54:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/PotStuck" /><feedburner:info uri="potstuck" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Ruby symbols instead of blocks</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/__S4gMQSfLQ/</link>
		<comments>http://www.potstuck.com/2011/08/06/ruby-symbols-instead-of-blocks/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 19:35:33 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Default]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=491</guid>
		<description><![CDATA[Every wonder why this works? It&#8217;s straight forward and easy to read, but how does it work internally in Ruby? &#91;1, 2, 3&#93;.inject&#40;&#38;:+&#41; # =&#62; 6 Meet &#038; The &#038; operator in Ruby lets us go from Proc to block and vise-versa, but only in certain places. In fact, there are two two places where [...]]]></description>
			<content:encoded><![CDATA[<p>Every wonder why this works?  It&#8217;s straight forward and easy to read, but how does it work internally in Ruby?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; 6</span></pre></div></div>

<h2>Meet &#038;</h2>
<p>The &#038; operator in Ruby lets us go from Proc to block and vise-versa, but only in certain places.  </p>
<p>In fact, there are two two places where we can use &#038;</p>
<p>In a method definition &#038;blk will turn the blk argument into a proc, allowing it to be called.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> block_to_proc <span style="color:#006600; font-weight:bold;">&amp;</span>blk
  blk.<span style="color:#9900CC;">call</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
block_to_proc <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;hello&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; &quot;hello&quot;</span></pre></div></div>

<p>In a method call it coverts the argument into a block.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> proc_to_block
  <span style="color:#9966CC; font-weight:bold;">yield</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
proc_to_block<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>proc <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;hello&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; &quot;hello&quot;</span></pre></div></div>

<h2>To Proc</h2>
<p>Ruby has a lot of to_X methods.  They are designed to express their receiver as another type.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;1&quot;</span>.<span style="color:#9900CC;">to_i</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">2</span>
<span style="color:#008000; font-style:italic;"># =&gt; 3</span></pre></div></div>

<p>Well, to_proc exists too.  Expressing non proc objects as procs?  When and why would we ever do that?  An example is our [...].inject(:+) piece.  A quick, simple, shorthand way to express a block without having to write it all out.</p>
<p>Anytime we are trying to express an object as a proc Ruby will check to see if that object responds to to_proc.  If it does it will use the return value to express the object as a proc.  Since &#038;object can covert procs to blocks the &#038; is going to want the object to be a Proc before it does anything.  So &#038;object is going to really be &#038;object.to_proc.</p>
<h2>Symbol&#8217;s To Proc</h2>
<p>Symbol&#8217;s to_proc is what allows us to pass it in place of a block.  It might look a little strange at first, but once you see it used it becomes pretty cool.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Symbol</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> to_proc
    <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>obj, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">|</span> obj.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This returns a proc that takes 2 parameters</p>
<ul>
<li>First is an object that will receive the method.</li>
<li>The second is the arguments that will be passed into the method.</li>
</ul>
<p>And when that proc is called</p>
<ul>
<li>Sends the original symbol to the object with the arguments.</li>
</ul>
<p>So, :methods.to_proc basically builds this method.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># :methods.to_proc</span>
<span style="color:#9966CC; font-weight:bold;">def</span> methods_to_proc_when_called obj, <span style="color:#006600; font-weight:bold;">*</span>args
  obj.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:methods</span>, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Lets play with :symbol.to_proc in some funny  ways.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_little_proc = <span style="color:#ff3333; font-weight:bold;">:methods</span>.<span style="color:#9900CC;">to_proc</span>
<span style="color:#008000; font-style:italic;"># my little proc is ready to call :methods on any object</span>
&nbsp;
my_little_proc.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">String</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; returns an array all of String's methods</span></pre></div></div>

<h2>The Setup</h2>
<p>We&#8217;re basically creating a proc from a symbol that can be used to send that symbol into any object.  In fact, a more fitting name for my_little_proc would be something like call_my_symbol_on.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">String</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> introduce
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hi I'm #{self}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> introduce_to name
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hi #{name}, I'm {self}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
call_my_symbol_on = <span style="color:#ff3333; font-weight:bold;">:introduce</span>.<span style="color:#9900CC;">to_proc</span>
call_my_symbol_on<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;ryan&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; &quot;Hi I'm ryan&quot;</span></pre></div></div>

<p>Since the proc that to_proc returned is setup to take arguments.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">call_my_symbol_on = <span style="color:#ff3333; font-weight:bold;">:introduce_to</span>.<span style="color:#9900CC;">to_proc</span>
call_my_symbol_on<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;ryan&quot;</span>, <span style="color:#996600;">&quot;steve&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; &quot;Hi steve, I'm ryan&quot;</span></pre></div></div>

<p>If you think about it, this is really simple.  call_my_symbol_on is just going to call introduce (because we used :introduce to create it) on the first parameter.  It is going to send the 2nd+ parameters in as arguments.</p>
<h2>With Map</h2>
<p>Ok, lets get to the real world examples.  We often see to_proc commonly used with enumerable.  Lets say we want to introduce a bunch of names.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'ryan'</span>, <span style="color:#996600;">'steve'</span>, <span style="color:#996600;">'jill'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:introduce<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; Hi I'm ryan</span>
<span style="color:#008000; font-style:italic;"># =&gt; Hi I'm steve</span>
<span style="color:#008000; font-style:italic;"># =&gt; Hi I'm jill</span></pre></div></div>

<p>Is really</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'ryan'</span>, <span style="color:#996600;">'steve'</span>, <span style="color:#996600;">'jill'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:introduce.<span style="color:#9900CC;">to_proc</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Which can be expressed as</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'ryan'</span>, <span style="color:#996600;">'steve'</span>, <span style="color:#996600;">'jill'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&amp;</span> <span style="color:#CC0066; font-weight:bold;">proc</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>obj, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">|</span> obj.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:introduce</span>, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>And &#038; is now going to covert that proc into a block.  That line can now become</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'ryan'</span>, <span style="color:#996600;">'steve'</span>, <span style="color:#996600;">'jill'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>obj, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">|</span> obj.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:introduce</span>, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>And since map only passes one argument, the element, to its block there is really no need to express the additional arguments.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'ryan'</span>, <span style="color:#996600;">'steve'</span>, <span style="color:#996600;">'jill'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>obj<span style="color:#006600; font-weight:bold;">|</span> obj.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:introduce</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Which is sending the message introduce our object, which we know is the same as</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'ryan'</span>, <span style="color:#996600;">'steve'</span>, <span style="color:#996600;">'jill'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>obj<span style="color:#006600; font-weight:bold;">|</span> obj.<span style="color:#9900CC;">introduce</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>You can see that by expanding and reducing the &#038;:symbol notation we can end up with a very familiar call map with block. </p>
<h2>Now with some *Args</h2>
<p>So what about *args?  We were able to drop it, because map only expects a block that will yield to one element.  We need to find a common ruby method that yields more than one thing to a block.</p>
<p>How about inject?  It&#8217;s block expects 2 parameters, result and element.</p>
<p>Enumerable.inject(start) { |result, element| &#8230; }</p>
<p>Lets do what we did above.  But this time get from</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># turn</span>
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># into</span>
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>result, element<span style="color:#006600; font-weight:bold;">|</span> 
  result <span style="color:#006600; font-weight:bold;">+</span> element
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Here we go</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:<span style="color:#006600; font-weight:bold;">+</span>.<span style="color:#9900CC;">to_proc</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># can be expressed as</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&amp;</span>proc<span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>obj, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">|</span> obj.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#006600; font-weight:bold;">+</span>, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> 
&nbsp;
<span style="color:#008000; font-style:italic;"># which &amp; will covert into</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">|</span>obj, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">|</span> obj.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#006600; font-weight:bold;">+</span>, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> 
&nbsp;
<span style="color:#008000; font-style:italic;"># which we can convert to</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>obj, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">|</span> 
  obj.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#006600; font-weight:bold;">+</span>, <span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span> 
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># and then we can just rename our parameters to something more friendly</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>result, element<span style="color:#006600; font-weight:bold;">|</span> 
  result.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#006600; font-weight:bold;">+</span>, element<span style="color:#006600; font-weight:bold;">&#41;</span> 
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># =&gt; 55</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/PotStuck/~4/__S4gMQSfLQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2011/08/06/ruby-symbols-instead-of-blocks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2011/08/06/ruby-symbols-instead-of-blocks/</feedburner:origLink></item>
		<item>
		<title>Map If in Ruby and an Introduction to Ruby’s Inject</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/V-HSS9TYDP4/</link>
		<comments>http://www.potstuck.com/2011/07/25/map-if-in-ruby-and-an-introduction-to-rubys-inject/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 22:42:32 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=446</guid>
		<description><![CDATA[When I first got into Ruby I kept hearing the saying: &#8220;There is more than one way to do it.&#8221; At first, this really freaked me out. I thought it meant that learning Ruby was going involving reading lots of code golf samples where developers come up with tricky, cleaver, and mind boggling ways to [...]]]></description>
			<content:encoded><![CDATA[<p>When I first got into Ruby I kept hearing the saying: &#8220;There is more than one way to do it.&#8221;  At first, this really freaked me out.  I thought it meant that learning Ruby was going involving reading lots of code golf samples where developers come up with tricky, cleaver, and mind boggling  ways to accomplish small problems.  That original thought could not have been more false.  Over the past year I&#8217;ve discovered that Ruby&#8217;s &#8220;There is more than one way to do it&#8221; really means that the best way to do something is generally the most readable and understandable way.</p>
<h2>Map if</h2>
<p>Here is a little example of multiple ways to do the same thing, what I like to call the &#8220;Map if&#8221; problem.</p>
<p>Map if involves taking an array (or any enumerable object) and returning a manipulated version of every element for which a certain condition is met. So, lets write a piece of code that takes an array of integers and returns an array of those integers multiplied by two if the integer is an even number.</p>
<p>given [1, 2, 3, 4, 5, 6] return [2*2, 4*2, 6*2] or [4, 8, 12]</p>
<h2>Select/Map</h2>
<p>A very straight forward way to solve this problem is just chaining two array methods together.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>elm<span style="color:#006600; font-weight:bold;">|</span> elm <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">2</span> == <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>elm<span style="color:#006600; font-weight:bold;">|</span> elm <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; [4, 8, 12]</span></pre></div></div>

<p>First select will filter the array but the condition defined in the block.  Then map will apply its block onto each element in the array that select built.</p>
<h2>Inject</h2>
<p>A better way is to solve this problem is to use Enumerable&#8217;s inject, which is somewhat foreign to new ruby programmers.  It&#8217;s syntax is a little off putting at first, it takes 2 parameters: a starting object and a block.  The block then takes another 2 parameters: an element and the result.  Before I explain this method any further here is an the above map/if done with inject.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">4</span>, <span style="color:#006666;">5</span>, <span style="color:#006666;">6</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>result, elm<span style="color:#006600; font-weight:bold;">|</span>
  result <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> elm <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">2</span> <span style="color:#9966CC; font-weight:bold;">if</span> elm <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">2</span> == <span style="color:#006666;">0</span>
  result
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># =&gt; [4, 8, 12]</span></pre></div></div>

<p>So what is this doing?  Inject in english is: </p>
<p>Start with some object (our empty array) and then pass each element in our caller ([1, 2, 3, 4, 5, 6]) to the block.  Provide a result object that the block can freely change.  The result starts out as our first parameter (the empty array) and then becomes whatever the block evaluates last.</p>
<p>So why is inject better?  There are a number of reasons</p>
<p><strong>One method with one block</strong><br />
Our select/map example above was straight forward, but it involves multiple method calls and multiple blocks.  The longer our method chain becomes the less readable our code is.  Inject is readable because the block is just a block, that is to say, if you write readable Ruby in the block then the inject method is going to be very easy to follow.</p>
<p><strong>Part of Enumerable</strong><br />
Any object that extends enumerable is going to have inject.  This makes it very reusable.  I find myself using inject on a daily bases for all sorts of different use cases.</p>
<p><strong>Powerful</strong><br />
The real power of inject lies in it&#8217;s ability to build new objects while looking at every element in the caller individually.  Since inject can draw values from any enumerable object it makes it an ideal method for many common uses such as filtering, grouping, summing and everything else related to building.  The map if example showed how inject can be used to filter and build. </p>
<h2>Grouping</h2>
<p>Grouping a list of words by the first letter of the word:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;alpha&quot;</span>, <span style="color:#996600;">&quot;bravo&quot;</span>, <span style="color:#996600;">&quot;charlie&quot;</span>, <span style="color:#996600;">&quot;bark&quot;</span>, <span style="color:#996600;">&quot;almond&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>result, elm<span style="color:#006600; font-weight:bold;">|</span>
  result<span style="color:#006600; font-weight:bold;">&#91;</span>elm<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_sym</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  result<span style="color:#006600; font-weight:bold;">&#91;</span>elm<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">to_sym</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> elm
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># =&gt; {:a=&gt;[&quot;alpha&quot;, &quot;almond&quot;], :b=&gt;[&quot;bravo&quot;, &quot;bark&quot;], :c=&gt;[&quot;charlie&quot;]}</span></pre></div></div>

<h2>Summing</h2>
<p>Inject is used in many common ruby idioms.  The next code line will look like absolute garbage to anyone not familiar with Ruby.  However, to someone that knows inject it is a very clear and readable code.</p>
<p>Sum up all the numbers between 1 and 10 inclusive.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span>:<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; 55</span></pre></div></div>

<p>Short and simple, but what happened to our parameters and block?  </p>
<p><strong>No Starting Value</strong><br />
When you call inject without a starting value (the first parameter) the starting value will default to the first element inside your enumerable.  So in the above example result would be 1 for the first pass.</p>
<p><strong>No Block</strong><br />
In Ruby 1.9 all symbols have a to_proc method.  Instead of passing a block where a block is required, you can pass a symbol and that symbol will create a proc that then calls itself on the object.  The object in this case is our result.  So :+ will turn into result + *args, where *args is our element.</p>
<img src="http://feeds.feedburner.com/~r/PotStuck/~4/V-HSS9TYDP4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2011/07/25/map-if-in-ruby-and-an-introduction-to-rubys-inject/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2011/07/25/map-if-in-ruby-and-an-introduction-to-rubys-inject/</feedburner:origLink></item>
		<item>
		<title>PHP Dependency – A PHP Dependency Injection Framework</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/GKThkhhNiBs/</link>
		<comments>http://www.potstuck.com/2010/09/09/php-dependency-a-php-dependency-injection-framework/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 00:38:57 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=348</guid>
		<description><![CDATA[PHP-Dependency (Pd) is a dependency injection framework and container written in PHP. One of the main features of PHP-Dependency is that it supports class reflection, which means you do not have to maintain ANY configuration files. Class MyExample&#40;&#41; &#123; &#160; private $_database; &#160; /** * This is the constructor, the database is automatically injected * [...]]]></description>
			<content:encoded><![CDATA[<p>PHP-Dependency (Pd) is a <a href="http://www.potstuck.com/2009/01/08/php-dependency-injection/">dependency injection</a> framework and container written in PHP.  One of the main features of PHP-Dependency is that it supports class reflection, which means you do not have to maintain ANY configuration files.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">Class</span> MyExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_database</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * This is the constructor, the database is automatically injected
     * due to the PdInject DocBlock.
     *
     * @PdInject database
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</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>_database <span style="color: #339933;">=</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s it.  No configuration other than that one line Doc Block.</p>
<h2>Documentation</h2>
<ul>
<li><a href="#phpdependency-download">Download</a></li>
<li><a href="#phpdependency-setup">Setup</a></li>
<li><a href="#phpdependency-thecontainer">The Container</a></li>
<li><a href="#phpdependency-yourclasses">Your Classes</a></li>
<li><a href="#phpdependency-keyvaluecommands">Key:Value Commands and Examples</a></li>
<li><a href="#phpdependency-creatingobjects">Creating Objects</a></li>
<li><a href="#phpdependency-nonreflection">Non Reflection</a></li>
<li><a href="#phpdependency-unittesting">Unit Testing</a></li>
</ul>
<p><a name="phpdependency-download"></a></p>
<h2>Download</h2>
<p><a href="http://www.potstuck.com/wp-content/uploads/2010/09/PHP-Dependency.tar.gz">PHP-Dependency.tar.gz</a><br />
<a href="http://github.com/ryanto/PHP-Dependency" target="_blank">PHP-Dependency on GitHub</a></p>
<p><a name="phpdependency-setup"></a></p>
<h2>Setup</h2>
<p>You need to make sure that the Pd library is on your include path.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>
    <span style="color: #0000ff;">'/path/to/php-dependency/library/'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><b>Autoloader</b><br />
If you are using a PHP framework with an autoloader then just make sure the library/Pd is on the include path and tell the autoloader to use  &#8216;Pd&#8217; as the class prefix/namespace.</p>
<p><b>No Autoloader</b><br />
If you are not using an autoloader then you can just require the class.  All classes follow the naming conventions from Zend Coding Standards.  See Below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Pd/Container.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$container</span> <span style="color: #339933;">=</span> Pd_Container<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a name="phpdependency-thecontainer"></a></p>
<h2>The Container</h2>
<p>The container holds all of your dependencies.  Adding dependencies to the container is very simple.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Database<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mysql://user:password@server/database&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$container</span> <span style="color: #339933;">=</span> Pd_Container<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$container</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dependencies</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'database'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You can put anything into the container.  Objects, arrays, strings, and even anonymous functions.</p>
<p><a name="phpdependency-yourclasses"></a></p>
<h2>Your Classes</h2>
<p>Class dependencies are defined by PHP DocBlocks.  Every Pd command begins with @PdInject and is followed by a key:value type syntax.  The most common command will be injecting a dependency by name, which just requires the dependency name.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">Class</span> Book <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_database</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @PdInject database
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setDatabase<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</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>_database <span style="color: #339933;">=</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><a name="phpdependency-keyvaluecommands"></a></p>
<h2>Key:Value Commands and Examples</h2>
<p>Key:Value commands can be used with the @PdInject DocBlock.</p>
<p><strong>new:ClassName</strong> &#8211; This will create a brand new instance of ClassName and inject it into the object. <a class="spoiler_link_show" href="javascript:void(0)" onclick="wpSpoilerToggle(document.getElementById('id829586594'), this, 'Example', 'Hide')">Example</a>
<div class="spoiler_div" id="id829586594" style="display:none"></p>
<blockquote><p>
<strong>New Example</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">Class</span> MyEmail <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_mailer</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @PdInject new:Zend_Mail
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setMailer<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mailer</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>_mailer <span style="color: #339933;">=</span> <span style="color: #000088;">$mailer</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A new instance of Zend_Mail is created and injected every time a MyEmail object is made.
</p></blockquote>
<p></div>
</p>
<p><strong>method:MethodName</strong> &#8211; This will use MethodName to inject the dependency. <a class="spoiler_link_show" href="javascript:void(0)" onclick="wpSpoilerToggle(document.getElementById('id71130529'), this, 'Example', 'Hide')">Example</a>
<div class="spoiler_div" id="id71130529" style="display:none"></p>
<blockquote><p>
<strong>Method Example</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @PdInject database method:random_method
 */</span>
<span style="color: #000000; font-weight: bold;">Class</span> AClass <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_database</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_book</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> random_method<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</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>_database <span style="color: #339933;">=</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @PdInject book
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setBook<span style="color: #009900;">&#40;</span><span style="color: #000088;">$book</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>_book <span style="color: #339933;">=</span> <span style="color: #000088;">$book</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The injection of the database dependency is handled by random_method.  Notice how the PdInject command is defined in the DocBlock of the class and the book inject is handled by the setBook DocBlock.  When placing PdInject commands over a method, like in book&#8217;s case, no method:X command is needed.
</p></blockquote>
<p></div>
</p>
<p><strong>property:PropertyName</strong> &#8211; This will inject the dependency as PropertyName. <a class="spoiler_link_show" href="javascript:void(0)" onclick="wpSpoilerToggle(document.getElementById('id1343966365'), this, 'Example', 'Hide')">Example</a>
<div class="spoiler_div" id="id1343966365" style="display:none"></p>
<blockquote><p>
<strong>Property Example</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @PdInject database property:database
 */</span>
<span style="color: #000000; font-weight: bold;">Class</span> AnotherClass <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @PdInject book
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$book</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is going to inject the dependency right into the property.  You can place the PdInject commands in the class DocBlock (database) or right above the property (book).  Remember to make sure your properties are publicly available when using this command.  The property command cannot be used with private or protected members.
</p></blockquote>
<p></div>
</p>
<p><strong>constructor:number</strong> &#8211; Used for constructor injection.  <a class="spoiler_link_show" href="javascript:void(0)" onclick="wpSpoilerToggle(document.getElementById('id367074326'), this, 'Example', 'Hide')">Example</a>
<div class="spoiler_div" id="id367074326" style="display:none"></p>
<blockquote><p>
<strong>Constructor Example</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @PdInject database constructor:1
 */</span>
<span style="color: #000000; font-weight: bold;">Class</span> ExampleClass <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_database</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_book</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_anotherBook</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @PdInject book
     * @PdInject anotherBook
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #000088;">$book</span><span style="color: #339933;">,</span> <span style="color: #000088;">$anotherBook</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>_database <span style="color: #339933;">=</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span> 
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_book <span style="color: #339933;">=</span> <span style="color: #000088;">$book</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_anotherBook <span style="color: #339933;">=</span> <span style="color: #000088;">$anotherBook</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When using constructor injection you can define the PdInject command in the __constructor DocBlock or the class DocBlock.  In the above example database is defined in the class DocBlock while book and anotherBook are defined in the __constructor DocBlock.  I would recommend using the __constructor DocBlock for constructor injection.
</p></blockquote>
<p></div>
</p>
<p><strong>force:true</strong> &#8211; Used to force injection.  <a class="spoiler_link_show" href="javascript:void(0)" onclick="wpSpoilerToggle(document.getElementById('id1451973787'), this, 'Example', 'Hide')">Example</a>
<div class="spoiler_div" id="id1451973787" style="display:none"></p>
<blockquote><p>
<strong>Force Example</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @PdInject database property:database force:true
 * @PdInject book property:book force:true
 */</span>
<span style="color: #000000; font-weight: bold;">Class</span> ForceClass <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$book</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> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Forcing injection is used when the methods or properties do not exists in code.  An example would be magic functions like __call and __set.  This is very useful when working with dynamic classes that are contain large amounts polymorphic code.  When the force command is used Pd will not check to verify that the method or property exists, instead it will just attempt to force injection.
</p></blockquote>
<p></div>
</p>
<p><a name="phpdependency-creatingobjects"></a></p>
<h2>Creating Objects</h2>
<p>To create objects use the Pd_Make class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* @var $book Book */</span>
<span style="color: #000088;">$book</span> <span style="color: #339933;">=</span> Pd_Make<span style="color: #339933;">::</span><span style="color: #004000;">name</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is the same as doing</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$book</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Book<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note:  Use the @var doc line above the Pd_Make command.  This will tell your IDE that $book is an instance of the Book class, which will allow the IDE to auto complete any calls/usages of $book in your code.</p>
<p><a name="phpdependency-nonreflection"></a></p>
<h2>Non Reflective Map Building</h2>
<p>If you do not want to use class reflection the Pd library contains a map builder that works off arrays.  The commands are somewhat different from the key:value syntax, but they are self explanatory and easy to follow.  Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$builder</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Pd_Map_Builder_Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'dependencyName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'database'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'injectWith'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'method'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'injectAs'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'setDatabase'</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;">$builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'dependencyName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'apple'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'injectWith'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'constructor'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'injectAs'</span> <span style="color: #339933;">=&gt;</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: #000088;">$builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'injectWith'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'property'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'injectAs'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'theService'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'force'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'newClass'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Service_Class'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Pd_Container<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">maps</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MyClass'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$builder</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">map</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></div></div>

<p>This type of map building should only be used when needed.</p>
<p><a name="phpdependency-unittesting"></a></p>
<h2>Unit Testing</h2>
<p>Pd includes a test suite that will unit test the library.  Running this test is simple:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[ryan@localhost]$ cd /path/to/php-dependency/tests/
[ryan@localhost]$ phpunit AllTests</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">PHPUnit 3.4.11 by Sebastian Bergmann.
&nbsp;
....................................................
&nbsp;
Time: 0 seconds, Memory: 7.00Mb
&nbsp;
OK (52 tests, 59 assertions)</pre></div></div>

<p><strong>Testing within your application</strong><br />
If you would like to test the library within your application, then inside of your test suite you can include the Pd test suite.  To do this your AllTests file should look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'/usr/share/php/php-dependency/tests/AllTests.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> AllTests <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestSuite <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> suite<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$suite</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AllTests<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// your unit test suites here</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// test php-dependency</span>
        <span style="color: #000088;">$suite</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addTestSuite</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PdTests_AllTests'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$suite</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/PotStuck/~4/GKThkhhNiBs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2010/09/09/php-dependency-a-php-dependency-injection-framework/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2010/09/09/php-dependency-a-php-dependency-injection-framework/</feedburner:origLink></item>
		<item>
		<title>Vegas, Wifi, and Internet Poker</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/kW8b4UP-4u8/</link>
		<comments>http://www.potstuck.com/2010/06/14/vegas-wifi-and-internet-poker/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 04:42:14 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Poker]]></category>
		<category><![CDATA[4g]]></category>
		<category><![CDATA[clear]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[vegas]]></category>
		<category><![CDATA[wifi]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=310</guid>
		<description><![CDATA[I am in Vegas for the World Series of Poker, which means 30 days of living out of a suitcase in a hotel room. After using the internet connection in the hotel for less than a few hours there is one thing that really stands out: this is the worst connection ever. Constant disconnections, tons [...]]]></description>
			<content:encoded><![CDATA[<p>I am in Vegas for the World Series of Poker, which means 30 days of living out of a suitcase in a hotel room.  After using the internet connection in the hotel for less than a few hours there is one thing that really stands out: this is the worst connection ever.  Constant disconnections, tons of lag, and I have no idea how secure/unsecure the network is.  These things all make it impossible to play online poker.</p>
<p>I did some research on wifi cards and came across <a href="http://www.clear.com">Clear</a>, which currently has 4g network infrastructure in Las Vegas as well as a month-to-month payment plan.  Jackpot!</p>
<div id="attachment_320" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.potstuck.com/wp-content/uploads/2010/06/clear.jpg"><img src="http://www.potstuck.com/wp-content/uploads/2010/06/clear.jpg" alt="Clear USB Wifi Card for Poker" title="USB Wifi Card" width="300" height="225" class="size-full wp-image-320" /></a><p class="wp-caption-text">Clear's 4g USB Wifi Card</p></div>
<p>They have two options, buy online or buy in one of their Vegas stores, which are about five minutes off the strip.  I choose to do an in-store buy because I wanted the connection right away.  Purchasing the plan/card was really easy.  I was somewhat hesitant because I never heard of this provider before, but I was really impressed with their store and knowledge.  All-in-all I paid about $170 dollars for the modem, activation, and one month of coverage.  I was also in and out of the store in 15 minutes, which is unbelievable when you think of average time spent in cell phone stores.</p>
<p>Since buying the card I have played six sessions of online poker on just about every single site.  My average session is about five hours.  I have had one disconnect, after four hours during the first session.  Since then it has been flawless, including an eight hour session. </p>
<p>I am in one of these mega hotels in Vegas and my connection is usually one or two bars out of 10.  The connection manager rates this as a &#8216;weak signal&#8217;.  This is somewhat worrisome, but like I said, I was able to play a couple of big sessions without a single disconnect.</p>
<p>The speed seems pretty solid, it is not your cable modem, but for browsing webpages its perfectly fine.  </p>
<div id="attachment_316" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.potstuck.com/wp-content/uploads/2010/06/internet-speed.png"><img src="http://www.potstuck.com/wp-content/uploads/2010/06/internet-speed.png" alt="Clear Wifi 4g Speed Test" title="Clear Wifi 4g Speed Test" width="300" height="135" class="size-full wp-image-316" /></a><p class="wp-caption-text">Clear Wifi 4G Speed Test in Las Vegas, NV</p></div>
<p>I would easily recommend this to any online poker player spending the month in Vegas.  Especially when you consider the $14.95/day fee for using the hotel&#8217;s unstable internet, this card becomes a no brainer.</p>
<img src="http://feeds.feedburner.com/~r/PotStuck/~4/kW8b4UP-4u8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2010/06/14/vegas-wifi-and-internet-poker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2010/06/14/vegas-wifi-and-internet-poker/</feedburner:origLink></item>
		<item>
		<title>My Development Environment</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/YSpOKLRH5b4/</link>
		<comments>http://www.potstuck.com/2010/05/05/my-development-environment/#comments</comments>
		<pubDate>Thu, 06 May 2010 04:57:12 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[virtual machine]]></category>
		<category><![CDATA[virtualbox]]></category>
		<category><![CDATA[virtualization]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=210</guid>
		<description><![CDATA[For the last two years I have been developing applications by using a virtualized development server as main my development environment. This process has worked out extremely well since a virtualized environment is always accessible, easy to replicate, easy to access, and is able to mimic production down to every last detail. Always accessible Since [...]]]></description>
			<content:encoded><![CDATA[<p>For the last two years I have been developing applications by using a virtualized development server as main my development environment.  This process has worked out extremely well since a <a href="http://www.potstuck.com/2010/05/05/how-to-setup-centos-in-virtualbox/">virtualized environment</a> is always accessible, easy to replicate, easy to access, and is able to mimic production down to every last detail.</p>
<h3>Always accessible</h3>
<p>Since the development server is running on my computer, it&#8217;s always available.  It does not matter if I am disconnected from the Internet, or the internal network at work, I am always able to access my development server.  This is especially nice when taking long flights, because I able to write and test code with no external network dependencies.</p>
<h3>Easy to Replicate</h3>
<p>Virtual Machines are just files on the hard drive, which means they can easily be copied and shared.  The size of the files are roughly the same size as the hard drive size that was chosen during the setup.  Giving the development server a smaller hard drive is going to make sharing the environment much easier.</p>
<p>I use an 8gb hard drive for my machines.  I find this generally gives me enough space and is easy enough to share.  Sharing a development environment comes in handy when the development server takes a while to configure.  If the development OS requires a lot of third party software or tweaking before it is able to run any code then saving and reusing the virtual machine will save a ton of time.  </p>
<p>I recommend setting up a virtual environment, installing the OS and all of the required services/tweaks.  Before you write any code backup the the virtual machine files.  This will give you a good base system (restore point) if you ever need to setup the same environment on another computer.</p>
<p>The development environment will running on each developers computer, so developer A can break his environment without affecting any of developer B&#8217;s work.  This is really nice when it comes to experimenting with operating system and software upgrades.  </p>
<h3>Easy to Access</h3>
<p>Virtualized environments allow you to easily share files between your computer and the virtual machine.  This means you can edit your code through your favorite desktop IDE and save it right to your development environment without having to move or transfer the files.  This makes accessing your project or code repositories really easy from any virtual machine installed on your computer.</p>
<p>You can also share folders by using Samba, SSHFS, or FTP.  However, those services are generally a lot slower and require some sort of configuration.  </p>
<h3>Mimic Production</h3>
<p>Being able to have your development mimic your production environment is a must.  It eliminates bugs, makes testing more effective, and makes coding a lot easier.  Virtual machines are a great way to have your development environment be an exact replica of your production server.  You can setup your development machine to have the same OS/PHP/MySQL/Apache/etc versions as production.  </p>
<p>I have a Macbook Pro laptop, a Windows desktop, and an Ubuntu desktop that I write code from.  Without virtualization I would never be able to code from all three of these machines.  Some of the projects that I have worked on have deep dependencies (down to the OS version).  Even with a pretty universal language like PHP you would be surprised to learn the number of functions/extensions that only work on certain operating systems.  Using a virtual development environment instantly solves these problems.  </p>
<h3>One project per Machine</h3>
<p>Aside from hard drive space, there is no limit to the number of virtual machines you can have.  Most applications, especially web apps, start out as simple projects with little or no dependencies.  However, over time these projects grow and start to rely on a specific database or language version.  This creates the need to have each project have its own virtual machine.  </p>
<p>I try to develop every project on a virtual machine that closely mimics that project&#8217;s production environment.  This means a lot of different development environments, which really is not a problem.  It&#8217;s a good rule to follow and it&#8217;s a necessary rule when dealing with complex projects.</p>
<h3>Getting Started</h3>
<p>I would recommend Oracle&#8217;s <a href="http://www.virtualbox.org/">VirtualBox</a> for running your development environment.  It&#8217;s free and takes minutes to install, setup, and configure.  I also wrote a guide for <a href="http://www.potstuck.com/2010/05/05/how-to-setup-centos-in-virtualbox/">installing CentOs with VirtualBox</a>.</p>
<img src="http://feeds.feedburner.com/~r/PotStuck/~4/YSpOKLRH5b4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2010/05/05/my-development-environment/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2010/05/05/my-development-environment/</feedburner:origLink></item>
		<item>
		<title>How to Setup CentOs in VirtualBox</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/GiiUC36GtZg/</link>
		<comments>http://www.potstuck.com/2010/05/05/how-to-setup-centos-in-virtualbox/#comments</comments>
		<pubDate>Wed, 05 May 2010 07:11:09 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[virtual machine]]></category>
		<category><![CDATA[virtualbox]]></category>
		<category><![CDATA[virtualization]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=217</guid>
		<description><![CDATA[My primary development environment is generally a CentOS install through VirtualBox. Although the examples here are all CentOS, this guide can be used to install any operating system through VirtualBox. What is a Virtual Machine A virtual machine provides a platform for your machine to execute and support a complete operating system. In other words, [...]]]></description>
			<content:encoded><![CDATA[<p>My primary development environment is generally a CentOS install through <a href="http://www.virtualbox.org/">VirtualBox</a>.  Although the examples here are all CentOS, this guide can be used to install any operating system through VirtualBox.</p>
<h3>What is a Virtual Machine</h3>
<p>A virtual machine provides a platform for your machine to execute and support a complete operating system.  In other words, it is like having an operating system installed on your computer that can be accessed as if it were just another program.  Need to test something in Windows, just open up the Windows virtual machine and close it when done.  With Sun/Oracle&#8217;s VirtalBox platform, installing and maintaining these virtual machines are easier than ever.</p>
<h3>Getting Started</h3>
<p>Setting up a virtual machine is actually really easy.  It may seem intimidating at first, but if you can install Firefox on your computer then you can handle installing a virtual machine.  The first thing to do is to download the operating system as well as the VirtualBox platform.</p>
<p><strong>Download the Os</strong></p>
<p>Download the operating system and save it somewhere on your computer.  Generally, this is going to be an ISO file.  CentOs has three ways to install.  The first is by downloading seven (yes, seven!) different iso files.  Avoid this at all costs, because mounting and unmounted seven different files during an OS install is quite annoying.  The other options are a DVD or a net install.  I prefer having a copy of the DVD, but the net install is approximately 10mb so it is really easy to download. </p>
<p><strong>Download and Install VirtualBox</strong></p>
<p>Head over to the <a href="http://www.virtualbox.org/wiki/Downloads">VirtualBox download area</a> and pick the version that matches your computer.  You are selecting the version that will be installed on your computer.  So if your computer is a Windows computer, select the Windows version. </p>
<h3>Installing the Virtual Machine</h3>
<p>VirtualBox&#8217;s setup wizard makes it really easy to install a Virtual Machine.  Really easy.  So easy, that this guide is pretty much useless.</p>
<p><strong>Naming</strong></p>
<p>Open VirtualBox and click the &#8216;New&#8217; button in the top right.  This will pop up the wizard.  </p>
<p>For the name enter something really simple that describes the box.  I usually enter the box&#8217;s host name such as &#8216;development1&#8242;.  </p>
<p>Choose the best operating system/version that matches your install.  For CentOs choose Operating System: Linux and Version: Red Hat.</p>
<p><strong>Memory</strong></p>
<p>The next step will be memory.  Understanding how the virtual machine uses your systems memory is pretty important.  Whenever the virtual machine is on it will immediately take up all the memory it was given.  For example, if your computer has 2gb of memory and your virtual machine is set to use 1gb of memory then as soon as your virtual machine turns on 50% of your memory is in use.  Assigning too much memory or leaving unnecessary virtual machines turned on is a great way too eat up your computer&#8217;s resources and make your system crawl.</p>
<p>VirtualBox shows a slider with the bounds for safe memory.  Stay within the green and you will be fine. The amount of memory can always be changed later on.</p>
<p><img src="http://www.potstuck.com/wp-content/uploads/2010/04/vmmemory.png" alt="virtualbox-memory" title="VritualBox Memory Selector" width="405" height="81" class="aligncenter size-full wp-image-219" /></p>
<p><strong>Hard Disk</strong></p>
<p>Next we have the hard drive, which I generally keep small, around 8gb for CentOs.  I find that this is more than enough space.  VirtualBox also allows your virtual machines to access files on your computer&#8217;s hard drive, so sharing files that already exist on your computer is very easy. </p>
<p><strong>Setup</strong></p>
<p>Your machine is all setup, but right now it is a bare machine with no operating system installed.  Before booting the machine, go into the settings (right next to the &#8216;New&#8217; button).  These are settings that can be changed whenever your virtual machine is off.  I&#8217;d recommend browsing each of these sections and tweaking with the settings.  It&#8217;s pretty hard to break anything here, so experiment and do whatever you think is best.  Remember that you can always change these things later.</p>
<p><strong>Network</strong></p>
<p>One setting I recommend changing right away is the network.  By default, VirtualBox uses a NAT network type.  I like to use the bridge adapter network, because it will make the virtual machine act more like a computer on the network.  With a bridged network the virtual machine will actually contact the router through your computer&#8217;s network card and obtain a new IP address.  This makes accessing the machine services, like httpd, extremely easy.</p>
<h3>Installing the Os</h3>
<p>If we try to start the virtual machine we will get an error telling us that no bootable medium was found.  This is because there is no OS installed.  To install an OS start (boot) the virtual machine.  </p>
<p>On the bottom right of the virtual machine&#8217;s window you should see a grayed out CD.  Right click this CD and click &#8216;more CD images&#8217;.  </p>
<p><img src="http://www.potstuck.com/wp-content/uploads/2010/04/vm.png" alt="Virtual Machine Options" title="Virtual Machine Options" width="294" height="66" class="aligncenter size-full wp-image-226" /></p>
<p>Click the Add button and find the ISO file for the operating system you downloaded.  Select that and then reboot the machine.  Easiest way to reboot is to use the Menu Bar -> Machine -> Reset.  Also Right Ctrl + R works.</p>
<p>Once your machine reboots you should be able to install the OS from the ISO you mounted.  After your OS has installed you can use your new virtual machine as if it were a new computer.</p>
<img src="http://feeds.feedburner.com/~r/PotStuck/~4/GiiUC36GtZg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2010/05/05/how-to-setup-centos-in-virtualbox/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2010/05/05/how-to-setup-centos-in-virtualbox/</feedburner:origLink></item>
		<item>
		<title>PHP Seo – Sitemap and Robots.txt</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/GxYFfnOfaCA/</link>
		<comments>http://www.potstuck.com/2010/03/19/php-seo-sitemaps-and-robots-txt/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 20:45:54 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[robots.txt]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[sitemap]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=160</guid>
		<description><![CDATA[PHP-Seo is a module that can plug into any application or framework to make SEO life a bit easier. Specifically, it provides an object oriented way of generating sitemap.xml and robots.txt files. Download php-seo.tar.gz Github Documentation Installation Creating a Sitemap Creating Robots.txt Unit Testing Installation After you have downloaded PHP-Seo you will need to add [...]]]></description>
			<content:encoded><![CDATA[<p>PHP-Seo is a module that can plug into any application or framework to make SEO life a bit easier.  Specifically, it provides an object oriented way of generating sitemap.xml and robots.txt files.</p>
<h2>Download</h2>
<p><a href="http://www.potstuck.com/wp-content/uploads/2010/05/php-seo.tar.gz">php-seo.tar.gz</a><br />
<a href="http://github.com/ryanto/PHP-Seo">Github</a></p>
<h2>Documentation</h2>
<ol>
<li><a href="#phpseo-installation">Installation</a></li>
<li><a href="#phpseo-sitemap">Creating a Sitemap</a></li>
<li><a href="#phpseo-robots">Creating Robots.txt</a></li>
<li><a href="#phpseo-unittesting">Unit Testing</a></li>
</ol>
<p><a name="phpseo-installation"></a></p>
<h3>Installation</h3>
<p>After you have downloaded PHP-Seo you will need to add the library to the global include path.  This can be done by:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>
    <span style="color: #0000ff;">'/path/to/php-seo/library/'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a name="phpseo-sitemap"></a></p>
<h3>Creating A Sitemap</h3>
<p>Creating a sitemap is easy.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PSeo/Sitemap/Xml.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sitemap</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PSeo_Sitemap_Xml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sitemap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.potstuck.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sitemap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.potstuck.com/category/programming/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sitemap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Will output:</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;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;urlset</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.sitemaps.org/schemas/sitemap/0.9&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;loc<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.potstuck.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/loc<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;loc<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.potstuck.com/category/programming/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/loc<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/urlset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>When building a sitemap the only required field is a Url.  However, there are other optional fields that you may wish to pass.</p>
<ul>
<li><strong>loc</strong> &#8211; The Url. (required)</li>
<li><strong>lastmod</strong> &#8211; Last modification date.  In ISO 8601 or YYYY-MM-DD.</li>
<li><strong>changefreq</strong> &#8211; How often the document changes: never, monthly, daily, always, etc.</li>
<li><strong>priority</strong> &#8211; Range from 0.0 to 1.0, 1 being the most important</li>
</ul>
<p>For the most part, only passing the Url (by using $sitemap-&gt;addUrl()) is sufficient.  However if you would like to take advantage of the additional fields you can use addUrlData function by passing it an array.  Example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PSeo/Sitemap/Xml.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sitemap</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PSeo_Sitemap_Xml<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sitemap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addUrlData</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'loc'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://www.potstuck.com'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'lastmod'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'2010-03-15'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'changefreq'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'monthly'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'priority'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1.0'</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sitemap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Will output:</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;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;urlset</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.sitemaps.org/schemas/sitemap/0.9&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;loc<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.potstuck.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/loc<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lastmod<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2010-03-15<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lastmod<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;changefreq<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>monthly<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/changefreq<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;priority<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/priority<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/urlset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You can generate a plain text sitemap by using the PSeo_Sitemap_Txt() class.  A plain text sitemap is just a list of Urls.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// old</span>
<span style="color: #666666; font-style: italic;">// require_once 'PSeo/Sitemap/Xml.php';</span>
<span style="color: #666666; font-style: italic;">// $sitemap = new PSeo_Sitemap_Xml();</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// new</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PSeo/Sitemap/Txt.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sitemap</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PSeo_Sitemap_Txt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a name="phpseo-robots"></a></p>
<h3>Creating Robots</h3>
<p>Creating a robots.txt is simple.  By default, the User-Agent will be *, the path / will be allowed, and nothing will be disallowed.  See the following code/output for a clearer picture.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PSeo/Robots/Txt.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$robots</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PSeo_Robots_Txt<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;">$robots</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Will output:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">User-Agent: *
Allow: /</pre></div></div>

<p>You can of course change all of these settings by using the built in functions of the Robots class.</p>
<p><strong>$robots-&gt;setUserAgent(&#8216;User Agent&#8217;)</strong><br />
Use any string as a user agent</p>
<p><strong>$robots-&gt;setSitemap(&#8216;Url To Sitemap&#8217;)</strong><br />
Use the full domain and path to the sitemap, http:// included</p>
<p><strong>$robots-&gt;allowUrl(&#8216;Url&#8217;)</strong><br />
Allow a single url</p>
<p><strong>$robots-&gt;blockUrl(&#8216;Url&#8217;);</strong><br />
Block a single url</p>
<h3>An Example</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PSeo/Robots/Txt.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$robots</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PSeo_Robots_Txt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$robots</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blockUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/private'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$robots</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blockUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/wp-admin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$robots</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSitemap</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.potstuck.com/sitemap.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$robots</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Will output:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">User-Agent: *
Disallow: /private
Disallow: /wp-admin
Allow: /
Sitemap: http://www.potstuck.com/sitemap.xml</pre></div></div>

<h3>Multiple User Agents</h3>
<p>You may need to block and allow certain Urls for different User Agents.  The best way to do this is by creating multiple robot objects for each agent.  Example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'PSeo/Robots/Txt.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$robotsBotA</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PSeo_Robots_Txt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$robotsBotA</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUserAgent</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Bot A'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$robotsBotA</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blockUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/blockAAA'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$robotsBotB</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PSeo_Robots_Txt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$robotsBotB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUserAgent</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Bot B'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$robotsBotB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blockUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/blockBBB'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$robotsBotA</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$robotsBotB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">content</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Will output:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">User-Agent: Bot A
Disallow: /blockAAA
Allow: /
User-Agent: Bot B
Disallow: /blockBBB
Allow: /</pre></div></div>

<p><a name="phpseo-unittesting"></a></p>
<h3>Unit Testing</h3>
<p>PHP-Seo includes a test suite that will unit test the library.  Running this test is simple:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[ryan@localhost]$ cd /path/to/php-seo/tests/
[ryan@localhost]$ phpunit AllTests</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">PHPUnit 3.3.17 by Sebastian Bergmann.
&nbsp;
....................
&nbsp;
Time: 0 seconds
&nbsp;
OK (20 tests, 29 assertions)</pre></div></div>

<p><strong>Testing within your application</strong><br />
If you would like to test the library within your application, then inside of your test suite you can include the PHP-Seo test suite.  To do this your AllTests file should look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'/usr/share/php/php-seo/tests/AllTests.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> AllTests <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestSuite <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> suite<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$suite</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AllTests<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// your unit test suites here</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// test php-seo</span>
        <span style="color: #000088;">$suite</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addTestSuite</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PseoTests_AllTests'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$suite</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Once this is done whenever you test your application the PHP-Seo library will be tested as well. </p>
<img src="http://feeds.feedburner.com/~r/PotStuck/~4/GxYFfnOfaCA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2010/03/19/php-seo-sitemaps-and-robots-txt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2010/03/19/php-seo-sitemaps-and-robots-txt/</feedburner:origLink></item>
		<item>
		<title>Zend Framework View Helpers</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/4ZVKhKVyKMY/</link>
		<comments>http://www.potstuck.com/2009/05/11/zend-framework-view-helpers/#comments</comments>
		<pubDate>Tue, 12 May 2009 04:12:14 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[view helpers]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=113</guid>
		<description><![CDATA[Zend Framework View Helpers allow developers to easily maintain view code, but many existing helpers are coded in very poor form. A few simple guidelines can vastly improve the way developers approach these helpers. What is a View Helper A view helper is simply an additional method added to the view object. The Zend Framework [...]]]></description>
			<content:encoded><![CDATA[<p>Zend Framework View Helpers allow developers to easily maintain view code, but many existing helpers are coded in very poor form.  A few simple guidelines can vastly improve the way developers approach these helpers.</p>
<p><strong>What is a View Helper</strong></p>
<p>A view helper is simply an additional method added to the view object.  The Zend Framework includes are number of very simple and easy to use view helpers.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- Inside the View --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">partial</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sidebar.phtml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This partial view helper does exactly what you would expect it to, it displays the contents of sidebar.phtml onto the page.</p>
<p><strong>Why Use a View Helper</strong></p>
<p>Imagine having to include a bunch of files on a page without using the partial() view helper.  The code would get messy pretty quickly since it would be filled with random file opens.  The view helper makes it easier since it cuts down on copy-and-paste code and centralizes all logic specific to a certain process in one place.  In other words, it keeps code DRY.</p>
<p>All developers have heard of &#8220;Fat Model Skinny Controller&#8221;, but when it comes to views there is no catch phrase rule of thumb.  Many believe that views can be fat (lots of code).  Its perfectly fine for views to contain many lines of code, but high line count in views is often an indicator to add in view helpers.  Think of views in the skinniest way possible by putting every modular part of a page into a custom view helper.</p>
<p>View helpers should exist for any content that could possibly go onto more than one page.  For example, a social networking site would have a page that lists all of your friends.  The site would also want to display a user&#8217;s top ten friends on another page.  This is the perfect use a view helper, which we will call friends().</p>
<p><strong>What does a Zend Framework View Helper Look Like?</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_View_Helper_Friends <span style="color: #000000; font-weight: bold;">extends</span> Zend_View_Helper_Abstract <span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> friends<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;hello friends!&quot;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is a view helper in its simplest form.  Adding this helper to an applicated can be done by calling Zend_View::addHelperPath().</p>
<p>To call this method from a view:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h1&gt;My Friends&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">friends</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!-- this will output 'hello friends!' --&gt;</pre></div></div>

<p><strong>Use Chaining</strong></p>
<p>The biggest problem with View Helpers in the Zend Framework Community is that most developers treat them as methods/functions rather than objects.  Since they are defined in their own class developers really have no excuse for ignoring the added benefits that come with object oriented programming.  Using methods and setters of an class is <em>always</em> going to be more maintainable than passing parameters to a function.</p>
<p>Parameters are messy. The friends helper could take a ton of parameters, such as: The number of friends to display, recent friends, old friends, male friends, female friends, friends in a certain city, it goes on-and-on-and-on.  Does this look fun?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h1&gt;My Female Friends in Boston&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">friends</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'recent'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'female'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$city120</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The function is ugly, it&#8217;s impossible to remember the order of parameters, and what happens if another parameter is needed in the function in a few months from now.  Refactoring this code is going to be painful.</p>
<p>More important, that function is going to be nearly impossible to unit test.  All of the if statements, extra parameters, and default parameters are going to cause nightmares for any coder.  The test cases are going to be filled with so much junk in order to test each little section of the function.</p>
<p>This code on the other hand is much easier to read, extremely easy to refactor, and simple to test:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h1&gt;My Bro Friends&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">friends</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gender</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Male'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Write a render method, Don&#8217;t Call it</strong></p>
<p>Its good practice to write a render() method that returns the HTML/final output to the view, but it sure is a pain to have to keep writing &#8220;-&gt;render()&#8221; at the end of each view helper call.</p>
<p>To avoid this use PHP&#8217;s magic method __toString().</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> render<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;some html&quot;</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: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now every time the helper is called with a short tag it will automatically be displayed.</p>
<p><strong>Things to remember about View Helpers</strong></p>
<ol>
<li>When chaining an object, the setter methods will have to return instances of the object.</li>
<li>The Zend View object will retain all instances of view helpers.  So all default values will have to be reset whenever the helper is called for a 2nd time.  See method _defaultValues() below for an example.</li>
<li>The render() method returns HTML/text/output.</li>
</ol>
<p>Here is what the final friends() helper would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_View_Helper_Friends <span style="color: #000000; font-weight: bold;">extends</span> Zend_View_Helper_Abstract <span style="color: #009900;">&#123;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_show</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_gender</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> friends<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>_defaultValues<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _defaultValues<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;">show</span><span style="color: #009900;">&#40;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">gender</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> show<span style="color: #009900;">&#40;</span><span style="color: #000088;">$show</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;">show</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$show</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</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> gender<span style="color: #009900;">&#40;</span><span style="color: #000088;">$gender</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;">gender</span><span style="color: #339933;">=</span> <span style="color: #000088;">$gender</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</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> render<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;">// .. replace with a method call to gather data based on whats been set</span>
  <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;these are my friends: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_finalOutput<span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>My Experience</strong></p>
<p>I pretty much put just about everything I can inside a view helper.  I have no view that is over 100 lines of code.  In fact, most of my pages are just a couple of lines, there are probably very few that contain more than 20 lines.  There is a huge benefit here that has nothing to do with DRY principles or the object oriented properties I have mentioned in this article.  In fact, it has very little to do with development, but everything to do with team flow:</p>
<p>When a view is skinny and has a majority of its &#8216;code&#8217; moved into helpers the only thing that remains is text. Views with mostly text allow non technical people to easily edit (and even commit) the pages of your application.</p>
<p><strong>More </strong></p>
<p><a href="http://framework.zend.com/manual/en/zend.view.helpers.html">Zend Framework View Reference</a><br />
<a href="http://devzone.zend.com/article/3412-View-Helpers-in-Zend-Framework">Devzone Article on Views</a></p>
<img src="http://feeds.feedburner.com/~r/PotStuck/~4/4ZVKhKVyKMY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2009/05/11/zend-framework-view-helpers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2009/05/11/zend-framework-view-helpers/</feedburner:origLink></item>
		<item>
		<title>PHP Dependency Injection</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/YAsLhaJq7W8/</link>
		<comments>http://www.potstuck.com/2009/01/08/php-dependency-injection/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 10:17:30 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=66</guid>
		<description><![CDATA[Dependency injection is the answer to more maintainable, testable, modular code. Every project has dependencies and the more complex the project is the more dependencies it will most likely have.  The most common dependency in today&#8217;s web application is the database and chances are if it goes down the application will all together stop working.  [...]]]></description>
			<content:encoded><![CDATA[<p>Dependency injection is the answer to more maintainable, testable, modular code.</p>
<p>Every project has dependencies and the more complex the project is the more dependencies it will most likely have.  The most common dependency in today&#8217;s web application is the database and chances are if it goes down the application will all together stop working.  That is because the code is dependent on the database server, which is perfectly fine.  Not using a database server because it could one day crash is a bit ridiculous.  Even though the dependency has its flaws, it still makes life for the code, and thus the developer, a lot easier.</p>
<p>The problem with most dependencies is the way that code handles and interacts with them.  What I really mean is the problem is in the code and not the dependency.  If you are not using dependency injection, chances are your code looks something 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> Book <span style="color: #009900;">&#123;</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>
&nbsp;
		<span style="color: #000088;">$registry</span>  <span style="color: #339933;">=</span> RegistrySingleton<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_database <span style="color: #339933;">=</span> <span style="color: #000088;">$registry</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// or</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$databaseConnection</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_database <span style="color: #339933;">=</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The book object now is given full access to the database once it is constructed.  That is good, the book needs to be able to talk to the database and pull data.  The problem lies in the way the book gained its access.  In order for the book to be able to talk to the database the code must have an outside variable named $database, or worse, it must have a singleton pattern class (registry) object containing a record for a database connection.  If neither of these exist the book fails, making the code far from modular.</p>
<p>This raises the question, how exactly does the book get access to the database?  This is where inversion of control comes in.</p>
<p>In Hollywood a struggling actor does not call up a director and ask for a role in his next film.  No, the opposite happens.  The director calls up the actor and asks him to play the main character in his next movie.  Objects are struggling actors, they do not get to pick the roles they play, the director needs to tell them what to do.  Objects do not get to pick the outside systems they interact with, instead, the outside systems are given to the objects.  Remember this as Inversion of Control.</p>
<p>This is how a developer tells his objects how to interact with outside dependencies:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Book <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_databaseConnection</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> setDatabaseConnection<span style="color: #009900;">&#40;</span><span style="color: #000088;">$databaseConnection</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>_databaseConnection <span style="color: #339933;">=</span> <span style="color: #000088;">$databaseConnection</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$book</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Book<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$databaseConnection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This code allows for the book class to be used in any web application.  The Book is no longer dependent on anything other than the developer supplying a database shortly after object creation.</p>
<p>This is, at its finest, dependency injection.  There are two common ways of injecting dependencies.  The first being constructor injection and the second being setter injection.  Constructor injection involves passing all of the dependencies as arguments when creating a new object.  The code would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$book</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Book<span style="color: #009900;">&#40;</span><span style="color: #000088;">$databaseConnection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$configFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are some issues with constructor injection.  First, the more dependencies a class has the messier the constructor becomes.  Passing in three or four dependencies all in a constructor is extremely hard to read.  Also, the less work a constructor does the better.</p>
<p>This leaves us with our second method of dependency injection, setter injection. A dependency is set by using a public method inside the class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$book</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Book<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$databaseConnection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setConfigFile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$configFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is easy to follow, but it leads writing more and more code for your application.  When a book object is created three lines of code are required.  If we have to inject another dependency, a 4th line of code is now needed.  This gets messy quickly.</p>
<p>The answer to this problem is a container, which is class that is designed to hold, create, and inject all the dependencies needed for an application or class.  Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Container <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000088;">$_database</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> makeBook<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$book</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Book<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$book</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDatabase</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;">$_database</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// more injection...</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$book</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$book</span> <span style="color: #339933;">=</span> Container<span style="color: #339933;">::</span><span style="color: #004000;">makeBook</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All dependencies should be registered into the container object during run time.  This object is now the gateway that all dependencies must pass through before they can interact with any classes.  This is the dependency container.</p>
<p>The reason makeBook is a public static function is for ease of use and global access.   When I started this article off I made a reference to the singleton pattern and global access being a poor choices of code.  They are, for the most part.  It is bad design when they control access, but it is perfectly ok when they control creation.  The makeBook function is only a shortcut for creation.  There is no dependency what-so-ever between the book class and the container class.  The container class exists so we can contain our dependencies in one location and automatically inject those dependencies with one line of code creation.</p>
<p>The container class removes all of the extra work of dependency injection.  </p>
<p>Before injection:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$book</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Book<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And now:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$book</span> <span style="color: #339933;">=</span> Container<span style="color: #339933;">::</span><span style="color: #004000;">makeBook</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Hardly any extra work, but tons of extra benefits.</p>
<p>When test code is run, specifically unit tests, the goal is to see if a method of a class is working correctly.  Since the book class requires database access to read the book data it adds a whole layer of complexity.  The test has to acquire a database connection, pull data, and test it.  All of a sudden the test is no longer testing a single method in the book class, it is now also testing database.  If the database is offline, the test would fail.  This is far from the goal a unit test.</p>
<p>A way of dealing with this is just using a different database dependency for the unit tests.  When the test suite starts up a dummy database is injected into the book.  The dummy database will always have the data the developer expects it to have.  If a live database was used in a unit test the data could potentially change causing tests to unnecessarily fail.  </p>
<p>The code is more modular because it can dropped into any other web application.  Create the book object and inject a database connection with $book-&gt;setDatabase().  It does not matter if the database is in Registry::Database, $database, or $someRandomDatabaseVarible.  As long as there is a database connection the book will work inside any system.</p>
<p>The code is more maintainable because each object is given exactly what it needs.  If separate database connections are required between different instances of the same class then there no extra code needed inside the class what-so-ever.  Give book1 access to database1 and book2 access to database2.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Container<span style="color: #339933;">::</span><span style="color: #000088;">$_database</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ourDatabaseVarForDB1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$book1</span> <span style="color: #339933;">=</span> Container<span style="color: #339933;">::</span><span style="color: #004000;">makeBook</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$book2</span> <span style="color: #339933;">=</span> Container<span style="color: #339933;">::</span><span style="color: #004000;">makeBook</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$book2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Dependency injection really is the answer to more maintainable, testable, modular code.</p>
<hr />
<p>I titled this article PHP Dependency Injection, but there is really nothing in here that is specific to PHP.  The reason I choose to include PHP and write all of the code examples in PHP is because I constantly see PHP projects not using DI.  I believe the best place to teach good coding practices is in popular frameworks by forcing the users of the framework to develop within the guidelines of a certain pattern.  Take MVC for example, nowadays all developers understand it and most of the  underlying principles that accompany it.  This is because the frameworks they use force them to use MVC.</p>
<p>Also, please note that the code samples used in this article are just really simple examples to show the nuts and bolts of dependency injection and inversion of control.  If you wish to implement injection into your project please take a look at an already built <a href="http://www.potstuck.com/2010/09/09/php-dependency-a-php-dependency-injection-framework/">dependency injection framework</a>.</p>
<img src="http://feeds.feedburner.com/~r/PotStuck/~4/YAsLhaJq7W8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2009/01/08/php-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>38</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2009/01/08/php-dependency-injection/</feedburner:origLink></item>
		<item>
		<title>Oh yeah, that would work</title>
		<link>http://feedproxy.google.com/~r/PotStuck/~3/81kuK_mp8iw/</link>
		<comments>http://www.potstuck.com/2008/11/30/oh-yeah-that-would-work/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 02:30:20 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[privacy]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.potstuck.com/?p=71</guid>
		<description><![CDATA[I came across Social History (code) today. In a nutshell, its a javascript plugin that allows you to see what social bookmarking sites each of your users visit. It removes the need of having to display a link to every social site on each of your pages. If a user doesn&#8217;t visit digg, don&#8217;t waste [...]]]></description>
			<content:encoded><![CDATA[<p>I came across <a href="http://www.azarask.in/blog/post/socialhistoryjs/" target="_blank">Social History</a> (<a href="http://code.google.com/p/aza/source/browse/trunk/SocialHistory/SocialHistory.js" target="_blank">code</a>) today.  In a nutshell, its a javascript plugin that allows you to see what social bookmarking sites each of your users visit.  It removes the need of having to display a link to <em>every</em> social site on each of your pages.  If a user doesn&#8217;t visit digg, don&#8217;t waste his screen space with a digg icon.  Its a simple idea, it makes sense, and its genius.</p>
<p>More interesting, is how it works.  It builds a list of links and puts them on the page somewhere, hidden, so the user cannot see them.  Links that you have not visited are in blue, and links you have visited are in purple.  The javascript then loops through all the links, and if a social bookmark site has a purple link, it knows you have visited it in the past.</p>
<p>Its nice to see this code being used in a productive way, but there is a pretty large privacy flaw that this code can easily start to abuse.  Instead of tracking social bookmarking sites, it could be used to track sites you wouldn&#8217;t want anyone tracking, for example online banking sites, facebook pages, porn sites, and whatever else you want to keep private.</p>
<p>The good news is that it can only find the pages you have visited by asking your browser if you have visited a specific page. So in order for it to grab your entire history, it would have to ask your browser about each web page on the Internet, one by one.  The bad news is that someone could pretty easily compile an extensive list of private web pages.</p>
<p>Personally, I say we patch this flaw.  Lets stop displaying visited links in purple, unvisited links in blue.  In other words, kill off a:visited.  This is a pretty bold statement to make, but the security benefit outweighs the usability benefit in this scenario for me personally.  It probably does for you too, when was the last time you went to a website and 1) saw the blue/purple links, and 2) thought &#8216;Oh good, I know which links of this site I&#8217;ve clicked and which ones I haven&#8217;t'?  For me, that would be somewhere around 1999.</p>
<p>So yes, remove the a:visited property.</p>
<img src="http://feeds.feedburner.com/~r/PotStuck/~4/81kuK_mp8iw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.potstuck.com/2008/11/30/oh-yeah-that-would-work/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.potstuck.com/2008/11/30/oh-yeah-that-would-work/</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.potstuck.com @ 2013-06-10 19:31:52 -->
