<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>CodeCall Programming Forum - Blogs - John</title>
		<link>http://forum.codecall.net/blogs/john/</link>
		<description>CodeCall is where developers can come to share programming ideas, articles, questions, answers, tips, tricks, source code, and other topics related to programming languages such as C++, Visual C++, C#, Visual Basic, ASP, ASP.NET, Java, and more.</description>
		<language>en</language>
		<lastBuildDate>Mon, 13 Feb 2012 15:05:45 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://forum.codecall.net/images/misc/rss.jpg</url>
			<title>CodeCall Programming Forum - Blogs - John</title>
			<link>http://forum.codecall.net/blogs/john/</link>
		</image>
		<item>
			<title>The Most Important Software Engineering Skills</title>
			<link>http://forum.codecall.net/blogs/john/1682-most-important-software-engineering-skills.html</link>
			<pubDate>Tue, 25 Jan 2011 04:01:15 GMT</pubDate>
			<description>I was recently asked by an freshman computer science student several questions about the software engineering industry. The one I would like to highlight is What are some of the most important software engineering skills? I immediately wanted to start naming off technologies and paradigms that I...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">I was recently asked by an freshman computer science student several questions about the software engineering industry. The one I would like to highlight is <i>What are some of the most important software engineering skills?</i> I immediately wanted to start naming off technologies and paradigms that I see used daily. Things like object oriented programming, test driven development, design/architecture patterns, and popular programming languages. But after a moment of contemplation, I responded with what I consider to be the two most valuable skills any [software] engineer can have.<br />
