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

<channel>
	<title>Professional PHP &#187; php-patterns</title>
	<atom:link href="http://www.procata.com/blog/archives/tag/php-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.procata.com/blog</link>
	<description>PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.</description>
	<lastBuildDate>Fri, 10 Dec 2010 17:23:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Let Your Properties be Properties</title>
		<link>http://www.procata.com/blog/archives/2007/05/08/let-your-properties-be-properties/</link>
		<comments>http://www.procata.com/blog/archives/2007/05/08/let-your-properties-be-properties/#comments</comments>
		<pubDate>Tue, 08 May 2007 17:41:51 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Design]]></category>
		<category><![CDATA[php-patterns]]></category>
		<category><![CDATA[properties]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.procata.com/blog/archives/2007/05/08/let-your-properties-be-properties/</guid>
		<description><![CDATA[There is a coding pattern that I see (and have used) in PHP code that defines generic methods on a class for setting and getting properties.
&#160;
function set&#40;$name, $value&#41;;
function get&#40;$name&#41;;
&#160;Google code search for examples
Some times there are some ancillary methods to deal with unsetting, checking for existence, setting via an array, or dealing with references in [...]]]></description>
			<content:encoded><![CDATA[<p>There is a coding pattern that I see (and have used) in PHP code that defines generic methods on a class for setting and getting properties.<br />
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> set<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span>, <span style="color: #0000ff;">$value</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">function</span> get<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$name</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre><a href="http://www.google.com/codesearch?hl=en&#038;lr=&#038;q=lang%3Aphp+function%5Cs%2Bset%5Cw*%5Cs*%5C%28%5Cs*%5C%24name%5Cs*%2C%5Cs*%26%3F%5Cs*%5C%24value%5Cs*%5C%29&#038;btnG=Search">Google code search for examples</a></p>
<p>Some times there are some ancillary methods to deal with unsetting, checking for existence, setting via an array, or dealing with references in PHP 4.  They can really clutter up the definition of a class.  That&#8217;s not good.  All this code is fairly standard, too, but it gets duplicated on every class that does this.  That&#8217;s not good, either.</p>
<p>Oh, I&#8217;ll solve this problem by making a base class, some may say.  Wrong.  This a very feeble reason to spend your one shot at inheritance.  Trust me, I know, I&#8217;ve done it.</p>
<p>I think the idea is to make the class extensible.  But PHP is really ok with just setting new properties on a class.<br />
<pre class="php">&nbsp;
<span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">foo</span> = <span style="color: #ff0000;">'bar'</span>;
&nbsp;</pre><br />
So why not just do this?</p>
<p>Another variation of this pattern is to use setXXX($name, $value) or setYYY($name, $value) methods.  This happens alot with &#8220;options&#8221; or &#8220;vars&#8221; or &#8220;properties.&#8221;  It also happens on request wrapper classes.  To me this looks like there is an object here just begging to get out for each XXX and YYY.<br />
<pre class="php">&nbsp;
<span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">xxx</span>-&gt;<span style="color: #006600;">prop</span> = <span style="color: #ff0000;">'foo'</span>;
<span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">yyy</span>-&gt;<span style="color: #006600;">prop</span> = <span style="color: #ff0000;">'foo'</span>;
<span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">zzz</span>-&gt;<span style="color: #006600;">prop</span> = <span style="color: #ff0000;">'foo'</span>;
&nbsp;</pre><br />
This eliminates a slew of property manipulation methods and leaves the original class free to implement its true purpose.  Methods of the form getXXX($name) and setXXX($name, $value) should be the solution of last resort.  </p>
<p>Since I&#8217;ve started eliminating these in my own code in favor of direct properties, intermediate objects or __set and __get, I feel I&#8217;ve seen nothing but positive results.  Try it.  You may like it, too.  Let your properties be properties.</p>
<p><b>UPDATE:</b><br />
From reading the comments, I think there was some confusion about what I meant in this post.  I am not talking about using naked properties instead of accessor methods.  I&#8217;m not talking about accessor methods at all in this post.  A specific accessor method, such as<br />
<pre class="php"><span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">getFoo</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre><br />
where the name of the property is part of the method name is very different than<br />
<pre class="php"><span style="color: #0000ff;">$obj</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'foo'</span><span style="color: #66cc66;">&#41;</span>;</pre><br />
Where you pass the name of the property as a string parameter.  Its only the latter pattern, where the property name is an actual parameter to the method, that I am talking about in this post and that I think should generally be refactored.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.procata.com/blog/archives/2007/05/08/let-your-properties-be-properties/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Harry Fuecks, PHP Patterns and Dynamically Typed</title>
		<link>http://www.procata.com/blog/archives/2005/11/04/harry-fuecks-php-patterns-and-dynamically-typed/</link>
		<comments>http://www.procata.com/blog/archives/2005/11/04/harry-fuecks-php-patterns-and-dynamically-typed/#comments</comments>
		<pubDate>Fri, 04 Nov 2005 07:54:32 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[php-patterns]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.procata.com/blog/?p=153</guid>
		<description><![CDATA[I was reading this post over at Dynamically Typed and I was thinking to myself, this looks like something that Harry Fuecks would write.  A glance at the top of the page confirmed my suspicions.  Harry has been writing a few new blog posts over there.  That and the resurrection of the [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading <a href="http://www.sitepoint.com/blogs/2005/11/03/web-bugs-for-job-scheduling-hack-or-solution/">this post</a> over at <a href="http://www.sitepoint.com/blog-view.php?blogid=9">Dynamically Typed</a> and I was thinking to myself, this looks like something that Harry Fuecks would write.  A glance at the top of the page confirmed my suspicions.  Harry has been writing a few new blog posts over there.  That and the resurrection of the <a href="http://www.phppatterns.com/">PHP Patterns</a> site has probably been keeping him busy.  All I&#8217;m gonna say about the new site design for PHP Patterns is <a href="http://www.ficml.org/jemimap/style/color/wheel.html">color wheel</a>, <a href="http://www.meyerweb.com/eric/tools/color-blend/">color blender</a>, <a href="http://www.colorschemer.com/online.html">color scheme</a>, <a href="http://pixelfever.com/tools/colormatch/">color match</a>, and <a href="http://www.colorcoordinator.com/colorCoordinator.php">color coordinator</a>.  <img src='http://www.procata.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Welcome Back Harry.</p>
<p>Also, unless I&#8217;m mistaken, it looks like SitePoint is using WordPress for their blogs now.  Or has this always been the case and I&#8217;m just now noticing it?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.procata.com/blog/archives/2005/11/04/harry-fuecks-php-patterns-and-dynamically-typed/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

