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

<channel>
	<title>Programmer&#039;s Notes &#187; PHP</title>
	<atom:link href="http://programmersnotes.info/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmersnotes.info</link>
	<description>Notes on the web-development and artificial intelligence.</description>
	<lastBuildDate>Mon, 18 Jul 2011 13:20:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Database structure and filtering approach. SMS Notification system.</title>
		<link>http://programmersnotes.info/2010/03/19/database-sms-notification/</link>
		<comments>http://programmersnotes.info/2010/03/19/database-sms-notification/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 06:00:38 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[DB Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[modelling]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[sms-notify]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=351</guid>
		<description><![CDATA[The database structure In the previous post I outlined the system&#8217;s specs and use cases. We also selected the primary use case we should start from – that is filtering screen. The complexity with DB design here is that we want maximum flexibility on the one hand and we don&#8217;t want to lose much in [...]


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<h2><a name="dbstruct">The database structure</a></h2>
<p>In the <a href="http://programmersnotes.info/2010/03/18/requirements-use-cases-sms-notification/">previous post</a> I outlined the system&#8217;s specs and use cases. We also selected the primary use case we should start from – that is <a href="http://programmersnotes.info/2010/03/18/requirements-use-cases-sms-notification/#groupadmin">filtering screen</a>.</p>
<p><span id="more-351"></span></p>
<p>The complexity with DB design here is that we want maximum flexibility on the one hand and we don&#8217;t want to lose much in performance on the other hand.<br />
Let&#8217;s recap what data about student we should store:</p>
<ol>
<li>Phone</li>
<li>Password</li>
<li>Email</li>
<li>ICQ</li>
<li>Name</li>
<li>Surname</li>
<li>Patronymic</li>
<li>Gender</li>
<li>Birthday</li>
<li>Faculty</li>
<li>Course</li>
<li>Group</li>
<li>Clubs</li>
<li>Tags</li>
<li>OK to send SMS (true/false)</li>
</ol>
<p>Items 1-9 and #15 are scalar values and can be easily stored in User table as fields of the correspondent type. While Faculty, Course, Group and Clubs are either N-M (Clubs) or 1-N relations. So obvious way to handle them is to create tables and cross-table for N-M relation. Tags can be also represented as N-M relation.<br />
It seems OK until we don&#8217;t want to extend the system by adding some more fields. But even without extensibility in mind, we should create 3 separate tables (for courses, faculties and clubs). However if we look closer at these, we&#8217;ll see that all of them are kind of groups and we can create a kind of hierarchy:</p>
<ul>
<li>University
<ul>
<li>Faculty
<ul>
<li>Course
<ul>
<li>Group</li>
</ul>
</li>
</ul>
</li>
<li>Club
<ul>
<li>Section</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>So we can consider everything to be group. With this approach we also store information about relations between these groups.<br />
With such approach we can add more types of groups in the future if needed. Or this solution can be ported to another domain model (e.g. school or business organization) without changes in the structure. We should only define, which groups are required for user. In this case Faculty, course and group are required. However, knowing the group, we can get both course and faculty. But I&#8217;m not sure if that is a good idea because this way we&#8217;ll have to make several queries to the DB in order to get faculty instead of one.<br />
Here&#8217;s the DB scheme we came up with:</p>
<div id="attachment_360" class="wp-caption aligncenter" style="width: 566px"><a href="http://programmersnotes.info/wp-content/uploads/2010/03/db.png"><img src="http://programmersnotes.info/wp-content/uploads/2010/03/db.png" alt="Database design" title="Database design" width="556" height="349" class="size-full wp-image-360" /></a><p class="wp-caption-text">Database design</p></div>
<h2><a name="filtering">Filtering it</a></h2>
<p>Let&#8217;s try to compose some simple queries to test if our DB can cope with them.</p>
<ol>
<li>Let&#8217;s get all male users who&#8217;re elder than 18:
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> User <span class="kw1">WHERE</span> gender=<span class="st0">&#8216;male&#8217;</span> <span class="kw1">AND</span> birthday &lt;= <span class="st0">&#8217;1992-03-19 23:59:59&#8242;</span></div>
</li>
</ol>
</div>
<p>Date for the birthday is set to the 18 years back from now. Everyone who was born before or on 19 of March 1992 will fall into the condition. And gender is obvious.</li>
<li>Let&#8217;s try to get all male that have first names starting with &#8216;K&#8217; and who study on the first or second course:
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> u.* <span class="kw1">FROM</span> User u <span class="kw1">INNER</span> <span class="kw1">JOIN</span> UserGroup ug <span class="kw1">ON</span> u.userID=ug.userID <span class="kw1">WHERE</span> u.gender=<span class="st0">&#8216;male&#8217;</span> <span class="kw1">AND</span> u.name <span class="kw1">LIKE</span> <span class="st0">&#8216;K%&#8217;</span> <span class="kw1">AND</span> ug.groupID <span class="kw1">IN</span> <span class="br0">&#40;</span><span class="nu0">5</span>,<span class="nu0">6</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Suppose that in Group table we have groups for the first course and second course and they have Ids 5 and 6 respectively.</li>
<li>Let&#8217;s try to search for all who is on the 3rd course and is also in Geeks&#8217; Club:
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> u.* <span class="kw1">FROM</span> User u <span class="kw1">INNER</span> <span class="kw1">JOIN</span> UserGroup ug <span class="kw1">ON</span> u.userID=ug.userID <span class="kw1">WHERE</span> ug.groupID <span class="kw1">IN</span> <span class="br0">&#40;</span><span class="nu0">7</span>, <span class="nu0">15</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>7 is ID of the group “3rd course” and 15 is ID of the group “Geeks&#8217; Club”.<br />
We have the problem with the last query <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  It will find all users who either studies on the 3rd course or attends Geeks&#8217; Club. However we need 3rd course and Geeks&#8217; club.<br />
There are two possible solutions here. One is SQL-based, another is PHP-based.</p>
<ol>
<li>1.SQL-based:
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> User <span class="kw1">WHERE</span> userID <span class="kw1">IN</span> <span class="br0">&#40;</span><span class="kw1">SELECT</span> userID <span class="kw1">FROM</span> UserGroup <span class="kw1">WHERE</span> userID <span class="kw1">IN</span><span class="br0">&#40;</span><span class="kw1">SELECT</span> userID <span class="kw1">FROM</span> UserGroup <span class="kw1">WHERE</span> groupID=<span class="nu0">15</span><span class="br0">&#41;</span> <span class="kw1">AND</span> groupID=<span class="nu0">7</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Here we select users who have group=15. Then from these users we select the ones who have group=7 as well, and finally we select user records that correspond the userIDs found.<br />
I think, the variant with INNER JOIN is also possible here (at least, joining results of the groups filtering instead of sub-querying). It just takes some time to figure it out and it has to deal more with optimization than with the principle of filtering.<br />
Anyway, the more groups we have here, the more complex this query becomes.<br />
<strong>UPDATE:</strong> <a href="http://www.sitepoint.com/forums/showthread.php?p=4542692">Here</a> I&#8217;ve been advised to use the following query which is more simple and efficient:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; u.*</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">FROM</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; USER u</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">INNER</span> <span class="kw1">JOIN</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; UserGroup ug1</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">ON</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; u.id = ug1.userID <span class="kw1">AND</span> ug1.groupID=<span class="nu0">7</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">INNER</span> <span class="kw1">JOIN</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; UserGroup ug2</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">ON</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; u.id = ug2.userID <span class="kw1">AND</span> ug2.groupID=<span class="nu0">15</span></div>
</li>
</ol>
</div>
<p>I think this is the best way of making such queries. Great thanks to <strong>ScallioXTX</strong></li>
<li>PHP-based. Basically, the principle is the same:
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$group7</span> = getUsersForGroup<span class="br0">&#40;</span><span class="nu0">7</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$group15</span> = getUsersForGroup<span class="br0">&#40;</span><span class="nu0">15</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$userIDs</span> = <a href="http://www.php.net/array_intersect"><span class="kw3">array_intersect</span></a><span class="br0">&#40;</span><span class="re0">$group7</span>, <span class="re0">$group15</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$users</span> = getUsersByIDs<span class="br0">&#40;</span><span class="re0">$userIDs</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>getUsersForGroup actually executes the query:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> userID <span class="kw1">FROM</span> UserGroup <span class="kw1">WHERE</span> groupID=<span class="br0">&#91;</span><span class="kw1">GROUP</span><span class="br0">&#93;</span></div>
</li>
</ol>
</div>
<p>getUsersByID executes the following query:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">SELECT</span> * <span class="kw1">FROM</span> User <span class="kw1">WHERE</span> userID <span class="kw1">IN</span> <span class="br0">&#40;</span><span class="br0">&#91;</span>ids<span class="br0">&#93;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>array_intersect is a PHP function that returns the values present in all given arrays, returns the intersection of sets.<br />
For example, if we give it 3 arrays:<br />
(1, 5, 8, 15)<br />
(5, 8, 15, 19)<br />
(1, 5, 15, 25)<br />
it will return (5, 15).<br />
In this case we execute several little queries instead of one big. In MySQL this often gives benefits in performance.</li>
</ol>
</li>
</ol>
<p>So that&#8217;s our filtering approach.</p>
<p>There is one thing to add here. If we have group #1 and #2 and group#2 is a child of #1. And user selected to filter users which are both in #1 and #2, then we should perform a little optimization and search only for #2 because that will be the correct answer to the query. This situation is stupid, but we should act smart even in a stupid queries <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Maybe there are better solutions. We&#8217;re open to them! Moreover, we realize that there are more experienced developers that already solved such problem. Help us <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Tomorrow we have a meeting where we&#8217;ll be discussing the architecture, classes etc. I&#8217;ll upload our results on the weekends or early next week. This depends on the amount of work done during the meeting.<br />
Stay tuned! Subscribe to my blog&#8217;s RSS, share your opinions/suggestions in comments and don&#8217;t miss the next post in this series!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d351').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d351" 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F&amp;title=Database+structure+and+filtering+approach.+SMS+Notification+system." 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F&amp;title=Database+structure+and+filtering+approach.+SMS+Notification+system." 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F" 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F&amp;title=Database+structure+and+filtering+approach.+SMS+Notification+system." 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://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F&amp;T=Database+structure+and+filtering+approach.+SMS+Notification+system." rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F&amp;title=Database+structure+and+filtering+approach.+SMS+Notification+system." 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?url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F&amp;title=Database+structure+and+filtering+approach.+SMS+Notification+system." 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F" 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://twitter.com/home/?status=Check+out+Database+structure+and+filtering+approach.+SMS+Notification+system.+@+http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></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://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F19%2Fdatabase-sms-notification%2F&amp;t=Database+structure+and+filtering+approach.+SMS+Notification+system." 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.d351').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.d351').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2010/03/19/database-sms-notification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Requirements and Use Cases. SMS Notification System.</title>
		<link>http://programmersnotes.info/2010/03/18/requirements-use-cases-sms-notification/</link>
		<comments>http://programmersnotes.info/2010/03/18/requirements-use-cases-sms-notification/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 06:00:56 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[experiece]]></category>
		<category><![CDATA[modelling]]></category>
		<category><![CDATA[sms-notify]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=342</guid>
		<description><![CDATA[Background I&#8217;ve graduated in March 2010 and I don&#8217;t need to visit university any more. However, I enjoyed studying there and I know what challenges my fellow students-programmers have. That&#8217;s why I continue the job started this autumn – computer club, we call it “Geeks&#8217; Club”. Sure, we&#8217;re not true geeks there, but we tend [...]


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<h2><a name="background">Background</a></h2>
<p>I&#8217;ve graduated in March 2010 and I don&#8217;t need to visit university any more. However, I enjoyed studying there and I know what challenges my fellow students-programmers have. That&#8217;s why I continue the job started this autumn – computer club, we call it “Geeks&#8217; Club”. Sure, we&#8217;re not true geeks there, but we tend to be <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So I&#8217;m helping students who&#8217;re interested in web-technologies and in object-oriented programming. I explain them things that are not covered in the standard university course. Things, that are more practical and they can be paid money for.</p>
<p><span id="more-342"></span></p>
<p>Here I&#8217;ll be covering both OOP and web-dev, but we&#8217;ll start from design hence OOP. The story began when some time passed after we started learning OOP at the club meetings and I decided that there is enough theory and separate examples, it&#8217;s time to build something practical that will be challenging for me too.<br />
I thought of different systems, but finally decided to pick up the SMS notification one. It requires both user interface, database, API (my friends want to communicate to it from standalone Java and C++ applications) and overall application design. In addition, I&#8217;d like such system to be deployed at our university and it is interesting for me to create one.</p>
<h2><a name="intro">Introduction</a></h2>
<p>This is going to be a series of articles, I&#8217;ll describe the progress and challenges we come across as we proceed with development. I think this kind of posts will be interesting for those who study OOP and want to apply what they&#8217;ve learned to some practical things. I&#8217;ll try to guide my students through the process using the iterative development methodology.</p>
<p>We have meetings once a week (on Saturdays), so I&#8217;ll post once or twice a week, depending on amount of work done on weekends and my free time during the week.</p>
<p>I think, this series will contain around 10 posts. We&#8217;re going to share the work done as an open-source project. There is nothing to share at the moment, so I&#8217;ll give the link when we code something <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2><a name="overview">System overview</a></h2>
<h3><a name="vision">Vision</a></h3>
<p>Obviously, the main purpose of the system is to send SMS notifications to students or tutors. These notifications may be quite different – from the timetable changes to some announcements or even warnings (e.g. “Due to the weather conditions, today is a day-off for all. Stay at home and have a good time”).</p>
<p>Actually the system isn&#8217;t that complex. Basically all it needs to do is to enable administrator pick a group of users, enter the message and send it through the SMS gateway. The tricky things start from “pick a group of users”. We want this to be as flexible as possible so we could use this system not only for university, but any other organization.</p>
<h3><a name="vision">University structure</a></h3>
<p>This is obvious to anyone who studied at the university in Ukraine, Russia and former USSR, but I&#8217;m not sure the structure is the same abroad. So we have the university. There are faculties (e.g. Computer Science, Philology etc). These faculties have specialties. For example, Computer Science faculty has 2 specialties – “Intellectual Systems of Decision-Making” (mine) and “Computer Engineering”. (For those interested about the difference, first one deals more with AI technologies while the latter – with system programming, networks etc).</p>
<p>Next level is “course”. We call it so, but actually that is the year of study. I know, there are special words for the 1st year students, for the 2nd etc. Here we just say “I&#8217;m on the first course”. That means “I&#8217;m studying the first year”.</p>
<p>All students that study the same specialty and are on the same course are called “Stream”. This is literal translation and I&#8217;m sure there is some more correct term. I&#8217;d be thankful if someone posts it in comments.</p>
<p>There are 2-4 groups in the “stream”. In our university they are names as follows: 101, 102, 103 etc. First number is course. In this case it is the first one. Second number is the faculty code. 0 is for computer science. And the third number is the group number. Thus 102 is the second group of first-year  students studying Computer Science. I realize that not all universities name groups like this, so we don&#8217;t want to stick to such naming in our product.</p>
<p>Now let&#8217;s go through the tutors and staff. There is a rector of a university. There are deans for every faculty. And all tutors are grouped by departments. Usually, departments relate to the faculty, but that&#8217;s not a rule. For example, on our faculty we have Department of Higher Mathematics, Department of Intellectual systems ans some others. One tutor usually belongs to one department.</p>
<p>In addition, there are different clubs for students like our “Geeks&#8217; Club”, translators&#8217; “Pickwick Club” etc. Some of these clubs (like ours) have departments (Web-development, OOP, Java, C++, Delphi, Administration etc). </p>
<p>We want to be able to filter users using all the above groups plus some more user-specific filters like age or gender.<br />
Let&#8217;s go through the planned features now.</p>
<h3><a name="userperspective">User perspective</a></h3>
<p>First of all, user can log in. User should enter his phone number and password. We&#8217;ll possibly add additional security for administrators. If administrator logs in, he enters his phone and password, but then a dynamic password is sent to his phone. And he completes login procedure only after he enters this dynamic password. We&#8217;re not sure if we&#8217;ll implement this. Your comments are welcome!</p>
<p>Next, user can edit his profile. He&#8217;ll have the following fields there:</p>
<ul>
<li>Phone</li>
<li>Email</li>
<li>Password (and confirmation)</li>
<li>Name, surname, patronymic</li>
<li>Gender</li>
<li>Birthday</li>
<li>ICQ</li>
<li>Faculty</li>
<li>Course (1st year, 2nd year etc)</li>
<li>Group</li>
<li>Clubs user is member of</li>
<li>Any tags user assigns to himself (e.g. “musician”)</li>
</ul>
<p>Plus user should check the option “receive SMS”.</p>
<p>However, user can&#8217;t edit phone directly because we&#8217;ll have to confirm it. So instead of giving him the field in the profile editing form, we&#8217;d rather give him a button or a link which will bring him a from where he puts new phone number and password. After that a dynamic password is sent to his phone via SMS and he enters this dynamic password and phone is updated.</p>
<p>Finally, user may register. When registering, he enters nearly the same data as when editing profile, but there is a dynamic password sent to him in order to confirm the phone number.<br />
All these screens are shown below.</p>
<div id="attachment_346" class="wp-caption aligncenter" style="width: 563px"><a href="http://programmersnotes.info/wp-content/uploads/2010/03/UserArea.png"><img src="http://programmersnotes.info/wp-content/uploads/2010/03/UserArea.png" alt="User area screens" title="UserArea" width="553" height="702" class="size-full wp-image-346" /></a><p class="wp-caption-text">User area screens</p></div>
<h3><a name="groupadmin">Group Administrator perspective</a></h3>
<p>Group administrator role may be assigned to the club&#8217;s leader, to the group monitor (or it&#8217;s better to say headman? What sound more “English”), to the tutor or headmaster. Basically everyone who needs to send notifications will have this role.</p>
<p>So, this type of users can do the same as the simple user plus searching users, sending email notifications and viewing statistics.<br />
Each group administrator will be limited to the groups he may view/notify. For example, headmaster can notify anyone. Tutor may notify all groups of students he tutors in. Group monitor may notify all students within his/her group.</p>
<p>Listing users and searching through them is done in the same screen. In the main part of the screen there is a list of users that are in the groups available to the group administrator. In the left side there is a “narrow search” panel which lists all filters that can be applied to the current set of users. Clicking the filter option (e.g. “Geeks&#8217; Club”) lists all users that fall into the current filter conditions. And it also refreshes the filtering options. For example, if there is no girls in the “Geeks&#8217; Club” then there is no point to show these option in the filter. This is harder to code, but it will bring excellent user experience.</p>
<p>In the right hand side there will be the “saved searches” panel. When you applied some searching criteria, you may save it with some label so you can quickly access this search in the future. Were going to create standard searches in addition to filtering to simplify the system&#8217;s usage.</p>
<p>This works as following. For example, you can view all Computer Faculty. But you need only girls from the Geeks&#8217; Club. So you filter by gender and then by club. And save this search as “Girls-geeks” <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  This immediately appears in the right “saved searches” panel.</p>
<p>These saved searched may be group-specific and user-specific. This basically means that when group admin saves search, he may select if he wants it to be private or he wants to share this search with other group administrators, who have access to the same groups as he does. We think, this will help users creating the searches that they really need. Sure, saved searched may be easily removed.</p>
<p>In the right side there will be also a “Send notification” panel. It will contain only textarea field and a “send” button. When you have your group ready, you type the message an click “send”. It asks for confirmation, displaying how much it would cost and sends it over.</p>
<p>Another page in the Group administrator&#8217;s  perspective is statistics. Here he&#8217;ll see how much messages he sent during the last day/week/month/overall and how much did it cost.</p>
<p>As we progress with development, we may add some settings, that will be the third page then. Here&#8217;s the filtering screen:</p>
<div id="attachment_344" class="wp-caption aligncenter" style="width: 568px"><a href="http://programmersnotes.info/wp-content/uploads/2010/03/FilterUsers.png"><img src="http://programmersnotes.info/wp-content/uploads/2010/03/FilterUsers.png" alt="Filter users screen" title="FilterUsers" width="558" height="370" class="size-full wp-image-344" /></a><p class="wp-caption-text">Filter users screen</p></div>
<p>Statistics screen is pretty straightforward, so we&#8217;ll omit it here.</p>
<h3><a name="administrator">Administrator&#8217;s perspective</a></h3>
<p>Administrator is almost god here <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  He can manage users, assign rights to the group administrators, set limits of SMS count, view logs and overall statistics.<br />
Users management screen will be nearly the same as filtering, with the only difference that when clicking the row with user&#8217;s you&#8217;ll see the profile screen with additional options:</p>
<ul>
<li>ban/unban</li>
<li>delete</li>
<li>phone editing does not require confirmation</li>
<li>administrator of (group selection)</li>
<li>notifications limit</li>
</ul>
<p>Logs and statistics screens are quite simple too.</p>
<h2><a name="usecase">Use cases</a></h2>
<p>After analyzing the above overview, we come up with the following use case diagram. It is pretty self-explanatory and written after the description above, so it is useful only as a summary.</p>
<div id="attachment_345" class="wp-caption aligncenter" style="width: 451px"><a href="http://programmersnotes.info/wp-content/uploads/2010/03/UseCaseModel.png"><img src="http://programmersnotes.info/wp-content/uploads/2010/03/UseCaseModel.png" alt="SMS Notification Use Cases" title="UseCaseModel" width="441" height="697" class="size-full wp-image-345" /></a><p class="wp-caption-text">SMS Notification Use Cases</p></div>
<p>Following the iterative development methodology, we should describe all usecases now, but I find this quite useless. After describing all usecases briefly, we should select 10% critical usecases and describe them in full. Full description includes the sequence of actions taken in the main success scenario and all alternative scenarios that may happen (e.g. DB error, connection failure, no users found etc). This is useful sometimes, but I think that for such simple application as this one, we ma skip this step either.</p>
<p>What is really useful about the iterative development at this stage is that it recommends picking 10% of the most critical usecases and start implementing them in full. By “critical” I usually mean those which may result in essential DB structure or application design changes. In our case there are 14 usecases.10% is 1.4 usecase. Let&#8217;s pick only one. The most critical usecase, that affects all the application design and DB structure is the user filtering. The success of an application depend on the stability and usability of this feature. So we should code it efficiently and then move to the other usecases.</p>
<p>I like this approach cause it is much more practical than designing the system from the interface. Changing interfaces and adopting them to core is much more simple and has less risk than doing vice versa.</p>
<p>So we decided to start with user filtering. The first thing we should create is the database structure. This will be covered in the tomorrow&#8217;s post.</p>
<p>I&#8217;d like to learn your opinion on all that. Maybe you can propose some better interface, or some nice filtering approach or just some idea for this. We&#8217;re open to all suggestions and comments! Thank you for reading my blog and following this project <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Subscribe to RSS and stay tuned!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d342').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d342" 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F" 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." 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://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;T=Requirements+and+Use+Cases.+SMS+Notification+System." rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." 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?url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F" 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://twitter.com/home/?status=Check+out+Requirements+and+Use+Cases.+SMS+Notification+System.+@+http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></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://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;t=Requirements+and+Use+Cases.+SMS+Notification+System." 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.d342').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.d342').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2010/03/18/requirements-use-cases-sms-notification/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is a framework and why you should use one?</title>
		<link>http://programmersnotes.info/2010/03/14/what-is-a-framework-and-why-you-should-use-one/</link>
		<comments>http://programmersnotes.info/2010/03/14/what-is-a-framework-and-why-you-should-use-one/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 12:53:49 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Yii]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=330</guid>
		<description><![CDATA[Building the best application&#8230; Most of the beginners and, sometimes, even more advanced programmers, want to develop everything from scratch because they want have control over the every aspect of their application. I entirely understand this tendency. When you learned how to program and you can code nearly anything you want (this relates more closely [...]


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<h2><a name="building-app">Building the best application&#8230;</a></h2>
<p>Most of the beginners and, sometimes, even more advanced programmers, want to develop everything from scratch because they want have control over the every aspect of their application. I entirely understand this tendency. When you learned how to program and you can code nearly anything you want (this relates more closely to scripting languages like php, perl, js etc), you start to do something global. Something that will change the world. This may be a new CMS, a new framework, new blog of e-commerce engine. And when you start this, you want to code it in the most efficient way, optimizing every little thing: every function, every SQL query. At the same time, you want to reuse the code you&#8217;re writing. So you tend to create more general solutions than you really need. And this slows down the performance. And again you go into optimization, limiting the possible uses to some extent.<br />
Finally, if you finish your application, it <em>may</em> be faster or better than similar ones (however, usually it is the same or worse), but remember how much time you spent developing it. Yes, you&#8217;re proud of yourself – you&#8217;ve created your own DB abstraction layer, that handles SQL injections in a very smart way. You&#8217;ve also developed your own ORM engine, that goes nicely with your DB abstraction layer. It is quite likely, that you&#8217;ve already created your very own template engine and a nice JS library that makes animation very easy. Great job, my fellow programmer!<br />
<span id="more-330"></span></p>
<h2><a name="brilliant">…but is it really brilliant?</a></h2>
<p>Now let&#8217;s stop for a moment and look back. <strong>Why</strong> you&#8217;ve started developing all that? Say you wanted to create a brand new blogging engine, that is better than WordPress. You&#8217;ve spent several months (I can&#8217;t believe you&#8217;ve created nice DB abstraction, ORM and template engine in a week) to develop all that stack. And all you got is just another blog engine! Yes, it is cool and is capable of doing hundreds of things. You&#8217;ve even thought of the external API, so you can send SMS to be automatically published on your blog. But actually that&#8217;s just a blog!<br />
Do you really think that the final result – blog  engine was worth 2 months of the hard work and sleepless nights? If you answer “yes” and you haven&#8217;t got a feeling that something is wrong, then then you&#8217;re either mega-guru and develop only facebook- and google-scale projects or just a beginner who is still very proud of himself that he can code anything and you want to overemphasize this.<br />
I think, every experienced developer was at that stage. I was there too. Moreover, I was there quite long and I regret of this.</p>
<h2><a name="frameworkdef">So what is the framework?</a></h2>
<p>By this time you may be quite bored and start thinking – what all that story has to deal with the framework. Actually the problem described above when you develop everything from scratch occurs when you don&#8217;t use any frameworks.<br />
So what is the framework? Basically it is a set of libraries/classes, tied together that speed up the development of a certain type of applications. For example, MFC (Microsoft Foundation Classes), Qt, wxWidgets are frameworks that simplify the user interface development. It obvious that you should not draw 6 states of the button and change it on different events when it comes to placing the button on some form. You just use the ready-made class. But when it comes to the web-development, some developers think that they don&#8217;t need the ready-made (and well-tested!) DB abstraction layer, proven authorization classes. They want to develop it themselves. As a result, we often see low-quality and buggy applications. On the other hand, using frameworks does not necessarily gives you the excellent code, but it definitely helps you to get where you need much faster.</p>
<h2><a name="useframeworks">Use frameworks!</a></h2>
<p>When you utilize good framework, you automatically get proven solutions in different spheres – DB connectivity, unit testing (most of frameworks provide tools for this or make it more simple), authentication, users and templates management. In addition, you also get a number of widgets, ready-made components for the problems you even don&#8217;t know about at the moment you start coding, but which usually come up during the development – caching, JS and CSS minimizing and publishing etc. Finally, you also get a bunch of extensions for a variety of tasks: RSS creation, CAPTCHA solutions, autocomplete widgets, treeview, drag&#038;drop components to name a few.<br />
The problem with framework usage for scripting languages is that they are quite simple and lots of developers don&#8217;t want to learn complex framework in order to achieve some simple task. And they are right&#8230; as long as their task is really simple. I think, “simple” is something that soesn&#8217;t require heavy DB usage, caching, user management, web-services integration and has very basic MVC implementation. That&#8217;s a simple homepage. All other projects should be developed with frameworks because that saves time and allows you to achieve better result.</p>
<h2><a name="choose">Choosing the framework</a></h2>
<p>But there is a little problem here <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  What framework is “good”? I wrote about this in one of my previous posts where I explained <a href="http://programmersnotes.info/2009/02/24/yii_framework_of_my_choice/">why I chose Yii as a PHP framework</a> for my project. Selecting wrong framework may result in project failure, so please refer to that post to see how I did that, maybe you&#8217;ll find that approach reasonable and join us in Yii community <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
And the last question I want to discuss here. The quality of code. I believe that using a framework with good design helps you to create better-structured applications that can be easily managed and supported. And the quality of code&#8230; I think this is up to programmer. It is definitely easier to create good application with properly designed framework just because you follow it&#8217;s patterns. However if you ignore the framework&#8217;s ideas and continue to create classes with static methods instead of simple functions, the framework is not very useful for you <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2><a name="notuse">When you should not use the frameworks</a></h2>
<p>Frameworks are good, but you should clearly understand what are they doing and use them only when appropriate. As a rule, framework provides a set of generic tools that simplify creation of any project. Key work is any. These techniques give good result for a standard use. However when it comes to some specific things, especially if they are very demanding in terms of performance, it&#8217;s time to evaluate your tools from the point of view of this specific project.<br />
Consider you have a framework designed for a web 2.0 applications. It works well for the most of the projects you create. However you&#8217;ve got to build an SMS portal. Unlike your usual applications, this doesn&#8217;t deal with HTTP a lot, it uses it&#8217;s own protocol. So 80-90% of your favourite framework&#8217;s tools will be useless here.<br />
If you&#8217;re building very large application that will be working under the high load, you should evaluate your framework and either reconfigure it or refuse from using it at all. Most of the great websites like facebook, for example, don&#8217;t use standard frameworks, but in order to become “facebook”, you should gain popularity and using the framework for your first version ill bring you to the marketing and launch much faster and you&#8217;ll start earning money.<br />
Similarly, if you&#8217;re building the business-card-like page, using the framework is a bit overkill <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2><a name="summary">Summary</a></h2>
<ol>
<li>Use frameworks because they simplify your development, help getting better code and simplify documenting your solution (framework documentation is already written!)</li>
<li>Choose framework very carefully!</li>
<li>When solving non-trivial tasks and develop extremely large applications, re-evaluate your framework&#8217;s features and decide if it really worth using it there.</li>
</ol>
<h4>Disclaimer</h4>
<ol>
<li>All above is my own opinion, formed during the 5+ years of everyday web-development. Maybe this is not the absolute truth, but I believe it is somewhere near it <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>Example with SMS portal was for illustration only, I&#8217;ve never developed ones and don&#8217;t know which protocols are used there. I&#8217;d be thankful if someone of my readers clarifies this.</li>
</ol>
<p>And what do you think about it? I&#8217;d be thankful if you share your thoughts in comments. Thanks!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d330').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d330" 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F" 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" 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://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;T=What+is+a+framework+and+why+you+should+use+one%3F" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" 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?url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" 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%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F" 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://twitter.com/home/?status=Check+out+What+is+a+framework+and+why+you+should+use+one%3F+@+http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></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://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;t=What+is+a+framework+and+why+you+should+use+one%3F" 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.d330').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.d330').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2010/03/14/what-is-a-framework-and-why-you-should-use-one/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Low Coupling and High Cohesion &#8211; 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 [...]


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></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 &#8211; &#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-tables1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables1.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/classescoupling1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/classescoupling1.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 &#8211; 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 &#8211; 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/classeslowcoupling1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/classeslowcoupling1.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 &#8211; triangle &#8211; we just add this class and then &#8211; 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-21.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables-21.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 &#8211; 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>Liked the post? 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%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+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%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+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%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F" 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%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+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://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;T=Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></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%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+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?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+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%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F" 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://twitter.com/home/?status=Check+out+Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29+@+http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></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://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;t=Low+Coupling+and+High+Cohesion+%26%238211%3B+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>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</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


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></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-auth1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/05/cookie-based-auth1.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 &#8211; this will make actual HTTP request to the URL we provide with parameters we provide (POST, GET variables, HTTP_REFERRER etc)</li>
<li>authenticate &#8211; this will send authentication info to the server using the requestContent function</li>
<li>getPage &#8211; 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? <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &#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 &#8211; 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-headers1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/05/http-headers1.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 &#8211; 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 &#8211; 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-area1.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>Liked the post? 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%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F&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%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F&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%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F" 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%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F&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://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F&amp;T=Grabbing+password-protected+content+with+cURL" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></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%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F&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?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F&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%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F" 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://twitter.com/home/?status=Check+out+Grabbing+password-protected+content+with+cURL+@+http%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></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://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F05%2F24%2Fgrabbing-password-protected-content-with-curl%2F&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>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/05/24/grabbing-password-protected-content-with-curl/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</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; [...]


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></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">&#8216;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 &#8211; 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 &#8211; 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>Liked the post? 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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F&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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F&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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F" 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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F&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://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F&amp;T=Trick+with+php+switch%28%29" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F&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?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F&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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F" 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://twitter.com/home/?status=Check+out+Trick+with+php+switch%28%29+@+http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></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://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Ftrick-with-php-switch%2F&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>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/03/06/trick-with-php-switch/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</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[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


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>As you know, PHP can return only one value from the function. However, there is a simple workaround &#8211; 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.d149').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d149" 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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F&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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F&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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F" 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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F&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://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F&amp;T=Return+several+values+from+PHP+function" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F&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?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F&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%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F" 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://twitter.com/home/?status=Check+out+Return+several+values+from+PHP+function+@+http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></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://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F06%2Freturn-several-values-from-php-function%2F&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.d149').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.d149').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/03/06/return-several-values-from-php-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with large files eficiently</title>
		<link>http://programmersnotes.info/2008/06/16/working-with-large-files-eficiently/</link>
		<comments>http://programmersnotes.info/2008/06/16/working-with-large-files-eficiently/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 16:59:20 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[file]]></category>

		<guid isPermaLink="false">http://konstantin.takeforce.net/?p=3</guid>
		<description><![CDATA[Once I had a task to import a number of data from datafeeds (that are in CSV or XML) into MySQL database. See solution in my another post. Here I want to tell about handling large files. Datafeeds there are 50-200M each, so reading all file at once was not possible due to memory limitations. [...]


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Once I had a task to import a number of data from datafeeds (that are in CSV or XML) into MySQL database. See solution in my another post. Here I want to tell about handling large files.</p>
<p>Datafeeds there are 50-200M each, so reading all file at once was not possible due to memory limitations. So we need to split it into portions. Here is the class, that solves the problem of downloading remote file, saving it locally and then reading by portions.<span id="more-19"></span> Position is saved in $this->Offset:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> WebServiceSource implements GenericSource</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="re0">$URI</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="re0">$TempFile</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">private</span> <span class="re0">$URIHandler</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="re0">$TMPHandler</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="re0">$Data</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="re0">$Offset</span> = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="re0">$EOF</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* Size to read from remote host at once, buffer</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; const BUFFER_LENGTH = <span class="nu0">102400</span>;<span class="co1">//1 MB</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="coMULTI">/**</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;* Number of records to read at once, from the local file</span></div>
</li>
<li class="li1">
<div class="de1"><span class="coMULTI">&nbsp; &nbsp; &nbsp;*/</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; const RECORDS_NUM = <span class="nu0">15</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$uri</span> = <span class="kw2">NULL</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">SetURI</span><span class="br0">&#40;</span><span class="re0">$uri</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">EOF</span> = <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">function</span> OpenURI<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">URIHandler</span> = @<a href="http://www.php.net/fopen"><span class="kw3">fopen</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">URI</span>, <span class="st0">&#8216;r&#8217;</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>!<span class="re0">$this</span>-&gt;<span class="me1">URIHandler</span><span class="br0">&#41;</span> throw <span class="kw2">new</span> ASException<span class="br0">&#40;</span><span class="st0">&#8216;WrongDatafeedURL&#8217;</span>, <span class="st0">&#8216;DFImport&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&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; <span class="kw2">private</span> <span class="kw2">function</span> OpenTempFile<span class="br0">&#40;</span><span class="re0">$mode</span> = <span class="st0">&#8216;w+&#8217;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">TMPHandler</span> = @<a href="http://www.php.net/fopen"><span class="kw3">fopen</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">TempFile</span>, <span class="re0">$mode</span><span class="br0">&#41;</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">$this</span>-&gt;<span class="me1">TMPHandler</span><span class="br0">&#41;</span> throw <span class="kw2">new</span> ASException<span class="br0">&#40;</span><span class="st0">&#8216;TempFileNotAccessible&#8217;</span>, <span class="st0">&#8216;DFImport&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&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; <span class="kw2">private</span> <span class="kw2">function</span> CloseURI<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/fclose"><span class="kw3">fclose</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">URIHandler</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&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; <span class="kw2">private</span> <span class="kw2">function</span> CloseTempFile<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/fclose"><span class="kw3">fclose</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">TMPHandler</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&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; <span class="kw2">private</span> <span class="kw2">function</span> IsDownloaded<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/file_exists"><span class="kw3">file_exists</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">TempFile</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">false</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="br0">&#40;</span><a href="http://www.php.net/time"><span class="kw3">time</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span> &#8211; <a href="http://www.php.net/filectime"><span class="kw3">filectime</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">TempFile</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &gt; TMP_EXPIRE<span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">function</span> Download<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</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">$this</span>-&gt;<span class="me1">IsDownloaded</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// &nbsp; &nbsp; &nbsp; &nbsp;set_time_limit(0);</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">OpenURI</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="re0">$this</span>-&gt;<span class="me1">OpenTempFile</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="kw1">while</span> <span class="br0">&#40;</span>!<a href="http://www.php.net/feof"><span class="kw3">feof</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">URIHandler</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="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$buffer</span> = <a href="http://www.php.net/fread"><span class="kw3">fread</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">URIHandler</span>, self::<span class="me2">BUFFER_LENGTH</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/fwrite"><span class="kw3">fwrite</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">TMPHandler</span>, <span class="re0">$buffer</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">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">CloseURI</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="re0">$this</span>-&gt;<span class="me1">CloseTempFile</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">function</span> GetFragment<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">OpenTempFile</span><span class="br0">&#40;</span><span class="st0">&#8216;r&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$RecordsNum</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">Data</span> = <span class="st0">&#8221;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/fseek"><span class="kw3">fseek</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">TMPHandler</span>, <span class="re0">$this</span>-&gt;<span class="me1">Offset</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="re0">$row</span> = <a href="http://www.php.net/fgets"><span class="kw3">fgets</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">TMPHandler</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &amp;&amp; <span class="re0">$RecordsNum</span> &lt;= self::<span class="me2">RECORDS_NUM</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; <span class="re0">$this</span>-&gt;<span class="me1">Data</span> .= <span class="re0">$row</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$RecordsNum</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="co1">//if number of records read is less then maximum, then we came to the</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// end of file</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">$RecordsNum</span> &lt; self::<span class="me2">RECORDS_NUM</span><span class="br0">&#41;</span> <span class="re0">$this</span>-&gt;<span class="me1">EOF</span> = <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">Offset</span> = <a href="http://www.php.net/ftell"><span class="kw3">ftell</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">TMPHandler</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">CloseTempFile</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="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">Data</span>;</div>
</li>
<li class="li1">
<div class="de1">&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; <span class="kw2">public</span> <span class="kw2">function</span> GetData<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">Download</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="kw1">if</span> <span class="br0">&#40;</span>!<span class="re0">$this</span>-&gt;<span class="me1">EOF</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">GetFragment</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="kw1">return</span> <span class="kw2">false</span>;</div>
</li>
<li class="li2">
<div class="de2">&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; <span class="kw2">public</span> <span class="kw2">function</span> SetURI<span class="br0">&#40;</span><span class="re0">$uri</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">URI</span> = <span class="re0">$uri</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">TempFile</span> = SYSTEM_ROOT.TMP_DIR.<a href="http://www.php.net/md5"><span class="kw3">md5</span></a><span class="br0">&#40;</span><span class="re0">$uri</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&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>And here is an example of how it works:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$source</span> = <span class="kw2">new</span> WebServiceSource<span class="br0">&#40;</span><span class="re0">$url</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">while</span> <span class="br0">&#40;</span><span class="re0">$res</span> = <span class="re0">$this</span>-&gt;<span class="me1">Source</span>-&gt;<span class="me1">GetData</span><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; <span class="re0">$this</span>-&gt;<span class="me1">Parser</span>-&gt;<span class="me1">SetRawData</span><span class="br0">&#40;</span><span class="re0">$res</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">Parser</span>-&gt;<span class="me1">Parse</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>
</ol>
</div>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d19').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d19" 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%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F&amp;title=Working+with+large+files+eficiently" 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%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F&amp;title=Working+with+large+files+eficiently" 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%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F" 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%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F&amp;title=Working+with+large+files+eficiently" 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://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F&amp;T=Working+with+large+files+eficiently" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></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%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F&amp;title=Working+with+large+files+eficiently" 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?url=http%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F&amp;title=Working+with+large+files+eficiently" 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%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F" 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://twitter.com/home/?status=Check+out+Working+with+large+files+eficiently+@+http%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></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://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2008%2F06%2F16%2Fworking-with-large-files-eficiently%2F&amp;t=Working+with+large+files+eficiently" 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.d19').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.d19').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2008/06/16/working-with-large-files-eficiently/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