<br />
1. The ability to adapt to change (and the willingness to learn).<br />
Someone famous once said, "The only constant is change," and in the technology industry I think this is most prevalent. The popular programming languages and paradigms are quickly depreciated, and the software specification written today, is rewritten tomorrow.<br />
<br />
2. Balancing good business with good engineering. <br />
Every engineer wants to write the fastest, most efficient, modular, and extensible code possible. The good engineer will know this is not possible (due to imposed deadlines for practical business reasons), and will understand the trade-offs that can be made.<br />
<br />
For those of you who have more industry experience than I do, I am interested in hearing your thoughts.</blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/1682-most-important-software-engineering-skills.html</guid>
		</item>
		<item>
			<title>Illogical PHP Logic</title>
			<link>http://forum.codecall.net/blogs/john/365-illogical-php-logic.html</link>
			<pubDate>Fri, 17 Jul 2009 22:53:16 GMT</pubDate>
			<description>In algebra, after we learn the basic distributive, commutative, and associative properties, the transitive property of equality is usually next in the curriculum. For those of you who do not recall the terminology, the transitive property of equality says if $a == $b and $b == $n then $a == $n....</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">In algebra, after we learn the basic distributive, commutative, and associative properties, the transitive property of equality is usually next in the curriculum. For those of you who do not recall the terminology, the transitive property of equality says if $a == $b and $b == $n then $a == $n. Using this age old logic, you can prove FALSE == TRUE and 0 == 1 in PHP. Here is how:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<div class="bbcode_code"style="height:72px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Hooray&nbsp;for&nbsp;PHP&nbsp;logic&nbsp;and&nbsp;dynamic&nbsp;type&nbsp;casting?"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(((</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">)&nbsp;==&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">))&nbsp;==&nbsp;(</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((((</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">)&nbsp;==&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">))&nbsp;==&nbsp;(</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">))&nbsp;==&nbsp;(</span><span style="color: #0000BB">TRUE&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">));&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>Returns <div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:48px;">bool(true)
bool(true)</pre>
</div>Q.E.D<br />
<br />
Note: I am comparing values not types. That being said, forgive me when I flame you for using PHP inappropriately. A string is NOT an integer and an integer is NOT a boolean. They should not be used as such.<br />
<br />
<i><font size="1">Originally posted on <a href="http://www.johnciacia.com" target="_blank">johnciacia.com</a></font></i></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/365-illogical-php-logic.html</guid>
		</item>
		<item>
			<title>PHP6 - ifsetor() :?</title>
			<link>http://forum.codecall.net/blogs/john/239-php6-ifsetor.html</link>
			<pubDate>Mon, 16 Feb 2009 08:07:04 GMT</pubDate>
			<description>Web developers are constantly validating data, either through JavaScript or through a server side programming language such as PHP. If you have done any PHP development, I am sure you have been in the situation where, if a request variable is set, you want want to use that value, if it is not set,...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Web developers are constantly validating data, either through JavaScript or through a server side programming language such as PHP. If you have done any PHP development, I am sure you have been in the situation where, if a request variable is set, you want want to use that value, if it is not set, you would like it to default to a specific value. The most common and logical method to accomplish this is to use an if/else block.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<div class="bbcode_code"style="height:84px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">if(isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">&#93;))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">&#93;;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />}&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>The other, more dense, and in my opinion, more elegant option is to use the ternary operator.<br />
<div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<div class="bbcode_code"style="height:36px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;(isset(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">&#93;))&nbsp;?&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">&#93;&nbsp;:&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div> More information on the ternary operator can be found in a previous blog of mine: <a href="http://forum.codecall.net/blogs/john/14-ternary-operator.html" target="_blank">The Ternary Operator</a>.<br />
<br />
The ternary operator is very powerful. As you can see, it has transformed 5 lines of code into one, but programmers hate repetition. $_GET['foo'] was written twice! The situation above is encountered so often that PHP 6 will accommodate for the repetition. At first, there was going to be a new language construct called ifsetor(), however the PHP developers settled on a new operator. The previous situations will look like the following in PHP 6: <div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<div class="bbcode_code"style="height:36px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">&#93;&nbsp;?:&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div> The following will also be possible: <div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<div class="bbcode_code"style="height:36px;"><code><code><span style="color: #000000">
<span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">&#93;==</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;?:&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div> In other words, if $_GET['foo'] does not equal 2, it will default to 1.</blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/239-php6-ifsetor.html</guid>
		</item>
		<item>
			<title>1-2-3-4-5-6-7-8-9-0</title>
			<link>http://forum.codecall.net/blogs/john/235-1-2-3-4-5-6-7-8-9-0.html</link>
			<pubDate>Sat, 14 Feb 2009 03:13:03 GMT</pubDate>
			<description>Today, February 13, 2009 at 6:31 EST, the Unix time reached 1234567890. That is, 1,234,567,890 seconds since the Unix epoch (January 1, 1970).</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Today, February 13, 2009 at 6:31 EST, the Unix time reached 1234567890. That is, 1,234,567,890 seconds since the Unix epoch (January 1, 1970).</blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/235-1-2-3-4-5-6-7-8-9-0.html</guid>
		</item>
		<item>
			<title>D Link DNS 323 NAS</title>
			<link>http://forum.codecall.net/blogs/john/117-d-link-dns-323-nas.html</link>
			<pubDate>Sat, 17 Jan 2009 05:00:00 GMT</pubDate>
			<description>A few days ago I purchased a D-Link DNS 323 and a 1TB Western Digital hard drive to go along with it. I must say, I am thoroughly impressed. The initial setup took five minutes. I simply took off the front cover of the NAS, inserted the drive (no cables), plugged in the RJ-45 and power cables, and...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">A few days ago I purchased a D-Link DNS 323 and a 1TB Western Digital hard drive to go along with it. I must say, I am thoroughly impressed. The initial setup took five minutes. I simply took off the front cover of the NAS, inserted the drive (no cables), plugged in the RJ-45 and power cables, and it was all set.<br><br><br>The purpose of a NAS is to provide network storage. The D-Link NAS 323 extends beyond this default requirement and offers an abundance of applications such as a built in FTP server, UpnP support, and iTunes server, moreover it features a built in BitTorrent client and download manager. Since the NAS runs an embedded version of Linux I found it odd the web interface was built in ASP, nonetheless, it works well.<br><br><br>After searching around, I found out that when the when the DNS-323 boots it reads /etc/inittab which will execute the /etc/rc.sh script. The /etc/rc.sh script calls the /usr/sbin/chk_fun_plug script which gives the user the freedom to start their own services. Someone by the name of Fonz has created several scripts for the fun_plug package which includes telnet, ssh, lighthttpd, and many others. Installing this gives you much more freedom to use your device and does NOT void your warranty. I will be it's rsync capabilities to backup my home directory every night. I have also installed the Twonky Media Server which gives the NAS better UpnP support for my Xbox 360. I can now download torrents directly onto my NAS and stream the videos directly to my Xbox. It is a flawless media center.<br><br><br>The only issue I had was connecting the samba shares on my Ubuntu 8.10 machine. From what I have read, this is because Ubuntu 8.10 uses a newer authentication method than the one recognized by the NAS. To fix this, you need to edit your smf.conf file:<br><br><div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:36px;">sudo nano /etc/samba/smb.conf</pre>
</div> In the [global] section add <div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<pre class="bbcode_code"style="height:36px;">client lanman auth = yes</pre>
</div> and you should then be able to connect.<br><br><br>Although I have not used any other network storage devices, if you are in the market for a cheap ($180) one that works really well, I would recommend the D-Link DNS 323.<br><br></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/117-d-link-dns-323-nas.html</guid>
		</item>
		<item>
			<title>Tidy up your HTML</title>
			<link>http://forum.codecall.net/blogs/john/101-tidy-up-your-html.html</link>
			<pubDate>Thu, 18 Dec 2008 05:00:00 GMT</pubDate>
			<description><![CDATA[If you are a php programmer, you should be aware of the annoyances brought upon by html. I am not saying html is annoying, but when you combine php and html in the same document, it becomes a head ach.&nbsp; Even if you separate your presentation logic from your business logic and data, your html...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">If you are a php programmer, you should be aware of the annoyances brought upon by html. I am not saying html is annoying, but when you combine php and html in the same document, it becomes a head ach.&nbsp; Even if you separate your presentation logic from your business logic and data, your html source code will most likely look like crap. Sure you might spend a few minutes properly indenting your php source, but once that php executes and sends the response to the browser, at the very least, there will be issues with your indenting, that is, if your html is not on a single line. For example:<br><br><div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<div class="bbcode_code"style="height:36px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"&lt;table&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&nbsp;&lt;tbody&gt;&lt;tr&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&nbsp;&lt;td&gt;Hello&nbsp;World&lt;/td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&nbsp;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;"</span><span style="color: #007700">;&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div>Though the code may look properly indented. If you look at the source code once your browser renders it, it will all be on one line. Sure you can use the \t, \r, and \n characters, but but who really wants to do that?<br>[code=php]echo "<table>\r\n\t<tbody><tr>\r\n\t\t<td>Hello World</td>\r\n\t</tr>\r\n</tbody></table>";[/code]<br>Now if that does not look like crap, then I do not know what does. With PHP5 you can use the tidy functions available from PECL. Using this wonder function is easy. First you have to capture your html in an output buffer. Once you have the entire html contents in a string, use the tidy_parse_string function. There are several configuration options you can specify which can alter your html even further like making your document xhtml compliant. Here is what I use which will indent the html properly and make it xhtml compliant:<br><br><div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<div class="bbcode_code"style="height:36px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">?</span><span style="color: #0000BB">ob_start</span><span style="color: #007700">();</span><span style="color: #FF8000">//html&nbsp;code&nbsp;here$output&nbsp;=&nbsp;ob_get_contents();ob_end_clean();$options&nbsp;=&nbsp;array("output-xhtml"&nbsp;=&gt;&nbsp;true,&nbsp;"indent"&nbsp;=&gt;&nbsp;true,&nbsp;"indent-spaces"&nbsp;=&gt;&nbsp;4);&nbsp;$tidy&nbsp;=&nbsp;tidy_parse_string($output,&nbsp;$options);tidy_clean_repair($tidy);echo&nbsp;$output;&nbsp;<br /></span><span style="color: #0000BB"></span>
</span>
</code></code></div>
</div></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/101-tidy-up-your-html.html</guid>
		</item>
		<item>
			<title>Joomla! And Incompetent Programmers</title>
			<link>http://forum.codecall.net/blogs/john/84-joomla-incompetent-programmers.html</link>
			<pubDate>Sat, 29 Nov 2008 05:00:00 GMT</pubDate>
			<description><![CDATA[Over the past few days I have been attempting to migrate my Joomla 1.0 &amp; SMF website to Joomla 1.5 along with Fireboard. I decided to upgrade Joomla for the obvious reason - it is outdated, and I decided to migrate away from SMF due to the licensing issues between Joomla and SMF. These issues cause...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Over the past few days I have been attempting to migrate my Joomla 1.0 &amp; SMF website to Joomla 1.5 along with Fireboard. I decided to upgrade Joomla for the obvious reason - it is outdated, and I decided to migrate away from SMF due to the licensing issues between Joomla and SMF. These issues cause any extensions connecting the two pieces of software, for example a bridge, to be illegal (so much for the OpenSource "freedom").<br><br>In terms of forum software, there were several options. I decided to give Fireboard a try for one reason - it is seamlessly integrated with Joomla which means no bridge was necessary, and not to mention, many Joomla themes come packaged with styles for Fireboard. However, this is were my disgust for incompetent programmers grew even larger.<br><br>The issues started with the conversion from SMF to Fireboard. After thirty minutes or so of searching Google, I stumbled across one tool to do the conversion, and much to my dismay it was a commercial product, but with optimistic thought, I figured the $20 would be well spent on a decent piece of commercial software. Now, I have previously converted my forum from phpBB2 to SMF which did cause some issues, so I was not so naive to think the conversion would work perfectly, but I did expect it to work.<br><br>After I bought the software, I soon found out that all my users passwords would not be preserved. This was not a huge ordeal (but could be fixed by a competent programmer), so I went on with the conversion. On the third phase of the conversion there was a bug in an sql query which halted the conversion. I figured since the conversion utility was not intended for Joomla 1.5 (which I JUST upgraded), I decided I would restore my backup and try again on J!1.0. However, after the downgrade, the conversion utility once again did not work. At this point I was thoroughly pissed. I wasted $20 on a piece of **** software that did not work. I started looking through the code, and the issue was spotted with relative ease (the programmer did not know how to properly escape quotes). Luckily for me the bug was a quick fix because I could not stomach the ****ty code much longer. After that problem was solved, the conversion finished smoothly.<br><br>Once the posts, boards, users, and private messages were imported, it was time to set the board permissions. This was the point where I realized the ****ty Fireboard software has no “permissions.” Meaning, I cannot assign certain users to certain groups which would direct forum access. Forums are either assigned a public status or private status. And the private access is granted to the "Authors" access group - too bad for you if you would like more access groups. Can Fireboard even be considered a forum with out permissions?<br><br>Joomla! And 90% of the third party extensions are complete rubbish. Aside from ionFiles, I honestly cannot think of a single Joomla extension I have used that worked properly. I really do appreciate the OpenSource community and all the programmers who have attempted to help through contributions. However, you programmers are not helping the community in any way by releasing some crap code with security exploits, bugs that render the software useless, and no support. So next time you think of writing a new piece of software, pick up a book instead and learn how to write decent code.<br><br></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/84-joomla-incompetent-programmers.html</guid>
		</item>
		<item>
			<title>Google Web Toolkit</title>
			<link>http://forum.codecall.net/blogs/john/80-google-web-toolkit.html</link>
			<pubDate>Sun, 26 Oct 2008 04:00:00 GMT</pubDate>
			<description><![CDATA[<div>Ever since the web has ventured into its newest phase known as Web 2.0, one of the attributes web developers have started to focus on is the interface in which users interact. Gone are the days of static web pages built in html, table based layouts, and infamous page refresh. Developers have...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore"><div>Ever since the web has ventured into its newest phase known as Web 2.0, one of the attributes web developers have started to focus on is the interface in which users interact. Gone are the days of static web pages built in html, table based layouts, and infamous page refresh. Developers have made every attempt to drift away from the old look and feel and have started to concentrate on the “sexiness” by making the user interfaces more rich and application-like. This is still primarily done in JavaScript (though in can be done in Flash and Mozilla Prism/XUL), and if you are not aware of all the browsers idiosyncrasies when rendering JavaScript, the barrier to entry can be fairly high.</div><div><br></div><div>Several days ago when googleing “widgets” I came across a JavaScript library (ExtJS) which allows users to easily make rich interfaces. However, after looking around, I saw their library came in two flavors: ExtJS and ExtJS GWT (the latter being a plugin for the Google Web Toolkit). Up to this point, I have only heard of GWT. After looking through the documentation on the ExtJS website I saw a vast difference between the source code examples. The ExtJS code looked like archaic JavaScript while the source code for the ExtJS GWT examples looked more elegant and very similar to Java.</div><div><br></div><div>After looking into the GWT, it turns out that you are able to create interfaces using Java (a language more oriented toward GUI’s) then compile that interface into JavaScript code using the compiler provided by the GWT.</div><div><br></div></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/80-google-web-toolkit.html</guid>
		</item>
		<item>
			<title>Passwords</title>
			<link>http://forum.codecall.net/blogs/john/69-passwords.html</link>
			<pubDate>Mon, 18 Aug 2008 04:00:00 GMT</pubDate>
			<description><![CDATA[Passwords are our digital fingerprints. Shouldn't they be as secure as we want them? Apparently some websites don't think so.<br><br><br><font size="4">How are our passwords stored?</font><br>Most websites store users passwords as an md5 (message digest 5) hash. Essentially, your plain text...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Passwords are our digital fingerprints. Shouldn't they be as secure as we want them? Apparently some websites don't think so.<br><br><br><font size="4">How are our passwords stored?</font><br>Most websites store users passwords as an md5 (message digest 5) hash. Essentially, your plain text password is passed through a function that hashes your password. That hash is stored in the websites database. Since md5 is a one way hashing algorithm (there is no way to convert the md5 hash in the database back to your plain text password), password validation is done by comparing an md5 input and the md5 stored in the database. If the two match, you have successfully logged in.<br><br>In PHP, a basic authentication system might look similar to the following:<br>[code=php]$input = md5($_GET['password']);<br>$username = clean($_GET['username']);<br>$result = mysql_query(“SELECT `password` FROM `my_table` WHERE `user` = {$username}”);<br>$row = mysql_fetch_assoc($result);<br>if($input == $row['password']) {<br>&nbsp;&nbsp;&nbsp; echo “Valid password!”;<br>}[/code]<br><br>Even though this method has been the main stream method of storing passwords for years, the md5 hash has not been reversed. However, crackers have created whats called “rainbow tables.”<br><br><font size="4">What are rainbow tables?</font><br>Rainbow tables are essentially a large database that contains plain text passwords and its corresponding md5 hash. If a cracker performs an SQL injection on your website and is able to discover your websites user table (containing their password hashes), a lot of these hashes will be stored in the rainbow table thus allowing the intruder to gain access to users accounts.<br><br>As a result of rainbow tables, most easy passwords are more insecure now than ever before. Gone are the days of crackers sitting at their PC's attempting to guess your password by trying your wifes first name. As a result, I have come up with an eleven character password containing uppercase and lowercase characters along with numbers and special characters. Ten of the characters are of my Windows XP cd key; since I had to reinstall Windows so many times, I memorized my CD key – at least Windows is good for something, a password!<br><br>If you are like me, you are probably registered with several forums and other websites. Of which, you use the same password and attempt to use the same username. However, it seems 95% of the time, some user has already registered your username and you are forced to append some number to the end – thus forcing you to remember several usernames. At least you only have one password to remember – right? No! I have recently come across three websites that rejected my password because it was either too long or contained a special character. In leet speak WTF??!!one1. My secure password, my digital fingerprint, is rejected? I cannot fathom why.</blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/69-passwords.html</guid>
		</item>
		<item>
			<title>PHP Nowdocs</title>
			<link>http://forum.codecall.net/blogs/john/68-php-nowdocs.html</link>
			<pubDate>Sun, 17 Aug 2008 04:00:00 GMT</pubDate>
			<description><![CDATA[A few weeks ago Jordan blogged about the heredoc syntax. The point I want to raise in this blog, is the heredoc syntax parses PHP code, similar to double quotes around a string. For example<br>[code=php]$name = "John"; <br>$john = &lt;&lt;&lt;EOD <br>Hello World!&lt;br /&gt; <br>EOD; <br>&nbsp;<br>$world = &lt;&lt;&lt;EOD...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">A few weeks ago Jordan blogged about the heredoc syntax. The point I want to raise in this blog, is the heredoc syntax parses PHP code, similar to double quotes around a string. For example<br>[code=php]$name = "John"; <br>$john = &lt;&lt;&lt;EOD <br>Hello World!&lt;br /&gt; <br>EOD; <br>&nbsp;<br>$world = &lt;&lt;&lt;EOD <br>Hi $name!&lt;br /&gt;&lt;br &gt; <br>EOD;[/code] Would output <div class="bbcode_container">
	<div class="bbcode_quote">
		<div class="quote_container">
			<div class="bbcode_quote_container"></div>
			
				Hello World!<br>Hi John!
			
		</div>
	</div>
</div> Notice $name was parsed as a variable not as a literal. Until PHP 5.3 the only option to store and echo literal php code was to use single quotes around the code. However, you now have the option to use the <b>nowdoc</b> syntax. To use the nowdoc syntax all you have to do is place single quotes around the identifier. Using the nowdoc syntax our previous example becomes: [code=php]$name = "John"; <br>$john = &lt;&lt;&lt;'EOD' <br><br />
Hello World!&lt;br /&gt; <br>EOD; <br>&nbsp;<br>$world = &lt;&lt;&lt;'EOD' <br>Hi $name!&lt;br /&gt;&lt;br &gt; <br>EOD;[/code] The output now is <div class="bbcode_container">
	<div class="bbcode_quote">
		<div class="quote_container">
			<div class="bbcode_quote_container"></div>
			
				Hello World!<br>Hi $name!
			
		</div>
	</div>
</div></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/68-php-nowdocs.html</guid>
		</item>
		<item>
			<title>My Recent Shell Scripting</title>
			<link>http://forum.codecall.net/blogs/john/66-my-recent-shell-scripting.html</link>
			<pubDate>Fri, 15 Aug 2008 04:00:00 GMT</pubDate>
			<description>Recently I have been working on some shell scripts to aid in the installation of applications. One of the scripts I have been working on is a LAMP (linux, apache, mysql, and php) installer. For almost two years I ran a local WAMP server (same as LAMP but the operating system is Windows) which was...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Recently I have been working on some shell scripts to aid in the installation of applications. One of the scripts I have been working on is a LAMP (linux, apache, mysql, and php) installer. For almost two years I ran a local WAMP server (same as LAMP but the operating system is Windows) which was installed by clicking some executable (XAMPP / VertigoServ). However, Linux shows no sympathy for lazy Windows users, and thus forces us to use a restrictive package manager or install form source. Moreover, since I often need to recompile php to add new modules, I have decided to make my own LAMP installer. Below are some snippets of code that I have so far found useful in my shell script. <br>&nbsp;<br>Determine if a user is root: <br>[code=php]if [ "$(whoami)" != "root" ]; then &nbsp;<br>&nbsp;&nbsp; &nbsp;echo "Sorry, you are not root." &nbsp;<br>&nbsp;&nbsp; &nbsp;exit 1 &nbsp;<br>fi [/code] <br>&nbsp;<br>Determine if a directory exists: <br>[code=php]if [ ! -e /usr/local/src ]; then &nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp; mkdir /usr/local/src &nbsp;<br>fi [/code] <br>&nbsp;<br>Pass arguments to the shell script: <br>[code=php]if [ "$1" == "apache" ]; then &nbsp;<br>&nbsp;&nbsp;&nbsp; apache &nbsp;<br>elif [ "$1" == "php" ]; then &nbsp;<br>&nbsp;&nbsp;&nbsp; php &nbsp;<br>else &nbsp;<br>&nbsp;&nbsp;&nbsp; apache &nbsp;<br>&nbsp;&nbsp;&nbsp; php &nbsp;<br>fi<br />
[/code]<br>&nbsp;<br><div class="bbcode_container">
	<div class="bbcode_quote">
		<div class="quote_container">
			<div class="bbcode_quote_container"></div>
			
				john@pluto:~#./myscript php
			
		</div>
	</div>
</div>Here you are passing the script the argument “php.” This would cause the script to only run the php function.</blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/66-my-recent-shell-scripting.html</guid>
		</item>
		<item>
			<title>Do web hosts oversell? </title>
			<link>http://forum.codecall.net/blogs/john/63-do-web-hosts-oversell.html</link>
			<pubDate>Fri, 08 Aug 2008 04:00:00 GMT</pubDate>
			<description><![CDATA[<font size="4">What is overselling?</font><br>Simply put, overselling is when a host offers to more resources to their clients than what they physically have.<br><br>I would venture to say, all hosts oversell. Overselling for the most part is a legitimate practice. Most owners of dedicated servers...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore"><font size="4">What is overselling?</font><br>Simply put, overselling is when a host offers to more resources to their clients than what they physically have.<br><br>I would venture to say, all hosts oversell. Overselling for the most part is a legitimate practice. Most owners of dedicated servers who specialize in web hosting receive, say, an 80 GB hard drive and 8000 GB of transfer from their data center. Lets assume one of the packages a host offers for $5 a month is 1 GB of storage and 10 GB of transfer. Physically, the host can only handle 80 clients. However, since most clients will not use their entire alloted resources, the host can oversell the unused space. Granted, any professional web host should live up to their advertisements. Thus, when the server's physical resources dwindle, the web host will either have to purchase another server from the data center, or upgrade their current server.<br><br>For the past 18 months I have been a client of midPhase. They currently offer 750 GB of storage and 10 TB of transfer. However, since my account is fairly old, I only have 250 GB of storage. Does midPhase oversell? Will they live up to their advertisements?<br><br>Since I have access to a few web servers, my midPhase account is pretty much useless. Therefore I am going to test midPhase to see if they will actually give me 250 GB of storage.<br><br>After a day of pondering how to legally use 250 GB of storage (and not violate midPhase's ToS) I have decided to host 250 GB's of linux ISO's. I will write a follow up blog in a month and let you know if my account has been suspended.<br></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/63-do-web-hosts-oversell.html</guid>
		</item>
		<item>
			<title>Random Numbers</title>
			<link>http://forum.codecall.net/blogs/john/61-random-numbers.html</link>
			<pubDate>Tue, 05 Aug 2008 04:00:00 GMT</pubDate>
			<description><![CDATA[Have you ever been in a conversation where someone blurts out something completely off topic? You say to yourself, “well, that was random.” Chances are, unless that person suffers from a mental disability, that spew of “random” verbiage was not really random.<br><br><font size="4">What is...]]></description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Have you ever been in a conversation where someone blurts out something completely off topic? You say to yourself, “well, that was random.” Chances are, unless that person suffers from a mental disability, that spew of “random” verbiage was not really random.<br><br><font size="4">What is randomness?</font><br>The Oxford English Dictionary defines random as “made, done, or happening without method or conscious decision.” More than likely, your conversation sparked some conscious relocation which resulted in the person sharing their memory at an obscure time. Randomness brings rise to philosophical questions: Are a persons actions random or destiny? Physical questions: Is the motion of an electron around an atoms nucleus random? <br><br>As a programmer, one of the questions I always asked myself is “How random are these 'rand()' functions?” That is the question I will attempt to answer.<br><br>There are two approaches for generating “random” numbers using a computer.<br>&nbsp;&nbsp;&nbsp; -Pseudo-Random Number Generators (PRNG)<br>&nbsp;&nbsp;&nbsp; -True-Random Number Generators (TRNG)<br><br><br><b>True-random Number Generators</b><br>These type of number generators are not shipped as native language construct. This is because they rely on some physical phenomena such as radio active decay, atmospheric noise, or the photoelectric effect, which, software cannot mirror. As the name suggests, the random numbers generated by this method are truly random – or at least as random as we can get. It is this method that is used by casinos, the lottery, advanced cryptography. Because I am no genus, you should refer you to wikipedia for more information regarding TRNG<br><br><b>Pseudo-Random Number Generators</b><br>These are the types of number generators that are seen vastly in computation, and thus programming. A PRNG is nothing more than a predefined algorithm that in most cases seems random. To understand why it seems random lets paint a picture: Each time I roll a die, it has a random result. If I roll the die X amount of times, and record each result, I will end up with a list of randomly generated numbers. You can think of a PRNG as simply traversing this list. Although a PRNG is not actually a list, it is a mathematical formula which can produce varying results. One of the easiest, oldest, and best know formula is the linear congruential generator. Infact this formula is used by Borland C/C++ and glibc which is used by the GCC complier. Complex PRNG's with particular seeds (the initial values for the formula) can produce results that seem just as random as TRNG's.<br><br>I wanted to see how random php's rand() PRNG actually was. To test the function, used the GD library to plot 100,000 random x and y locations points on a canvass. The code I used is below:<br><br>[code=php]header("Content-type: image/png"); <br>$im = @imagecreatetruecolor(500, 500) &nbsp;<br>&nbsp;&nbsp;&nbsp; or die("Cannot Initialize new GD image stream"); <br>&nbsp;<br>$white = imagecolorallocate($im, 255, 255, 255); <br>for ($i = 0; $i &lt; 100000; $i++) { <br>&nbsp;&nbsp;&nbsp; imagesetpixel($im, rand(0,500), rand(0,500), $white); <br>} <br>&nbsp;<br>imagepng($im); <br>imagedestroy($im); <br>&nbsp;<br>?&gt;&nbsp;&nbsp;<br />
[/code]<br>Using the results of an online TRNG, I produced the following visualization:<img src="/images/John//tnrg.png" mce_src="/images/John//tnrg.png" border="0">  <br><br><br>Using the above code on my LAMP server produced seeming random results that closely paralleled that of the TRNG:<br><img src="/images/John//linux-rand.gif" mce_src="/images/John//linux-rand.gif" border="0"> <br><br><br>However, using the same code on my WAMP server, a pattern is clearly visible:<br><img src="/images/John//windows-rand.gif" mce_src="/images/John//windows-rand.gif" border="0"> <br>Therefor it is reccomended you use the mt_rand() function which is based on the <b>mersenne twister </b>algorithm which is faster and produces better results.<b><br></b></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/61-random-numbers.html</guid>
		</item>
		<item>
			<title>Extract Variables</title>
			<link>http://forum.codecall.net/blogs/john/58-extract-variables.html</link>
			<pubDate>Fri, 01 Aug 2008 04:00:00 GMT</pubDate>
			<description>A few months ago, in one of the php articles / books I was reading I stumbled across something called variable variables. Essentially, it lets you create a variable whose name is defined by another variable. I thought to myself “where the hell would this be useful?” A few days after Jordan bloged...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">A few months ago, in one of the php articles / books I was reading I stumbled across something called variable variables. Essentially, it lets you create a variable whose name is defined by another variable. I thought to myself “where the hell would this be useful?” A few days after Jordan bloged about <a href="/component/myblog/PHP-Variable-variables-.html" mce_href="/component/myblog/PHP-Variable-variables-.html">variable variables</a> I actually found a use for them:<br><br>ionManager (a framework and CMS built myself), uses the MVC enterprise pattern. The model querys the database and generates/manipulates any necessary data; the view is responsible for displaying the data. Since the models are classes and views are procedural php files, having the model communicate its data with the view was a minor problem. My solution was to use the controller to pass any data generated by the model to the view.<br><br>For example I am creating a blog module (not do be confused with model) for ionManager. One of the views associated with the blog is a main view – this view simply shows a 300 word excerpt of the last ten blog entries. In the model, the code is rather elementary: [code=php]    function getList ()<br>    {<br>        <br>        $result = $this-&gt;db-&gt;query("SELECT * FROM `jos_content` " <br>               . "WHERE `created_by`='110' " <br>               . "ORDER BY `created` DESC LIMIT 10");<br><br>        while ($blog = $this-&gt;db-&gt;fetch_assoc($result)) {<br>            $time = strtotime($blog['created']);<br>            $blog['created'] = date('F j, Y', $time);<br>            $blog['fulltext'] = $this-&gt;cutText($blog['fulltext'], 300);<br>            $blogs[] = $blog;<br>        }<br>        return $blogs;<br>    }[/code]<br><br>The controller had a loadView function which accepted a single paramater – a path to the view file which it included. Moreover the controller can interact with the model directly since it is responsible for creating its instance. Therefore my controller code looked like this: [code=php]    public function index ()<br>    {<br>        $blogs = $this-&gt;model-&gt;getList();<br>        $this-&gt;loadView("Blog/main_view.php");<br>    }[/code]<br><br>Which is great, however main_view.php knows nothing about $blogs. I therefore chose to pass another parameter to the loadView function which I called $data. My idea was to pass in an assocative array whose index wold become the variable name, and the corrisponding value would become the variables value. Below shows the new controller code after these alterations: [code=php]    public function index ()<br>    {<br>        $blogs = $this-&gt;model-&gt;getList();<br>        $data = array('blogs' =&gt; $blogs);<br>        $this-&gt;loadView("Blog/main_view.php", $data);<br>    }[/code] Now main_view.php can use $blogs.<br><br><br><font size="3"><b>How does this work?</b></font><br>At first I used a fairly swift technique that took advantage of variable variables. For the sake of brevity, I removed several conditionals from the original function. The (now depreciated) function used the following logic: [code=php]function loadView($path, $data)<br>{<br>    foreach($data as $foo =&gt; $bar) {<br>        $$foo = $bar;<br>    }<br>    <br>    include($path);<br>}[/code] $foo was the arrays index and $bar was the indexes value. I was fairly satisfied with this method until yesterday when I was reviewing the seventy two array functions and stumbled across extract. extract() produces the same exact result as my code – without a loop! Therefor my final code looks similar to: [code=php]function loadView($path, $data)<br>{<br>    extract($data);<br>    include($path);<br>}[/code]<br><br>Now I am back to my original question – where the hell are variable variables useful?<br></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/58-extract-variables.html</guid>
		</item>
		<item>
			<title>Object Oriented Design   The Singleton</title>
			<link>http://forum.codecall.net/blogs/john/53-object-oriented-design-singleton.html</link>
			<pubDate>Wed, 16 Jul 2008 04:00:00 GMT</pubDate>
			<description>One of the many annoyances of programming is a variables scope. Generally variables can be declared within the global namespace or the local namespace. When working with methods and classes global variables appear to be a good idea, however overuse of the global namespace can cause variables to...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">One of the many annoyances of programming is a variables scope. Generally variables can be declared within the global namespace or the local namespace. When working with methods and classes global variables appear to be a good idea, however overuse of the global namespace can cause variables to clash, it also undermines encapsulation. Moreover, a class which depends on a global variable is coupled with the project rendering it impossible to reuse (unless the global variable is declared). This will lead to unwanted debugging. Using the <b>singleton</b> design pattern, you can make a class seemingly “global.” Although the code in this article is PHP, the concepts apply across almost every programming language.<br><br><br>Problem:<br>While designing my framework I found access to the database object was required by many classes. Since I only intend on connecting to a single database server, only one object of my database class would ever need to be instantiated. A novice programmer would create a database class which would look like the following: [code=”PHP”]<br>class database {<br>	<br>    private $host;<br>    private $username;<br>    private $password;<br>    private $database;<br><br>    public function __construct(array $config) {<br>        $this-&gt;host = $config['host'];<br>        $this-&gt;username = $config['username'];<br>        $this-&gt;password = $config['password'];<br>        $this-&gt;database = $config['database'];<br>    }<br>    <br>    public function connect() {<br>    	mysql_connect($this-&gt;host, $this-&gt;username, $this-&gt;password);<br>    }<br>    <br>    public function query() {<br>    	//...code here<br>    }<br>    <br>    //...more code here<br>}<br><br>?&gt;[/code] To access any one of the methods you must instantiate the class then call the method:[code=”PHP”]$db = new database($config);<br>$database-&gt;query(“SELECT * FROM users WHERE id = 1”);[/code]<br><br>Obviously the code above (assuming the methods have been implemented)will query the database. In most instances the code above is an acceptable practice. However, this will lead you down one of three roads: you will declare $db in the global namespace, you will instantiate a new database object each time it is needed, or you will end up passing $db to object X so when it creates object Y the database object can be passed. Alas, the learned programmer will choose to use the Singleton design pattern.<br><br>Solution:<br>The solution is to create a class that can only be instantiated itself. Moreover, it should not instantiate more than once instance of itself. We start by making the constructor private, creating a new static method which I will call getInstance(), and a new static variable which I will call $instance. <div class="bbcode_container">
	<div class="bbcode_description">Code:</div>
	<div class="bbcode_code"style="height:36px;"><code><code><span style="color: #000000">
<span style="color: #0000BB"></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">database&nbsp;</span><span style="color: #007700">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$host</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$username</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$database</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;</span><span style="color: #0000BB">$instance</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(array&nbsp;</span><span style="color: #0000BB">$config</span><span style="color: #007700">)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">host&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$config</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'host'</span><span style="color: #007700">&#93;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$config</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'username'</span><span style="color: #007700">&#93;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$config</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'password'</span><span style="color: #007700">&#93;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">database&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$config</span><span style="color: #007700">&#91;</span><span style="color: #DD0000">'database'</span><span style="color: #007700">&#93;;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">getInstance</span><span style="color: #007700">()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">connect</span><span style="color: #007700">()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">host</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">username</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">password</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">query</span><span style="color: #007700">()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//...code&nbsp;here&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//...more&nbsp;code&nbsp;here}</span><span style="color: #0000BB">?&gt;</span>&nbsp;<br /></span>
</code></code></div>
</div> Since the constructor is private, to convey the configuration settings to the database call we will need to pass them to the getInstance() function. Here is where the magic happens. The static variable $instance is used to hold the instance of this class (if one exists). Since we only want one instance to ever be created, we will check to see if $instance has already been set, if it has not, we will set $instance to a new instance of the object then return it. If it has been set, we will simply return the already created object. Confused? Take a look at the code – it should clear things up:<br>[code==”PHP”]<br>class database {<br>	<br>    private $host;<br>    private $username;<br>    private $password;<br>    private $database;<br>    //store the instance of this object if one exists<br>    private static $instance;<br>    <br>    <br>    private function __construct(array $config) {<br>    	$this-&gt;host = $config['host'];<br>        $this-&gt;username = $config['username'];<br>        $this-&gt;password = $config['password'];<br>        $this-&gt;database = $config['database'];<br>    }<br><br>    static function getInstance(array $config = null) {<br>        if( empty( self::$instance ) )<br>        {<br>            //create a new instance of this class<br>            self::$instance = new database($config);<br>        }<br>        //return the instance<br>        return self::$instance;<br>    }<br>    <br>    public function connect() {<br>    	mysql_connect($this-&gt;host, $this-&gt;username, $this-&gt;password);<br>    }<br>    <br>    public function query() {<br>    	//...code here<br>    }<br>    <br>    //...more code here<br>}<br><br>?&gt;[/code] To use this class, we use PHP's scope resolution operator, which allows us to access a static function of a class, without instantiating it. [code=”PHP”]$db = database::getInstance($config_info);<br>$db-&gt;connect();<br>$db-&gt;query(“SELECT * FROM users WHERE id = 1”);[/code] We can now call the database::getInstance($config_info) from any class as many times as we want, without creating a new object. Moreover, we can access the same exact object created in a different location of our application essentially providing “global” access.<br></blockquote>

]]></content:encoded>
			<dc:creator>John</dc:creator>
			<guid isPermaLink="true">http://forum.codecall.net/blogs/john/53-object-oriented-design-singleton.html</guid>
		</item>
	</channel>
</rss>

