<?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/"
	>

<channel>
	<title>Programmer's Notes</title>
	<atom:link href="http://programmersnotes.info/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmersnotes.info</link>
	<description>PHP. Yii. JavaScript. Programming. Software design. Patterns. UML. My experience.</description>
	<pubDate>Wed, 16 Dec 2009 13:04:18 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Low Coupling and High Cohesion - GRASP (Design patterns series)</title>
		<link>http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/</link>
		<comments>http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 06:00:40 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[Design patterns]]></category>

		<category><![CDATA[OOP]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[design pattern]]></category>

		<category><![CDATA[grasp]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[polymorphism]]></category>

		<category><![CDATA[tutorial]]></category>

		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=179</guid>
		<description><![CDATA[Low coupling
When designing some architecture, you face with the problem &#8220;which object should perform X task?&#8221; We discussed this question in my previous post in this series. There we noted, that object should perform the tasks he has enough info for. He should be an &#8220;expert&#8221;. But because one of the main OOP characteristics is [...]


Related posts:<ol><li><a href='http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/' rel='bookmark' title='Permanent Link: Creator and Information Expert principles - GRASP (Design patterns series)'>Creator and Information Expert principles - GRASP (Design patterns series)</a></li><li><a href='http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/' rel='bookmark' title='Permanent Link: What is OOP (Object-oriented programming)?'>What is OOP (Object-oriented programming)?</a></li></ol>]]></description>
			<content:encoded><![CDATA[<h2><a name="low-coupling">Low coupling</a></h2>
<p>When designing some architecture, you face with the problem &#8220;which object should perform X task?&#8221; We discussed this question in my previous post in this series. There we noted, that object should perform the tasks he has enough info for. He should be an &#8220;expert&#8221;. But because one of the main OOP characteristics is interaction between objects, it&#8217;s often hard to answer this question.<br />
<span id="more-179"></span></p>
<h3><a name="lc-example">Introducing example</a></h2>
<p>Consider you should print the table parameters (see <a href="http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/">What is OOP</a> post for this example). There are tables of 2 types: square and circle ones. Square has parameter &#8220;side&#8221;, circe has parameter - &#8220;radius&#8221;. And we need the table:<br />
<div id="attachment_312" class="wp-caption aligncenter" style="width: 289px"><a href="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables.png" alt="Tables list" title="List of tables" width="279" height="143" class="size-full wp-image-312" /></a><p class="wp-caption-text">Tables list</p></div><br />
Sure, we can create a class Lister and method Lisert::out(). But what logic to place there? We can go with something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Lister</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$tables</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+</span></div>
</li>
<li class="li2">
<div class="de2"><span class="st0">| &nbsp;Type &nbsp; &nbsp;| parameter | &nbsp;value &nbsp;|</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>, <span class="re0">$s</span> = <a href="http://www.php.net/sizeof"><span class="kw3">sizeof</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#41;</span>; <span class="re0">$i</span> &lt; <span class="re0">$s</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span> instanceof CircleTable<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| &nbsp;Circle &nbsp;| &nbsp;radius &nbsp; | &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span>-&gt;<span class="me1">radius</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| &nbsp;Square &nbsp;| &nbsp;side &nbsp; &nbsp; | &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span>-&gt;<span class="me1">side</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<h3><a name="lc-problem">Identifying the problem</a></h3>
<p>However, this method is strictly linked to using only CircleTable and SquareTable, it will not work without CircleTable and adding other table types is impossible. This can be imagined like this:<br />
<div id="attachment_313" class="wp-caption aligncenter" style="width: 288px"><a href="http://programmersnotes.info/wp-content/uploads/2009/10/classescoupling.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/classescoupling.png" alt="Classes coupling" title="Classes Coupling" width="278" height="257" class="size-full wp-image-313" /></a><p class="wp-caption-text">Classes coupling</p></div><br />
Notice that since Lister class uses CircleTable and SquareTable, they can&#8217;t be used along. This picture illustrates it - they are coupled together.</p>
<h3><a name="lc-solution">Coming up with a solution</a></h2>
<p>The correct variant of solving the task of table output is the following.</p>
<ol>
<li>Refactor Table classes putting the output logic, that is specific to the table into the class</li>
<li>Use these new methods of table classes in the Lister::out method</li>
</ol>
<p>Enough talk, let&#8217;s walk!</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> CircleTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$radius</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$r</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;<span class="co1">//calling the constructor of the parent class, passing height there.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">radius</span> = <span class="re0">$r</span>;<span class="co1">//setting the radius</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">radius*</span><span class="re0">$this</span>-&gt;<span class="me1">radius*M_PI</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| &nbsp;Circle &nbsp;| &nbsp;radius &nbsp; | &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">radius</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> SquareTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$s</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;<span class="co1">//calling the constructor of the parent class, passing height there.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">side</span> = <span class="re0">$s</span>;<span class="co1">//setting the side</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| &nbsp;Square &nbsp;| &nbsp;side &nbsp; &nbsp; | &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">side</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>And we&#8217;ll use these methods in the Lister class:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Lister</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$tables</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">| &nbsp;Type &nbsp; &nbsp;| parameter | &nbsp;value &nbsp;|</span></div>
</li>
<li class="li2">
<div class="de2"><span class="st0">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>, <span class="re0">$s</span> = <a href="http://www.php.net/sizeof"><span class="kw3">sizeof</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#41;</span>; <span class="re0">$i</span> &lt; <span class="re0">$s</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span>-&gt;<span class="me1">out</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Running the code with the same data will give us exactly the same thing. But only one difference - this code is easy to maintain, change and expand. We can imagine this structure as following:<br />
<div id="attachment_318" class="wp-caption aligncenter" style="width: 422px"><a href="http://programmersnotes.info/wp-content/uploads/2009/10/classeslowcoupling.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/classeslowcoupling.png" alt="Low coupling illustration" title="Low coupling illustration" width="412" height="337" class="size-full wp-image-318" /></a><p class="wp-caption-text">Low coupling illustration</p></div><br />
For example, if we need one more table - triangle - we just add this class and then - created object into the List::tables property and that&#8217;s all. It will be printed out.<br />
Here is the code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> TriangleTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$s</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;<span class="co1">//calling the constructor of the parent class, passing height there.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">side</span> = <span class="re0">$s</span>;<span class="co1">//setting the side</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="nu0">0.433</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| Triangle | &nbsp; side &nbsp; &nbsp;| &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">side</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>And now add this table to the test script:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#8216;Table.php&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#8216;Lister.php&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="re0">$list</span> = <span class="kw2">new</span> Lister<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$list</span>-&gt;<span class="me1">tables</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> CircleTable<span class="br0">&#40;</span><span class="nu0">5</span>, <span class="nu0">90</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> SquareTable<span class="br0">&#40;</span><span class="nu0">2</span>, <span class="nu0">85</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> SquareTable<span class="br0">&#40;</span><span class="nu0">8</span>, <span class="nu0">100</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> TriangleTable<span class="br0">&#40;</span><span class="nu0">7</span>,<span class="nu0">150</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> TriangleTable<span class="br0">&#40;</span><span class="nu0">10</span>,<span class="nu0">120</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/header"><span class="kw3">header</span></a><span class="br0">&#40;</span><span class="st0">&#8216;Content-type: text/plain&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$list</span>-&gt;<span class="me1">out</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">?&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>And we&#8217;ll get the following table with <strong>no modifications in the Lister class</strong>:<br />
<div id="attachment_314" class="wp-caption aligncenter" style="width: 285px"><a href="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables-2.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables-2.png" alt="Modified list of tables" title="Modified list of tables" width="275" height="204" class="size-full wp-image-314" /></a><p class="wp-caption-text">Modified list of tables</p></div><br />
Note, that we can populate tables list from the DB table and no code will be changed. Adding new table type is very simple - you just develop the class similar to CircleTable or SquareTable and then use it. No more changes needed.<br />
This technique when you do your best to write the isolated pieces of code, which can be reused separately is called &#8220;Low Coupling&#8221;, your goal is to create classes in such a way that they do not depend on each other or check these dependencies preventing runtime errors.<br />
Sure, that&#8217;s the goal, it can&#8217;t be achieved in full, but you should aim to do this.</p>
<h2><a name="high-cohesion">High cohesion</a></h2>
<p>When you do your best to implement the Lout Coupling principle, you write your code in a small portions, and each of them does some definite task. And since each class is small and does only one or several cohesive jobs, you get High Cohesion in your code.<br />
These two principles go together. When you fulfil one of them, you automatically follow another one.</p>
<h2><a name="summary">Summary</a></h2>
<p>Putting it all together, we may say that Low Coupling principle requires writing isolated pieces of code, that form complex logic only by interacting with each other, not by themselves. And While following this principle, our little parts automatically become focused on one task, so they become cohesive. This way we fulfil both principles at once.<br />
And good OO-design follow all GRASP principles at the same time just because they characterise nice application design.</p>
<p>What can you add here? Maybe you&#8217;ve got some questions? Feel free to ask here in comments or by email.<br />
Grab the code of examples <a href="http://programmersnotes.info/wp-content/uploads/2009/10/lc-and-hc.zip">here</a>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d179').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d179" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/&amp;title=Low+Coupling+and+High+Cohesion+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/&amp;title=Low+Coupling+and+High+Cohesion+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/&amp;title=Low+Coupling+and+High+Cohesion+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/&amp;title=Low+Coupling+and+High+Cohesion+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/&amp;title=Low+Coupling+and+High+Cohesion+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/&amp;title=Low+Coupling+and+High+Cohesion+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/&amp;t=Low+Coupling+and+High+Cohesion+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d179').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d179').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>Related posts:<ol><li><a href='http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/' rel='bookmark' title='Permanent Link: Creator and Information Expert principles - GRASP (Design patterns series)'>Creator and Information Expert principles - GRASP (Design patterns series)</a></li><li><a href='http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/' rel='bookmark' title='Permanent Link: What is OOP (Object-oriented programming)?'>What is OOP (Object-oriented programming)?</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Grabbing password-protected content with cURL</title>
		<link>http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/</link>
		<comments>http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/#comments</comments>
		<pubDate>Sun, 24 May 2009 07:00:55 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[experiece]]></category>

		<category><![CDATA[tip]]></category>

		<category><![CDATA[trick]]></category>

		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=159</guid>
		<description><![CDATA[Post gives practical information on accessing the password-protected areas with cUrl. Concrete examples are given and code is available for download


Related posts:<ol><li><a href='http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/' rel='bookmark' title='Permanent Link: Return several values from PHP function'>Return several values from PHP function</a></li></ol>]]></description>
			<content:encoded><![CDATA[<h2><a name="intro">Introduction</a></h2>
<p>Websites, that aggregate something become more and more popular because we need all information available in one place and accessed fast and easily. From time to time, you come across with tasks, that require retrieving some data from the password-protected area. For example, I use <a href="http://guru.com/">Guru.com</a> for my job search. They post lots of the projects there, but they don&#8217;t offer convenient listing and filtering. So I developed my own tool, that grabs everything from there and ranks it all in the way I need. So, we&#8217;ll take a look at different types of password-protected areas and see how to deal with any of them</p>
<p><span id="more-159"></span></p>
<h2><a name="cookie">Cookie-based authorization</a></h2>
<p>We&#8217;ll start from the most popular type of authorization <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> First, let&#8217;s take a look, how data goes between the user and the server:<br />
<div id="attachment_257" class="wp-caption aligncenter" style="width: 707px"><a href="http://programmersnotes.info/wp-content/uploads/2009/05/cookie-based-auth.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/05/cookie-based-auth.png" alt="Cookie-based authentication" title="Cookie-based authentication" width="697" height="116" class="size-full wp-image-257" /></a><p class="wp-caption-text">Cookie-based authentication</p></div><br />
From the process it becomes evident, that to pull the data from the pass-protected area, we should basically &#8220;catch&#8221; the cookie and then perform all requests passing it with every request. It seems to be complex, but luckily we have cURL PHP extension, that makes all the dirty job.</p>
<p>So all we have to do is:</p>
<ol>
<li>Send login request</li>
<li>Catch cookie</li>
<li>Send all data requests we need</li>
</ol>
<p>The second part is done automatically, so we can just focus on authentication and data retrieval.
</p>
<h3><a name="implementation">Implementation</a></h3>
<p>We&#8217;ll basically need 3 functions:</p>
<ul>
<li>requestContent - this will make actual HTTP request to the URL we provide with parameters we provide (POST, GET variables, HTTP_REFERRER etc)</li>
<li>authenticate - this will send authentication info to the server using the requestContent function</li>
<li>getPage - this will actually get data we need using the same requestContent function. Actually, you may use requestContent instead, but you&#8217;ll definitely want some post-processing the result, for example, fetch some info. So it&#8217;s better to define a separate function, that does it.</li>
</ul>
<p>
So let&#8217;s go! I&#8217;ve created 3 files: config.php where we&#8217;ll store config data, functions.php where we&#8217;ll define out functions and index.php, that actually carries out the job.<br />
config.php follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Specifies user agent. YOu can put anything you want here, I&#8217;m just showing, </span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* that with cUrl you can fake anything. I just copied my UserAgent string. Make ]</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp;* sure you don&#8217;t have line breaks in it!!!</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&#8216;GR_USER_AGENT&#8217;</span>, <span class="st0">&#8216;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 FirePHP/0.2.4&#8242;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Path to the file where cUrl will store cookies. cUrl behaves like a browser -</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp;* it stores all cookies it gets somewhere and then submits them with each HTTP</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* request.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&#8216;GR_COOKIE_FILE&#8217;</span>, <span class="st0">&#8216;cookies.txt&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp;* URL where you log in to your service. We&#8217;ll be logging in to Wikipedia.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* I took this path from the action parameter of the &lt;form&gt; tag on this page:</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* http://en.wikipedia.org/w/index.php?title=Special:UserLogin&amp;returnto=Special:UserLogout</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* You should just take a look at the source code of the page you&#8217;re going to</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp;* login from, finf the login form and see action parameter there.</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&#8216;GR_LOGIN_URL&#8217;</span>, <span class="st0">&#8216;http://en.wikipedia.org/w/index.php?title=Special:UserLogin&amp;action=submitlogin&amp;type=login&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Your username</span></div>
</li>
<li class="li2">
<div class="de2"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&#8216;GR_USERNAME&#8217;</span>, <span class="st0">&#8216;KJedi&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;* Your password</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp;*/</span></div>
</li>
<li class="li2">
<div class="de2"><a href="http://www.php.net/define"><span class="kw3">define</span></a><span class="br0">&#40;</span><span class="st0">&#8216;GR_PASSWORD&#8217;</span>, <span class="st0">&#8216;did you think I<span class="es0">\&#8217;</span>ll give you my pass? :)&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p>In functions.php I define our 3 functions. requestContent looks as following:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> requestContent<span class="br0">&#40;</span><span class="re0">$url</span>, <span class="re0">$postData</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>, <span class="re0">$referer</span> = <span class="st0">&#8221;</span>, <span class="re0">$headers</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$ch</span> = curl_init<span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="co1">//creating cUrl instance</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_URL, <span class="re0">$url</span><span class="br0">&#41;</span>;<span class="co1">//setting our URL</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_HEADER, <span class="nu0">0</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>GR_USER_AGENT<span class="br0">&#41;</span> curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_USERAGENT, GR_USER_AGENT<span class="br0">&#41;</span>;<span class="co1">//setting user agent</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$postData</span><span class="br0">&#41;</span><span class="co1">//if we have post data</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_POST, <span class="nu0">1</span><span class="br0">&#41;</span>;<span class="co1">//tell cUrl we&#8217;ll be using POST</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_POSTFIELDS, <span class="re0">$postData</span><span class="br0">&#41;</span>;<span class="co1">//set post data</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_RETURNTRANSFER, <span class="nu0">1</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_FOLLOWLOCATION, <span class="nu0">1</span><span class="br0">&#41;</span>;<span class="co1">//force cUrl to follow any redirects</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$referer</span><span class="br0">&#41;</span> curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_REFERER, <span class="re0">$referer</span><span class="br0">&#41;</span>;<span class="co1">//if there is referrer specified, set it</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>GR_COOKIE_FILE<span class="br0">&#41;</span><span class="co1">//if there is cookie file</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//set next 2 options needed in order to uuse cookies correctly everywhere</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_COOKIEFILE, GR_COOKIE_FILE<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_COOKIEJAR, GR_COOKIE_FILE<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$headers</span><span class="br0">&#41;</span><span class="co1">//if there are additional headers</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_HTTPHEADER, <span class="re0">$headers</span><span class="br0">&#41;</span>;<span class="co1">//set them</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$result</span> = curl_exec<span class="br0">&#40;</span><span class="re0">$ch</span><span class="br0">&#41;</span>;<span class="co1">//execute cUrl query</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; curl_close<span class="br0">&#40;</span><span class="re0">$ch</span><span class="br0">&#41;</span>;<span class="co1">//close cUrl</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$result</span>;<span class="co1">//return cUrl result</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Basically, here we init cURL, and get the handler. It is similar to the fopen(). Then we set all options we need and finally execute the HTTP query and return result.<br />
Authentication is based on the previous function, that carries our all the low-level job. We only have to examine our login form and determine, what variables to send. So, open the login page of the service you want to access, find the &lt;form&gt; tag and copy the login URL. You&#8217;ll have to put it into the config.php file. Then see what fields are there. Note, that there my be hidden fields also, note their variables and create an associative array for them (see the following code). If you&#8217;re not sure what variables are actually sent, you may use <a href="https://addons.mozilla.org/en-US/firefox/addon/3829">Live Http Headers plugin</a> for the Firefox. <br />
Usage is simple. Open the login page. Fill it in. Then start the plugin (Tools -> Live HTTP headers). Submit the form and see the number of queries are shown. You actually need only the first one - this is your request. See the screenshot:<br />
<div id="attachment_276" class="wp-caption aligncenter" style="width: 599px"><a href="http://programmersnotes.info/wp-content/uploads/2009/05/http-headers.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/05/http-headers.png" alt="Http headers" title="Http headers" width="589" height="500" class="size-full wp-image-276" /></a><p class="wp-caption-text">Http headers</p></div><br />
And now the code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> authenticate<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$post_info</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#8216;wpName&#8217;</span> =&gt; GR_USERNAME,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#8216;wpPassword&#8217;</span> =&gt; GR_PASSWORD,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#8216;wpLoginAttempt&#8217;</span> &nbsp;=&gt; <span class="st0">&#8216;Log+in&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$postData</span> = <span class="st0">&#8221;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">foreach</span><span class="br0">&#40;</span><span class="re0">$post_info</span> <span class="kw1">as</span> <span class="re0">$name</span> =&gt; <span class="re0">$value</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$postData</span> .= <span class="re0">$name</span>.<span class="st0">&#8216;=&#8217;</span>.<span class="re0">$value</span>.<span class="st0">&#8216;&amp;&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$postData</span> = <a href="http://www.php.net/trim"><span class="kw3">trim</span></a><span class="br0">&#40;</span><a href="http://www.php.net/substr"><span class="kw3">substr</span></a><span class="br0">&#40;</span><span class="re0">$postData</span>, <span class="nu0">0</span>, <span class="nu0">-1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$result</span> = requestContent<span class="br0">&#40;</span>GR_LOGIN_URL, <span class="re0">$postData</span>, GR_LOGIN_URL<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//here you may check if you were logged in successfully</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>After getting result, you may parse it and see if you got a successful login. After you performed this query and got response, you&#8217;ve also got the session ID stored in cookie. You may open the cookie file and take a look yourself. After this you may perform any requests to the servic and it will respond as if you are logged user. The trick is that cUrl will send him cookies from the cookie file. And we have the session ID there or anything else that site uses for user identification.<br />
So my getPage() function is a simple wrapper over the requestContent():</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> getPage<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> requestContent<span class="br0">&#40;</span><span class="st0">&#8216;http://en.wikipedia.org/&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Basically, that&#8217;s all you need in order to get into the protected area. Now you&#8217;re free to do anything you want. Using regular expressions for parsing is the most common way to get the data from the page, so maybe you&#8217;ll need the tool for testing your regexps. I really recommend <a href="http://weitz.de/regex-coach/">&#8220;The Regext Coach&#8221;</a>, it greatly simplifies debugging <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
</p>
<h2><a name="http">HTTP authentication</a></h2>
<p>Another approach to the authentication, that is sometimes used in different control panels is HTTP authentication. You should have seen that - you&#8217;re presented with a standard screen, prompting for username and password. This username and password are sent with each request. You are not prompted fro them every time just because browser remembers it. To access such area, you don&#8217;t need any authenticate() function. You just pass login and pass with each request.<br />
Here are modifications you should do in the above code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> requestContentHttpAuth<span class="br0">&#40;</span><span class="re0">$url</span>, <span class="re0">$postData</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>, <span class="re0">$referer</span> = <span class="st0">&#8221;</span>, <span class="re0">$headers</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$ch</span> = curl_init<span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="co1">//creating cUrl instance</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_URL, <span class="re0">$url</span><span class="br0">&#41;</span>;<span class="co1">//setting our URL</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_HEADER, <span class="nu0">0</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_USERPWD, GR_USERNAME.<span class="st0">&#8216;:&#8217;</span>.GR_PASSWORD<span class="br0">&#41;</span>;<span class="co1">//&lt;&#8212;&#8211; here is the change - you pass yout auth data each time</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>GR_USER_AGENT<span class="br0">&#41;</span> curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_USERAGENT, GR_USER_AGENT<span class="br0">&#41;</span>;<span class="co1">//setting user agent</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$postData</span><span class="br0">&#41;</span><span class="co1">//if we have post data</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_POST, <span class="nu0">1</span><span class="br0">&#41;</span>;<span class="co1">//tell cUrl we&#8217;ll be using POST</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_POSTFIELDS, <span class="re0">$postData</span><span class="br0">&#41;</span>;<span class="co1">//set post data</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_RETURNTRANSFER, <span class="nu0">1</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_FOLLOWLOCATION, <span class="nu0">1</span><span class="br0">&#41;</span>;<span class="co1">//force cUrl to follow any redirects</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$referer</span><span class="br0">&#41;</span> curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_REFERER, <span class="re0">$referer</span><span class="br0">&#41;</span>;<span class="co1">//if there is referrer specified, set it</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>GR_COOKIE_FILE<span class="br0">&#41;</span><span class="co1">//if there is cookie file</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//set next 2 options needed in order to uuse cookies correctly everywhere</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_COOKIEFILE, GR_COOKIE_FILE<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_COOKIEJAR, GR_COOKIE_FILE<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$headers</span><span class="br0">&#41;</span><span class="co1">//if there are additional headers</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curl_setopt<span class="br0">&#40;</span><span class="re0">$ch</span>, CURLOPT_HTTPHEADER, <span class="re0">$headers</span><span class="br0">&#41;</span>;<span class="co1">//set them</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$result</span> = curl_exec<span class="br0">&#40;</span><span class="re0">$ch</span><span class="br0">&#41;</span>;<span class="co1">//execute cUrl query</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; curl_close<span class="br0">&#40;</span><span class="re0">$ch</span><span class="br0">&#41;</span>;<span class="co1">//close cUrl</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$result</span>;<span class="co1">//return cUrl result</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>That&#8217;s all I wanted to share with you today. Hope you fins it helpful! If you have any questions/comments, feel free to add them!
</p>
<h4>Quick Note on OOP</h4>
<p>I didn&#8217;t create the Curl class or present solution as a class because I don&#8217;t think it makes my explanations more clear or the code easier to use. This is not full solution, this is just a technique and I tried to present the code in such a way, that you could use them with minimal changes. You can easily put that functions to the class <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>Further reading</h4>
<ul>
<li><a href="http://en.wikipedia.org/wiki/CURL">cUrl on Wikipedia</a></li>
<li><a href="http://curl.haxx.se/">cUrl home page</a></li>
<li><a href="http://ua2.php.net/curl">PHP documentation on cUrl</a></li>
</ul>
<p><a href='http://programmersnotes.info/wp-content/uploads/2009/05/grabbing-info-from-pass-protected-area.zip'>Download code for the post</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d159').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d159" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/&amp;title=Grabbing+password-protected+content+with+cURL" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/&amp;title=Grabbing+password-protected+content+with+cURL" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/&amp;title=Grabbing+password-protected+content+with+cURL" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/&amp;title=Grabbing+password-protected+content+with+cURL" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/&amp;title=Grabbing+password-protected+content+with+cURL" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/&amp;title=Grabbing+password-protected+content+with+cURL" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/&amp;t=Grabbing+password-protected+content+with+cURL" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d159').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d159').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>Related posts:<ol><li><a href='http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/' rel='bookmark' title='Permanent Link: Return several values from PHP function'>Return several values from PHP function</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/feed/</wfw:commentRss>
		</item>
		<item>
		<title>NetBeans 6.5 for PHP - My Experience</title>
		<link>http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/</link>
		<comments>http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/#comments</comments>
		<pubDate>Fri, 22 May 2009 07:00:48 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[choice]]></category>

		<category><![CDATA[experiece]]></category>

		<category><![CDATA[feedback]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=260</guid>
		<description><![CDATA[Quick overview of the NetBeans 6.5 features that are useful for the web-development with PHP


No related posts.]]></description>
			<content:encoded><![CDATA[<h2><a name="intro">Introduction</a></h2>
<p>Personal productivity depends on the tools greatly, so I pay much attention to the programs I use. My previous post in this field was about <a href="http://programmersnotes.info/2009/03/01/mysql-workbench-the-database-modeling-tool-for-mysql/">MySQL workbench</a> which I consider the best free DB design tool for MySQL.</p>
<p>When I started coding PHP, I used Linux so I used Kate, which only had syntax highlighting and allowed to save multiple files opened as a project <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Next was Quanta, the Linux IDE for PHP. I spent lots of time developing there until I installed trial version of Zend Development Environment (ZDE) 5.5. That was really cool. I liked it very much and used for quite long time. Around half a year ago I heard about NetBeans and decided to try it out. I was thinking quite long about it, I didn&#8217;t have enough time to install and go through all it&#8217;s functions. Finally I saw that I will never have this time, so I started using it around 1.5 months ago. While using, I was putting down some notes about the things I liked and the ones I didn&#8217;t. So now I just want to present my list <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-260"></span></p>
<h2><a name="advantages">Advantages</a></h2>
<p>What I really liked about NetBeans is that it is &#8220;the only IDE you need&#8221;. It has excellent Java support, good HTML, JS and PHP support and it even has C++ support, but I didn&#8217;t try it out, I use the old good Visual Studio 6 <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <br />
So here are the things I liked:</p>
<ul>
<li>HTML support is excellent. It founds tag mismatch, highlights start and end tags (this is extremely helpful when you deal with large portions of bad-formatted code</li>
<li>When you&#8217;re in HTML scope, and typing &#8220;&lt;p style=&#8221;, it adds double quotes automatically! Little, but very nice feature <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>Code formatting is great in everywhere. When working in Zend Studio, I was using 3rd party tools to format HTML, NetBeans does it perfectly. As for other languages, the only problem I have with it is that NetBeans puts opening curly bracket on the same line as loop or function definition, I like putting it on the next line so I see opening and closing brackets one under another. However, it highlights the opposite bracket, so it&#8217;s not a big problem <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>Greatest thing I got there is normal debugging in PHP, not just &#8220;echo&#8221;, &#8220;print_r()&#8221; and &#8220;exit&#8221;. NetBeans integrates with XDebug and it is VERY convenient. It provides normal info as any other debugger - stack trace, local variables, superglobals, watches etc. It saves great amount of time. The only drawback is that NetBeans starts debugging session slowly, but it is definitely faster, than &#8220;echo&#8221; and &#8220;exit&#8221;</li>
<li>When switching from Zend Studio, I experienced some inconveniences because NetBeans uses other shortcuts. However, it provides more useful shortcuts than ZDE, and I got used to it quickly</li>
<li>When you code in languages like JS or PHP, where using variables without declaration is not compilation error, it is a common mistake to misspell variable and then get some interesting bugs. That&#8217;s where variable highlighting helps greatly. In NetBeans you can put cursor into any variable and in a second you&#8217;ll see all occurrences of this variable in the file. That is also very helpful when you are digging through someone&#8217;s code</li>
<li>Search is also much better, than in Zend. When you search in files, search results are grouped by file. When you search in the file, you get all occurrences highlighted. This is also very useful when you&#8217;re looking into someone&#8217;s code or performing some refactoring</li>
<li>Commenting support is also great. For example, you write some //comment. If the line is long, you want to break it into several lines and press enter when you&#8217;re inside the line. NetBeans inserts line break and next line continues as comment! This is so helpful when you&#8217;re commenting some file and your comments are quite long.</li>
<li>PHPDoc support is also great. The fact, that you get help for function, its arguments and return value if you specify function description in the PHPDoc format wasn&#8217;t new to me, but if you start writing PHPDoc comment for the ready function and put &#8220;/**<enter>&#8220;, you immediately get full PHPDoc template - all arguments list with <type> placeholders, @return keyword, so everything remained is only put actual description, everything is automated. That&#8217;s great! The only thing I&#8217;d like to add here is that when function definition is changed, it should update PHPDoc comment - remove correspondent line or add more variables <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> I guess, that&#8217;s a dream</li>
<li>Very useful feature is auto-reloading files. You don&#8217;t need to reopen log every time it is updated. However, there is a slight disadvantage - if file is deleted in the file system, it doesn&#8217;t propose to save a copy like Zend does.</li>
<li>It has built-in diff tool. It is MEGA-useful <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> I have to compare files quite frequently, I was using WinMerge before that, but it was inconvenient. NetBeans integrates everything in one environment and it is very nice!</li>
<li>Auto-complete is better, than in ZDE, it takes scope into consideration. However, it is sometimes buggy.</li>
<li>In NetBeans you can view your DB in the same IDE, perform queries, view tables structure and much more. This tool is not as good as phpMyAdmin, so I am using the latter most of the time. Main reason is that NetBeans is slow when switching from code mode to the DB mode. But maybe that&#8217;s my PC <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<h2><a name="disadvantages">Disadvantages</a></h2>
<p>And now some things I didn&#8217;t like:</p>
<ul>
<li>Code templates are worse, than in Zend Studio. This thing is really simple there and behaved as supposed. In NetBeans I add some template, e.g. &#8220;echo &#8216;&lt;pre&gt;&#8217;.print_r($var,true).&#8217;&lt;/pre&gt;&#8217;;&#8221; for the &#8220;pri&#8221; keyword. And sometimes it doesn&#8217;t appear, sometimes it is inserted with additional <tab></li>
<li>There is no PHP or JS help integrated into IDE. In Zend Studio I could select php function and click &#8220;F1&#8243; and get full help. In Aptana IDE there is the same for JS, but there is noting like this here! Why? Is it something complex?</li>
<li>No FTP support without additional plug-ins. I didn&#8217;t have time to explore this feature, but I don&#8217;t like the fact I have to install something more for such common feature <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>There is UML package for Java, why there is nothing like this for JS?</li>
</ul>
<h2><a name="conclusion">Conclusion</a></h2>
<p>Anyway, despite of the disadvantages, I think, that NetBeans is better, because it offers even more features, than Zend, but for free! And it&#8217;s really good with Java <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> I especially liked refactoring there. Just put the name of the class and everything is changed correspondently!</p>
<p>What are your thoughts about NetBeans? Are you using/planning to use it? What are your workarounds for the disadvantages I noted? Can you add more &#8220;pros&#8221;?</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d260').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d260" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/&amp;title=NetBeans+6.5+for+PHP+-+My+Experience" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/&amp;title=NetBeans+6.5+for+PHP+-+My+Experience" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/&amp;title=NetBeans+6.5+for+PHP+-+My+Experience" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/&amp;title=NetBeans+6.5+for+PHP+-+My+Experience" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/&amp;title=NetBeans+6.5+for+PHP+-+My+Experience" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/&amp;title=NetBeans+6.5+for+PHP+-+My+Experience" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/&amp;t=NetBeans+6.5+for+PHP+-+My+Experience" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d260').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d260').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/05/22/netbeans-65-for-php-my-experience/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Building high-loaded portal - InnoDB vs MyISAM</title>
		<link>http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/</link>
		<comments>http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/#comments</comments>
		<pubDate>Wed, 20 May 2009 07:00:55 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[DB Design]]></category>

		<category><![CDATA[Databases]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[DB]]></category>

		<category><![CDATA[design pattern]]></category>

		<category><![CDATA[efficiency]]></category>

		<category><![CDATA[production]]></category>

		<guid isPermaLink="false">http://konstantin.takeforce.net/?p=5</guid>
		<description><![CDATA[Post shares practical experience of developing MySQL DB for the high-loaded website.


Related posts:<ol><li><a href='http://programmersnotes.info/2009/03/01/mysql-workbench-the-database-modeling-tool-for-mysql/' rel='bookmark' title='Permanent Link: MySQL Workbench - The Database Modeling Tool for MySQL'>MySQL Workbench - The Database Modeling Tool for MySQL</a></li></ol>]]></description>
			<content:encoded><![CDATA[<h2><a name="intro">Preface</a></h2>
<p>In November 2008 I&#8217;ve got a nice project, that resulted in a complex search engine, that specializes in clothing. I can&#8217;t give the link now, but will do when site officially launches. Key features of such service are:</p>
<ul>
<li>Large number of users, which search with various parameters, rate items, leave comments, create articles etc.</li>
<li>Large database, that is updated frequently</li>
</ul>
<p>Sure, the site is running on a dedicated server, sure I use caching, but optimal DB is a key to success.</p>
<p><span id="more-5"></span></p>
<h2><a name="myisam-innodb">MyISAM or InnoDB?</a></h2>
<p>Since we need reliability, I created different tests, search the internet etc. My main question was - what to use MyISAM or InnoDB? I found the <a href="http://www.mysqlperformanceblog.com/2007/01/08/innodb-vs-myisam-vs-falcon-benchmarks-part-1/">post on the MySQL Perfomance Blog</a> (also <a href="http://www.mysqlperformanceblog.com/2007/10/12/myisam-scalability-and-innodb-falcon-benchmarks/">this</a> and <a href="http://www.mysqlperformanceblog.com/2007/01/03/innodb-benchmarks/">this</a>) and came to the conclusion, that when SELECT&#8217;ing, InnoDB is definitely faster (or, at least, is not slower). In addition, it provides constraints mechanism and allows transactions. The only thing it doesn&#8217;t have is FULLTEXT search. One more disadvantage is slower inserts. However, 80% of DB usage in this project are SELECT queries without FULLTEXT. For the fulltext search I created MyISAM table with fields I need and it works fine. The project is finished, but there will be phase 2 and I&#8217;m thinking of introducing <a href="http://www.sphinxsearch.com/">Sphinx</a> there. Anybody has experience with it?<br />
So I came up with InnoDB storage engine.</p>
<h2><a name="structure">Optimizing structure</a></h2>
<p>Next important step is DB structure. I&#8217;ll give some background info about the project so you could better understand what I&#8217;m talking about. The site collects info from different UK shops. Then users can rate them, add some tags, create articles (called &#8220;looks&#8221;) and link that articles to the products they&#8217;re writing about. And the same (tags, comments, rating) applies to the looks. Here we come across with an interesting question. We can create structure of different types for this:</p>
<ul>
<li>Create 2 separate cross-tables for linking Rating to Look or Product</li>
<li>Create one cross-table and add additional field &#8220;type&#8221; there to differentiate between Product and Look entities</li>
</ul>
<p>See both variants in the following images:<br />
<div id="attachment_251" class="wp-caption aligncenter" style="width: 310px"><a href="http://programmersnotes.info/wp-content/uploads/2009/05/norm.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/05/norm-300x174.png" alt="Normalized variant" title="Normalized variant" width="300" height="174" class="size-medium wp-image-251" /></a><p class="wp-caption-text">Normalized variant</p></div><br />
<div id="attachment_252" class="wp-caption aligncenter" style="width: 310px"><a href="http://programmersnotes.info/wp-content/uploads/2009/05/denorm.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/05/denorm-300x157.png" alt="Denormalized version" title="Denormalized version" width="300" height="157" class="size-medium wp-image-252" /></a><p class="wp-caption-text">Denormalized version</p></div><br />
Initially I thought that the second variant would perform better, but after creating and running tests, I saw, that:</p>
<ul>
<li>If there are 0-2 ratings linked to entity, then normalized variant is better</li>
<li>If there are 3-5 ratings linked to entity, then denormalized variant is slightly (less than 1%) better</li>
<li>If there are 6-8 ratings linked to the entity, the normalized variant wins again</li>
</ul>
<p>I did these tests for different situations similar to this one, in each test I generated random records and randomly linked them with each other. Number of record in Product, Look tables was 10000, in Rating - 20000. Number of records in cross tables varies because the linkings are random.<br />
So I came up with normalizing everything like it should be done according to the DB theory.</p>
<p>The only exclusion of this rule is the following situation. In this project you can add different alternatives to the product. Each alternative is &#8220;better&#8221; or &#8220;cheaper&#8221; or &#8220;different&#8221; etc. And since the only case when these alternatives are pulled from DB is when user views the product, they are pulled altogether, so it is better to keep them in the same table.</p>
<h2><a name="conclusion">Conclusion</a></h2>
<ul>
<li>InnoDB turns out to behave better under the high load, it supports transactions and maintains constraints. All that helps to develop better and never leave orphan records.</li>
<li>Using normalization techniques you usually get better performance</li>
<li>Create tests and test everything if you are going to do something great <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<h4>Further reading</h4>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Database_normalization">Database Normalization</a></li>
<li><a href="http://www.mysqlperformanceblog.com/2007/01/08/innodb-vs-myisam-vs-falcon-benchmarks-part-1/">MySQL Performance Blog</a></li>
</ul>
<p>P.S. I&#8217;m looking forward to see your comments on implementation techniques in the high-loaded websites, your thoughts about the InnoDB vs MyISAM question and anything else you can think of <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d5').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d5" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/&amp;title=Building+high-loaded+portal+-+InnoDB+vs+MyISAM" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/&amp;title=Building+high-loaded+portal+-+InnoDB+vs+MyISAM" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/&amp;title=Building+high-loaded+portal+-+InnoDB+vs+MyISAM" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/&amp;title=Building+high-loaded+portal+-+InnoDB+vs+MyISAM" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/&amp;title=Building+high-loaded+portal+-+InnoDB+vs+MyISAM" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/&amp;title=Building+high-loaded+portal+-+InnoDB+vs+MyISAM" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/&amp;t=Building+high-loaded+portal+-+InnoDB+vs+MyISAM" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d5').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d5').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>Related posts:<ol><li><a href='http://programmersnotes.info/2009/03/01/mysql-workbench-the-database-modeling-tool-for-mysql/' rel='bookmark' title='Permanent Link: MySQL Workbench - The Database Modeling Tool for MySQL'>MySQL Workbench - The Database Modeling Tool for MySQL</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/05/20/building-high-loaded-portal-innodb-vs-myisam/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Eurovision 2009 - My Impressions</title>
		<link>http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/</link>
		<comments>http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/#comments</comments>
		<pubDate>Sun, 17 May 2009 10:18:55 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[Life]]></category>

		<category><![CDATA[eurovision]]></category>

		<category><![CDATA[show]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=195</guid>
		<description><![CDATA[Eurovision 2009 impressions


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not a music fan, I don&#8217;t usually watch this, but yesterday my girlfriend had birthday and she likes such things, so we were watching Eurovision together from beginning to the end. And I don&#8217;t regret it! <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
As for me, the show Russians prepared, was not excellent, it was mega-excellent(I&#8217;d even say supercalifragilisticexpialidocious;)) - the show, the scene, all effects were so cool! I can&#8217;t compare with previous ones, but my girl says Eurovision never had such beautiful show. Russians showed they can do things good if they want to, which I&#8217;m very proud of <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> I really liked the acrobatics and the girls in the shallow water above. Dima Bilan&#8217;s initial song and his show was somewhat philosophical, at least it seemed to me so.<br />
<span id="more-195"></span><br />
So, the songs. What I really liked about all the songs (except Ukraine) is that every song reflected the style of the country in some way. It&#8217;s really nice to see singers from different countries, each country has it&#8217;s own ethnic style, that is reflected in the show and the song. The only country that didn&#8217;t fit this tendency was Ukraine, I didn&#8217;t like it at all, although I live here <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> In my opinion, Eurovison is mainly the song contest, not the strip show and acrobatics. Yes, it was nice, flashing lights, the tricks, that guys in the Roman legionnaire&#8217;s costumes&#8230; But I didn&#8217;t see anything Ukrainian (except the flag) there! By the way, I didn&#8217;t like Russian Anastasia also - she wasn&#8217;t singing, she was shouting and Russians could make it more impressive (they can - you saw the show!)<br />
Final results actually reflected my impressions, Norway was really the best. However, I also liked:</p>
<ul>
<li><a href="http://www.youtube.com/watch?v=oEMno0Y5sUQ">Albania</a> - The girl was nice and the song also seemed good to me</li>
<li><a href="http://www.youtube.com/watch?v=_p7b5cxVRAs">Island</a> - Nice girl, nice song, but my girl disagrees it was so good to get 2nd place <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li><a href="http://www.youtube.com/watch?v=-8gr5GS2Sno">Turkey</a> - Nice dance, nice girls, good style</li>
<li><a href="http://www.youtube.com/watch?v=RnAJjXuY5YM">Greece</a> - VERY nice dance and song</li>
<li><a href="http://www.youtube.com/watch?v=IVsgIZN4XHs">Armenia</a> - really, really good style and song</li>
<li><a href="http://www.youtube.com/watch?v=P6h7pnvftbg">Azerbaijan</a> - I like Arash, I liked the style, the song, the dance and the overall show</li>
<li><a href="http://www.youtube.com/watch?v=DA19fcNxccA">UK</a> - Great voice, nice song, but I didn&#8217;t like her dress.</li>
</ul>
<p>(sorry, didn&#8217;t find yesterday&#8217;s videos for UK and Armenia :()<br />
And I really liked <a href="http://www.youtube.com/watch?v=Hg-75lIrP7I">Alexander Rybak</a>. The Norway style, great acrobatics, really good song and an interesting mix of Slavic music with some Norway motives resulted in excellent impressions. When I just saw him on the scene, I thought - what that guy is doing there? But when he started singing, I immediately changed my mind - he was doing really good! I entirely agree, that he got his 1st place because he deserves it. However, he got 387 points and the 2nd place - around 200. I don&#8217;t think he was twice as good as Iceland <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Anyway, I&#8217;m thankful to my girl who made me see this great show, I liked it all.</p>
<p>And what did you like there?</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d195').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d195" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/&amp;title=Eurovision+2009+-+My+Impressions" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/&amp;title=Eurovision+2009+-+My+Impressions" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/&amp;title=Eurovision+2009+-+My+Impressions" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/&amp;title=Eurovision+2009+-+My+Impressions" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/&amp;title=Eurovision+2009+-+My+Impressions" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/&amp;title=Eurovision+2009+-+My+Impressions" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/&amp;t=Eurovision+2009+-+My+Impressions" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d195').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d195').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/05/17/eurovision-2009-my-impressions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>OOP in jQuery</title>
		<link>http://programmersnotes.info/2009/04/30/oop-in-jquery/</link>
		<comments>http://programmersnotes.info/2009/04/30/oop-in-jquery/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 20:19:51 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[UI]]></category>

		<category><![CDATA[jQuery]]></category>

		<category><![CDATA[framework]]></category>

		<category><![CDATA[OOP]]></category>

		<category><![CDATA[tip]]></category>

		<category><![CDATA[trick]]></category>

		<category><![CDATA[workaround]]></category>

		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://konstantin.takeforce.net/?p=12</guid>
		<description><![CDATA[Article shows several tricks, that allow to use full-featured OOP techniques in developing with jQuery. Everything is illustrated with live examples.


No related posts.]]></description>
			<content:encoded><![CDATA[<h2><a name="#intro">Intro</a></h2>
<p>OOP is a great concept, it simplifies the programmer&#8217;s life, especially in the field of interface development. Nearly all modern web-interfaces use JS in some way - for field highlighting, validation, help etc. It&#8217;s OK when there are such simple things.<br />
And now imagine, that we have a set of dialogue windows (HTML+CSS) for adding a comment, rating, sending to the friend. Each of these boxes has a number of functions - check if user is logged in(if not - show login form), send AJAX request, process response, show error/success messages, position the box on the page etc.</p>
<p><span id="more-12"></span></p>
<h2><a name="#problem">The problem</a></h2>
<p>First idea I had about them is to use simple inheritance, create Box class, derive CommentBox, RatingBox and other classes, but it turned out, that jQuery (I used it there, because system is built using Yii framework, which uses jQuery for client-side coding) doesn&#8217;t support extending classes like, for example, ExtJS or YUI. Yes, it has $.extend() function, but it just copies the object, not actually extends class. Why it is not the same? Let&#8217;s look here:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">//namespace</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> Popups = <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//class constructor</span></div>
</li>
<li class="li1">
<div class="de1">Popups.<span class="me1">Box</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//initialization here</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">Popups.<span class="me1">Box</span>.<span class="me1">prototype</span> = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; container:$<span class="br0">&#40;</span><span class="st0">&#8216;&lt;div class=&quot;container&quot;&gt;&lt;/div&gt;&#8217;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; content:<span class="kw2">null</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//dom elements generation here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; showHide:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//some logic here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//other methods and properties go here</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Normally (in ExtJS, for example) when writing:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Popups.<span class="me1">CommentBox</span> = $.<span class="me1">extend</span><span class="br0">&#40;</span><span class="kw2">true</span>,<span class="br0">&#123;</span><span class="br0">&#125;</span>, Popups.<span class="me1">Box</span>, <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&#8216;overriden!&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>I expect, that if I do:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> c = <span class="kw2">new</span> CommentBox<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">c.<span class="me1">create</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>I should get an alert window. However, this is not true for jQuery. It just copies everything and we get some messed things. So I gave this up and followed another pattern.</p>
<h2><a name="#solution">The solution</a></h2>
<p>Box object became container for all boxes. I took care of messaging, determining if user is logged, and all other routines. It also serves as a factory, because all clicking icons go to the showHide method and it has to determine, which box to show and create this inner object.<br />
And this inner object takes care of the box-specific logic. If it needs some common info, for example, the ID of the product it is called for, it calls the Box method (see LowCoupling GRASP principle here :)). So in the Box I have:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; getBoxType:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//determine the box we&#8217;re dealing with by the link class</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> tg = $<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">event</span>.<span class="me1">target</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;f&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;favourite&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;t&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;friend&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;ra&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;rate&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;c&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;comment&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;r&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;rss&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;add&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;addTag&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;b&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;bookmark&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8221;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; getInnerObj:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!us.<span class="me1">logged</span><span class="br0">&#41;</span><span class="co1">//if user is not logged, we show him the login box instead of anything else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">new</span> Popups.<span class="me1">LoginObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> cls;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">type</span> = <span class="kw1">this</span>.<span class="me1">getBoxType</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">switch</span> <span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;favourite&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">FavouriteObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;friend&#8217;</span>:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">FriendObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;rate&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">RateObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;comment&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">CommentObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;rss&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">RssObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;bookmark&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">BookmarkObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">default</span>:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> cls;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; show:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//some stuff</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj = <span class="kw1">this</span>.<span class="me1">getInnerObj</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> obj == <span class="st0">&#8216;object&#8217;</span> &amp;&amp; obj != <span class="kw2">null</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">content</span>.<span class="me1">append</span><span class="br0">&#40;</span>obj.<span class="me1">getContents</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
</ol>
</div>
<p>So instead of inheritance I implemented aggregation (Box aggregates CommmentObject, Favourite object etc).</p>
<p>Everything goes well until you want to to attach some method of current object as a handler to some event. For example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Popups.<span class="me1">Box</span>.<span class="me1">prototype</span> = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//properties here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; productID:<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">form</span>.<span class="me1">submit</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">onSubmit</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productID = <span class="kw1">this</span>.<span class="me1">parentBox</span>.<span class="me1">getItemID</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; onSubmit:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">productID</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//other methods follow</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Right? Wrong! Because when submit event occurs, jQuery will call the onSubmit method, but this will refer to the form, not to the Box object! However, it is logical to put processing of the form into the same object. In ExtJS and YUI you can pass the scope parameter for every callback function, so you can control the &#8220;this&#8221; variable in the callback call. jQuery doesn&#8217;t allow this. I used the following workaround:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Popups.<span class="me1">Box</span>.<span class="me1">prototype</span> = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//properties here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; productID:<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> ctx = <span class="kw1">this</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">form</span>.<span class="me1">submit</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.<span class="me1">onSubmit</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productID = <span class="kw1">this</span>.<span class="me1">parentBox</span>.<span class="me1">getItemID</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; onSubmit:<span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">productID</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//other methods follow</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>If you need the object, that triggered an event, just pass &#8220;this&#8221; there:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> ctx = <span class="kw1">this</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">form</span>.<span class="me1">submit</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.<span class="me1">onSubmit</span><span class="br0">&#40;</span><span class="kw1">this</span>, event<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; productID = <span class="kw1">this</span>.<span class="me1">parentBox</span>.<span class="me1">getItemID</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">onSubmit:<span class="kw2">function</span><span class="br0">&#40;</span>form, event<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">productID</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Having these 2 tricks you can use OOP approach to designing reusable interface components in a handy modern way.</p>
<h2><a name="#inheritance">Back to inheritance</a></h2>
<p>Since I discovered the problem with inheritance, I want to develop a plugin, that fixes this, that will provide nice class inheritance model to take jQuery a step ahead. I was going to digg the ExtJS&#8217;s Ext.extend() code and find out how this is done correctly, but while preparing an article, I found this material, which is actually a guide for creation of such plugin: <a href="http://phrogz.net/JS/Classes/OOPinJS2.html">http://phrogz.net/JS/Classes/OOPinJS2.html</a>. Unfortunately, I don&#8217;t have time for this now, but I plan to do this in the nearest future. If someone does this earlier, please let me know <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3><a name="#reading">Links</a></h3>
<ul>
<li><a href="http://extjs.com/">ExtJS</a> - a component, interface-oriented JS framework</li>
<li><a href="http://developer.yahoo.com/yui/">YUI</a> - Yahoo library for the user interface. ExtJS was derived from it long ago. They are similar in extending and event processing (the &#8220;scope&#8221; I mentioned initially appeared in YUI)</li>
<li><a href="http://jquery.com/">jQuery</a> - a lightweight VERY popular JS framework with an easily extensible structure, but lacks OOP techniques!</li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d12').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d12" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/04/30/oop-in-jquery/&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/04/30/oop-in-jquery/&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/04/30/oop-in-jquery/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/04/30/oop-in-jquery/&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/04/30/oop-in-jquery/&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/04/30/oop-in-jquery/&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/04/30/oop-in-jquery/&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/04/30/oop-in-jquery/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/04/30/oop-in-jquery/&amp;t=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d12').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d12').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/04/30/oop-in-jquery/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creator and Information Expert principles - GRASP (Design patterns series)</title>
		<link>http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/</link>
		<comments>http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 13:37:32 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[Design patterns]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[design pattern]]></category>

		<category><![CDATA[efficiency]]></category>

		<category><![CDATA[grasp]]></category>

		<category><![CDATA[OOP]]></category>

		<category><![CDATA[polymorphism]]></category>

		<category><![CDATA[tutorial]]></category>

		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://konstantin.takeforce.net/?p=13</guid>
		<description><![CDATA[Post gives an overview of design patterns, GRASP principles and explains first two of them. Everything is illustrated with code and practical examples.


Related posts:<ol><li><a href='http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/' rel='bookmark' title='Permanent Link: Low Coupling and High Cohesion - GRASP (Design patterns series)'>Low Coupling and High Cohesion - GRASP (Design patterns series)</a></li><li><a href='http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/' rel='bookmark' title='Permanent Link: What is OOP (Object-oriented programming)?'>What is OOP (Object-oriented programming)?</a></li><li><a href='http://programmersnotes.info/2009/03/03/difference-between-adapter-and-template-method-pattern/' rel='bookmark' title='Permanent Link: Difference between Adapter and Template Method pattern'>Difference between Adapter and Template Method pattern</a></li></ol>]]></description>
			<content:encoded><![CDATA[<h2><a name="intro">Preface</a></h2>
<p>This is an introductory post to the series, where I&#8217;ll cover all design patterns, their usage, diagrams. Besides patterns, I am going to guide you through the principles of the high-quality application design. We&#8217;ll start from these principles (GRASP), and then advance to patterns, see how they use these principles and develop several systems (architecture only). The whole series will take 10-15 posts. However, I don&#8217;t have exact plan yet <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> So, let&#8217;s go!</p>
<h2>So what we&#8217;re talking about?</h2>
<p>Actually, design pattern is no more, than a proven solution to some common problem. When saying &#8220;problem&#8221; I mean things you face with when developing the application architecture. These solutions are intended to make you application better - more extensible, flexible, simple. </p>
<p>Common examples of design problems (Let&#8217;s agree that in this series &#8220;design&#8221; means application, not graphics or DB design) are:<br />
<span id="more-13"></span></p>
<ul>
<li>Organizing access to the shared resource. This can be application object, DB abstraction object, Session object or other similar things. Main problem is that they should be created only once and accessed in every method easily. Singleton pattern solves this problem.</li>
<li>Creating the easily extensible application. For example, code shopping cart in the way, that allows to add new payment or taxation modules easily. Adapter pattern saves us here.</li>
<li>Providing the simple interface for accessing features of the application or application module, hiding all the underlying classes/functions from the user. Façade pattern does this.</li>
<li>Separating data from data processing and from presentation. You&#8217;ve definitely heard about MVC (Model-View-Controller) - that&#8217;s also a pattern!</li>
</ul>
<p>So patterns are everywhere in OOP and knowing them is essential for high-quality and extensible application design.</p>
<h2><a name="basics">The basics</a></h2>
<p>However, patterns themselves are implementation of the 9 basic principles (GPASP - General Responsibility Assignment Software Principles).<br />
<div id="attachment_162" class="wp-caption aligncenter" style="width: 310px"><a href="http://programmersnotes.info/wp-content/uploads/2009/03/patterns.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/03/patterns-300x252.png" alt="GRASP and patterns" title="patterns" width="300" height="252" class="size-medium wp-image-162" /></a><p class="wp-caption-text">GRASP and patterns</p></div><br />
All OOP design is about creating real-world abstractions (classes), design methods (responsibilities) and their interaction. The key thing to good architecture is assigning responsibilities to the classes correctly. GRASP deals exclusively with this problem. We&#8217;ll go through all of them:</p>
<ul>
<li><a href="#creator">Creator</a></li>
<li><a href="#expert">Information Expert</a></li>
<li>Low Coupling</li>
<li>High Cohesion</li>
<li>Controller</li>
<li>Polymorphysm</li>
<li>Pure Fabrication</li>
<li>Indirection</li>
<li>Protected Variations</li>
</ul>
<p>Today we&#8217;ll start from the first principle.</p>
<h2><a name="creator">Creator</a></h2>
<p>The most common problem in OO design is &#8220;Who should create object X?&#8221; This principle states, that object A should create object B if:</p>
<ul>
<li><a href="#a_aggregates_b">A contains or aggregates B</a> (e.g. Object User should initialize object Address)</li>
<li><a href="#a_records_b">A records B</a> (saves to DB or file)</li>
<li>A closely uses B</li>
<li>A has the initialization data that should be passed to B when object is created</li>
</ul>
<p>If any of these conditions are true, then A should create B. Now let&#8217;s demonstrate how these principles work in real examples.</p>
<h3><a name="a_aggregates_b">A contains or aggregates B</a></h3>
<p>Consider we have table. Common table consists of desk and legs.<br />
<div id="attachment_166" class="wp-caption aligncenter" style="width: 310px"><a href="http://programmersnotes.info/wp-content/uploads/2009/03/table.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/03/table-300x277.png" alt="Common table" title="table" width="300" height="277" class="size-medium wp-image-166" /></a><p class="wp-caption-text">Common table</p></div><br />
If we use object decomposition to program the table, we&#8217;ll get 3 classes:</p>
<ul>
<li>
		Table (blue box on image):</p>
<ul>
<li>Desk (light grey)</li>
<li>Leg (wooden)</li>
</ul>
</li>
</ul>
<blockquote><p>Object decomposition basically means breaking some entity into parts and representing each part with a object. These parts can be decomposed further. For example, we can break Table into Desk and Leg. And then break Leg into Screw, Plank. In complex system there is a number of such levels.</p></blockquote>
<p>If we start programming this, we can go in 2 ways. For example, we don&#8217;t want to use the Creator principle and will create Table class as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$desk</span>;<span class="co1">//desk object</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$legs</span>;<span class="co1">//array of legs</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span>Desk <span class="re0">$d</span>, <span class="re0">$l</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">desk</span> = <span class="re0">$dd</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">legs</span> = <span class="re0">$l</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Then Desk and Leg classes:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Desk</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$width</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$length</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$w</span>, <span class="re0">$l</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">width</span> = <span class="re0">$w</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">length</span> = <span class="re0">$l</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">height</span> = <span class="re0">$h</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Leg</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$width</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$length</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$w</span>, <span class="re0">$l</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">width</span> = <span class="re0">$w</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">length</span> = <span class="re0">$l</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">height</span> = <span class="re0">$h</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Yes, Desk and Leg classes are really the same cause they both are parallelepipeds. So we can create Parallelepiped class and extend both of them from it. We&#8217;ll do it later, now we&#8217;re talking about object creation. The above classes can be used in the following way:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$desk</span> = <span class="kw2">new</span> Desk<span class="br0">&#40;</span><span class="nu0">900</span>,<span class="nu0">900</span>,<span class="nu0">20</span><span class="br0">&#41;</span>;<span class="co1">//90 cm width, 90 length (square) and 2cm height. A normal table for kitchen <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span></div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$legs</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="nu0">40</span>,<span class="nu0">40</span>, <span class="nu0">880</span><span class="br0">&#41;</span>,<span class="co1">//4&#215;4 cm square and 88cm height. 88cm leg + 2 cm desk = 90cm full height</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="nu0">40</span>,<span class="nu0">40</span>, <span class="nu0">880</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="nu0">40</span>,<span class="nu0">40</span>, <span class="nu0">880</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="nu0">40</span>,<span class="nu0">40</span>, <span class="nu0">880</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$table</span> = <span class="kw2">new</span> Table<span class="br0">&#40;</span><span class="re0">$desk</span>, <span class="re0">$legs</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Yes, the object is created, but large part of the creation logic is outside of the object. The class user (the programmer who is using this class) should know the internal structure of objects to create the whole object. This approach is inefficient when we deal with large application; on the other side, such solution can&#8217;t be reused without changing the code. And it violates the first condition of using the Creator principle. Either Desk and Leg are parts of the Table object, so it should take care of their instantiation. So let&#8217;s learn how to solve such task correctly:<br />
We&#8217;ll create constructor for Table class with parameters:</p>
<ul>
<li>Width - width of the table</li>
<li>Length - length of the table</li>
<li>Height - height of the table</li>
<li>DeskHeight - height of the desk</li>
<li>LegSection - (assume we want square legs)</li>
</ul>
<p>So the end-user of the class should specify only these 5 parameters, we don&#8217;t ask him to create parts of our object. OK, let&#8217;s code it:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$desk</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$legs</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$width</span>, <span class="re0">$length</span>, <span class="re0">$height</span>, <span class="re0">$deskHeight</span>, <span class="re0">$legSection</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">desk</span> = <span class="kw2">new</span> Desk<span class="br0">&#40;</span><span class="re0">$width</span>, <span class="re0">$length</span>, <span class="re0">$deskHeight</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>; <span class="re0">$i</span> &lt; <span class="nu0">4</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">legs</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="re0">$legSection</span>, <span class="re0">$legSection</span>, <span class="re0">$height</span> - <span class="re0">$deskHeight</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Desk and Leg classes remain the same. So here we followed the first condition (actually, the most important) and got nice architecture.</p>
<h3><a name="a_records_b">A records B</a></h3>
<p>After detailed discussion of the first condition, this should be quite evident. Usually if some object saves another one, it is his part. For example, we have some system with users. Each user may have several addresses. So Address will be the part of User (it is quite logical to design so), and User object will save all addresses in one field (Using serialization). And when loading, it will load the address info from DB and create Address objects. This is example of both 2, 3 and 4 item. The User object saves Address. User object has initialization info for Address and user object closely uses Address. By the way, first item is also here, Address is part of User object.<br />
As you see, all these conditions often go together.<br />
I think, that the final 2 items should be evident. </p>
<h4>Further development</h4>
<p>Tasks for improvement your skills:</p>
<ul>
<li>Extend both Leg and Desk from the Parallelipiped class</li>
<li>Implement getHeight() method for Table, Desk and Leg. Hint: In Leg and Desk classes it should simply return height. In Table class it should return sum of leg height and Desk height</li>
<li>Implement setHeight method for all classes. Hint: when setting height for table, you should actually increment Leg&#8217;s height</li>
<li>Implement Saving and loading from file or DB. Hint: actually you have to save only 5 parameters - those passed to table constructor. So when saving and loading Table class with interact with Desk and Leg classes. And when loading - will create these classes.</li>
</ul>
<h2><a name="expert">Information Expert</a></h2>
<p>The last condition of applying the Creator principle shows, that object A should have all info about B to create it. In other words, A should be <em>Expert</em> in info about B. Information Expert principle helps us assigning all responsibilities in the project. Real-world application usually holds a number of classes, that perform hundreds and thousands of actions, so it is essential to assign responsibilities correctly. The principle itself is fairly simple - you should give the responsibilities to the object it has information for. For example, assigning Table class responsibility to create Desk is correct because it has info about it. Assigning User responsibility to save data to DB is also correct, because User holds data about user and can perform this task without any other objects (assuming it deals with DB without DB abstraction layer). And assigning User to save information about Group is incorrect, because User will have to &#8220;ask&#8221; Group about it&#8217;s properties - ID, name, access rights etc. in order to save it correctly. In this case it&#8217;s much better to assign Group responsibility to save itself. And Use can call save() method on Group object.</p>
<p>In the next post I&#8217;ll cover Low Coupling and High Cohesion principles and illustrate it with practical examples. Subscribe to RSS not to miss it!<br />
Feel free to ask any questions in comments or by email: <a href="mailto:konstantin.mirin[at]programmersnotes.info">konstantin.mirin[at]programmersnotes.info</a></p>
<h4>Further reading</h4>
<ul>
<li><a href="http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)">GRASP</a></li>
<li><a href="http://en.wikipedia.org/wiki/Craig_Larman">Larman, Craig</a> (2005). Applying UML and Patterns - An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd ed.). Prentice Hall PTR. ISBN 0-13-148906-2.</li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d13').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d13" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/&amp;title=Creator+and+Information+Expert+principles+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/&amp;title=Creator+and+Information+Expert+principles+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/&amp;title=Creator+and+Information+Expert+principles+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/&amp;title=Creator+and+Information+Expert+principles+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/&amp;title=Creator+and+Information+Expert+principles+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/&amp;title=Creator+and+Information+Expert+principles+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/&amp;t=Creator+and+Information+Expert+principles+-+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d13').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d13').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>Related posts:<ol><li><a href='http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/' rel='bookmark' title='Permanent Link: Low Coupling and High Cohesion - GRASP (Design patterns series)'>Low Coupling and High Cohesion - GRASP (Design patterns series)</a></li><li><a href='http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/' rel='bookmark' title='Permanent Link: What is OOP (Object-oriented programming)?'>What is OOP (Object-oriented programming)?</a></li><li><a href='http://programmersnotes.info/2009/03/03/difference-between-adapter-and-template-method-pattern/' rel='bookmark' title='Permanent Link: Difference between Adapter and Template Method pattern'>Difference between Adapter and Template Method pattern</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Trick with php switch()</title>
		<link>http://programmersnotes.info/2009/03/06/trick-with-php-switch/</link>
		<comments>http://programmersnotes.info/2009/03/06/trick-with-php-switch/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 10:41:39 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[tip]]></category>

		<category><![CDATA[trick]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=151</guid>
		<description><![CDATA[I bet you didn&#8217;t know, that switch can be used to answer the question: &#8220;Which of the N following statements is true?&#8221; Here is how this can be done:



&#60;?php


$a = 1;


$b = 15;


$c = 20;


switch &#40;true&#41; 


&#123;


&#160; &#160; &#160; &#160; case $a &#62; $b:


&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#123;


&#160; &#160; &#160; &#160; [...]


Related posts:<ol><li><a href='http://programmersnotes.info/2009/02/20/nice-trick-with-for-loop/' rel='bookmark' title='Permanent Link: Nice trick with FOR loop'>Nice trick with FOR loop</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>I bet you didn&#8217;t know, that <strong>switch</strong> can be used to answer the question: &#8220;Which of the N following statements is true?&#8221;<span id="more-151"></span> Here is how this can be done:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$a</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$b</span> = <span class="nu0">15</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$c</span> = <span class="nu0">20</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">switch</span> <span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="re0">$a</span> &gt; <span class="re0">$b</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;$a &gt; $b&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="br0">&#40;</span><span class="re0">$a</span> &gt; <span class="re0">$b</span><span class="br0">&#41;</span> &amp;&amp; <span class="br0">&#40;</span><span class="re0">$c</span> &gt; <span class="re0">$a</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;($a &gt; $b) &amp;&amp; ($c &gt; $a); $c is the biggest, $b is the lowest&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="br0">&#40;</span><span class="re0">$c</span> &gt; <span class="re0">$b</span><span class="br0">&#41;</span> &amp;&amp; <span class="br0">&#40;</span><span class="re0">$c</span> &gt; <span class="re0">$a</span><span class="br0">&#41;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;$c is the biggest again!&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">default</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8217;some default value&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
</ol>
</div>
<p>And little description - you can write true or false in <strong>switch()</strong>, this will cycle through the case (statement) and execute the commands, that goes below it. In our case, only third statement is true - it prints out, that $c is the biggest.<br />
You shouldn&#8217;t use it instead of <strong>if</strong> or <strong>if&#8230;elseif&#8230;else</strong> every time, but if you know this trick, you&#8217;ll see when this can be applied.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d151').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d151" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/03/06/trick-with-php-switch/&amp;title=Trick+with+php+switch%28%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/03/06/trick-with-php-switch/&amp;title=Trick+with+php+switch%28%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/03/06/trick-with-php-switch/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/03/06/trick-with-php-switch/&amp;title=Trick+with+php+switch%28%29" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/03/06/trick-with-php-switch/&amp;title=Trick+with+php+switch%28%29" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/03/06/trick-with-php-switch/&amp;title=Trick+with+php+switch%28%29" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/03/06/trick-with-php-switch/&amp;title=Trick+with+php+switch%28%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/03/06/trick-with-php-switch/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/03/06/trick-with-php-switch/&amp;t=Trick+with+php+switch%28%29" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d151').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d151').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>Related posts:<ol><li><a href='http://programmersnotes.info/2009/02/20/nice-trick-with-for-loop/' rel='bookmark' title='Permanent Link: Nice trick with FOR loop'>Nice trick with FOR loop</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/03/06/trick-with-php-switch/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Return several values from PHP function</title>
		<link>http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/</link>
		<comments>http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 07:38:40 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Add new tag]]></category>

		<category><![CDATA[tip]]></category>

		<category><![CDATA[trick]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=148</guid>
		<description><![CDATA[Show simple way to return several values from PHP function


Related posts:<ol><li><a href='http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/' rel='bookmark' title='Permanent Link: Grabbing password-protected content with cURL'>Grabbing password-protected content with cURL</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>As you know, PHP can return only one value from the function. However, there is a simple workaround - we return an indexed array from function and immediately split it into variables using list():</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> ret<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//something useful here</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">return</span> <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="re0">$varA</span>, <span class="re0">$varB</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/list"><span class="kw3">list</span></a><span class="br0">&#40;</span><span class="re0">$a</span>, <span class="re0">$b</span><span class="br0">&#41;</span> = ret<span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="co1">//we&#8217;ll have $varA in $a and $varB in $b after this</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d148').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d148" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/&amp;title=Return+several+values+from+PHP+function" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/&amp;title=Return+several+values+from+PHP+function" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/&amp;title=Return+several+values+from+PHP+function" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/&amp;title=Return+several+values+from+PHP+function" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/&amp;title=Return+several+values+from+PHP+function" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/&amp;title=Return+several+values+from+PHP+function" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/&amp;t=Return+several+values+from+PHP+function" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d148').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d148').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>Related posts:<ol><li><a href='http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/' rel='bookmark' title='Permanent Link: Grabbing password-protected content with cURL'>Grabbing password-protected content with cURL</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Speeding up Yii or why should you use DB sessions</title>
		<link>http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/</link>
		<comments>http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 04:02:35 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
		
		<category><![CDATA[Databases]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Yii]]></category>

		<category><![CDATA[DB]]></category>

		<category><![CDATA[production]]></category>

		<category><![CDATA[session]]></category>

		<category><![CDATA[tip]]></category>

		<category><![CDATA[trick]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=135</guid>
		<description><![CDATA[Quick, effective and simple way how to speed up the Yii web application using the DB session storage.


Related posts:<ol><li><a href='http://programmersnotes.info/2009/03/04/handling-application-parameters-in-yii-using-the-database/' rel='bookmark' title='Permanent Link: Handling Application Parameters in Yii - Using the Database'>Handling Application Parameters in Yii - Using the Database</a></li><li><a href='http://programmersnotes.info/2009/03/03/registering-group-of-js-files-in-yii/' rel='bookmark' title='Permanent Link: Registering group of JS files in Yii'>Registering group of JS files in Yii</a></li><li><a href='http://programmersnotes.info/2009/02/20/yii-framework-tips-when-going-production/' rel='bookmark' title='Permanent Link: Yii framework - tips when going production'>Yii framework - tips when going production</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>Yesterday I&#8217;ve read a post on <a target="_blank" href="http://www.thedeveloperday.com/anti-asynchronous-ajax-calls-and-php-sessions/">The Developer&#8217;s Day blog</a>. Then tested this thing in Yii. There is a jQuery in my application, so I created test controller and test action, that only outputted &#8220;Hello, world&#8221; and called it from the firebug console <span id="more-135"></span>like this (I have url rewrite set up):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">$.<span class="me1">ajax</span><span class="br0">&#40;</span><span class="br0">&#123;</span>url:<span class="st0">&#8216;/test/test.html&#8217;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">$.<span class="me1">ajax</span><span class="br0">&#40;</span><span class="br0">&#123;</span>url:<span class="st0">&#8216;/test/test.html&#8217;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">$.<span class="me1">ajax</span><span class="br0">&#40;</span><span class="br0">&#123;</span>url:<span class="st0">&#8216;/test/test.html&#8217;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">$.<span class="me1">ajax</span><span class="br0">&#40;</span><span class="br0">&#123;</span>url:<span class="st0">&#8216;/test/test.html&#8217;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">$.<span class="me1">ajax</span><span class="br0">&#40;</span><span class="br0">&#123;</span>url:<span class="st0">&#8216;/test/test.html&#8217;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">$.<span class="me1">ajax</span><span class="br0">&#40;</span><span class="br0">&#123;</span>url:<span class="st0">&#8216;/test/test.html&#8217;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Yes, really it matters. Each call starts after the previous one ends and it takes long to finish them all. But each web application handles the concurrent requests by nature. So I started a <a target="_blank" href="http://www.yiiframework.com/forum/index.php/topic,1001.0.html">forum thread</a> to clarify this. And <a target="_blank" href="http://www.yiiframework.com/forum/index.php?action=profile;u=54">sebas</a> recommended me to use CDbHttpSession instead of standard one. Yes, I knew, that storing sessions in DB speeds up the application, but I thought that is because of more effective search. And it turns out, that besides more effective search it gives one more DB benefit - concurrent access to the session storage. That&#8217;s actually the reason #1 why we usually use databases instead of files <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
So here is a quick trick, that solves the problem:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// application components</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;components&#8217;</span>=&gt;array<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&#8230;&#8230;&#8230;.</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8217;session&#8217;</span> =&gt; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;class&#8217;</span> =&gt; <span class="st0">&#8217;system.web.CDbHttpSession&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8216;connectionID&#8217;</span> =&gt; <span class="st0">&#8216;db&#8217;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>It creates a new table YiiSession and uses it for session data. This time the JS test above runs much faster. Try it yourself!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d135').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d135" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/&amp;title=Speeding+up+Yii+or+why+should+you+use+DB+sessions" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/&amp;title=Speeding+up+Yii+or+why+should+you+use+DB+sessions" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/&amp;title=Speeding+up+Yii+or+why+should+you+use+DB+sessions" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/&amp;title=Speeding+up+Yii+or+why+should+you+use+DB+sessions" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/&amp;title=Speeding+up+Yii+or+why+should+you+use+DB+sessions" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/&amp;title=Speeding+up+Yii+or+why+should+you+use+DB+sessions" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/&amp;t=Speeding+up+Yii+or+why+should+you+use+DB+sessions" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d135').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d135').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>Related posts:<ol><li><a href='http://programmersnotes.info/2009/03/04/handling-application-parameters-in-yii-using-the-database/' rel='bookmark' title='Permanent Link: Handling Application Parameters in Yii - Using the Database'>Handling Application Parameters in Yii - Using the Database</a></li><li><a href='http://programmersnotes.info/2009/03/03/registering-group-of-js-files-in-yii/' rel='bookmark' title='Permanent Link: Registering group of JS files in Yii'>Registering group of JS files in Yii</a></li><li><a href='http://programmersnotes.info/2009/02/20/yii-framework-tips-when-going-production/' rel='bookmark' title='Permanent Link: Yii framework - tips when going production'>Yii framework - tips when going production</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/03/05/speeding-up-yii-or-why-should-you-use-db-sessions/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
