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

<channel>
	<title>Bashar's Blog</title>
	<atom:link href="http://bashar.alfallouji.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://bashar.alfallouji.com</link>
	<description>Talks around PHP, Computer Science and Stuffs</description>
	<pubDate>Sat, 03 Oct 2009 05:59:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>COM UQAM 2009 Lipdub - I gotta feeling</title>
		<link>http://bashar.alfallouji.com/2009/10/03/com-uqam-2009-lipdub-i-gotta-feeling/</link>
		<comments>http://bashar.alfallouji.com/2009/10/03/com-uqam-2009-lipdub-i-gotta-feeling/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 05:48:44 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[Fun and stuffs]]></category>

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

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=434</guid>
		<description><![CDATA[This is definitely not PHP or IT related but is worth talking about  The com department of the UQAM (Universite du Quebec a Montreal - Canada) has done an awesome and incredibly popular lipdub video. 
This LipDub has been produced during the integration week of UQAM with 172 communication students. It was made on [...]]]></description>
			<content:encoded><![CDATA[<p>This is definitely not PHP or IT related but is worth talking about <img src='http://bashar.alfallouji.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> The com department of the UQAM (Universite du Quebec a Montreal - Canada) has done an awesome and incredibly popular lipdub video. </p>
<p>This LipDub has been produced during the integration week of UQAM with 172 communication students. It was made on September 10th 2009, the footage lasted approximately 2h15min.</p>
<p>Available on youtube / google video, it is getting more popular every hour / minutes (more than 750k views already) and it is creating kind of a buzz on the web.</p>
<p>They even got an interview on <a href="http://www.cnn.com/video/data/2.0/video/showbiz/2009/09/28/nr.music.video.cnn.html">CNN</a>. </p>
<p>Congratulation to them all.  As an ex-student of UQAM, I specially enjoyed watching it.</p>
<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/-zcOFN_VBVo&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/-zcOFN_VBVo&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/10/03/com-uqam-2009-lipdub-i-gotta-feeling/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Web Performance - Write better code</title>
		<link>http://bashar.alfallouji.com/2009/09/11/write-better-code/</link>
		<comments>http://bashar.alfallouji.com/2009/09/11/write-better-code/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 05:54:25 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=425</guid>
		<description><![CDATA[Once again, Thanks Google for their tech talks.
Here is a video from Steve Souders named &#8220;Life&#8217;s Too Short - Write Fast Code&#8221;. This video talks about how to write efficient code and discuss speed optimizations techniques in Web programing.
Steve works at Google on web performance and open source initiatives. His book, High Performance Web Sites, [...]]]></description>
			<content:encoded><![CDATA[<p>Once again, Thanks Google for their tech talks.</p>
<p>Here is a video from Steve Souders named &#8220;Life&#8217;s Too Short - Write Fast Code&#8221;. This video talks about how to write efficient code and discuss speed optimizations techniques in Web programing.</p>
<blockquote><p>Steve works at Google on web performance and open source initiatives. His book, High Performance Web Sites, explains his best practices for performance; it reached the top of the Amazon computer and Internet bestseller list. His follow-up book, Even Faster Web Sites, provides performance tips for today&#8217;s Web 2.0 applications. Steve is the creator of <span style="text-decoration: underline;"><strong>YSlow</strong></span>, the performance analysis extension to Firebug, with over one million downloads.</p>
<p>(Source : http://stevesouders.com/bio.php)</p></blockquote>
<p>You can also find his latest book named &#8220;Even faster websites&#8221; at <a title="books.google.com" href="http://books.google.ca/books?id=E7p-07kNfXYC&amp;pg=PT255&amp;dq=even+faster+website#v=onepage&amp;q=&amp;f=false" target="_blank">books.google.com</a>.</p>
<p><object width="425" height="344" data="http://www.youtube.com/v/52gL93S3usU&amp;hl=en&amp;fs=1&amp;" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/52gL93S3usU&amp;hl=en&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /></object></p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/09/11/write-better-code/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Git or why should we use decentralized source control ?</title>
		<link>http://bashar.alfallouji.com/2009/09/10/git-or-why-should-we-use-decentralized-source-control/</link>
		<comments>http://bashar.alfallouji.com/2009/09/10/git-or-why-should-we-use-decentralized-source-control/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 07:02:27 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[Source Control]]></category>

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=414</guid>
		<description><![CDATA[Here is a very interesting video from Linus Torvalds on Git. He emphasizes on the importance and added value for companies to use decentralized source control.
You better listen to this video or you just may end up stupid and ugly  

]]></description>
			<content:encoded><![CDATA[<p>Here is a very interesting video from Linus Torvalds on Git. He emphasizes on the importance and added value for companies to use decentralized source control.</p>
<p>You better listen to this video or you just may end up stupid and ugly <img src='http://bashar.alfallouji.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><object width="425" height="344" data="http://www.youtube.com/v/4XpnKHJAok8&amp;hl=en&amp;fs=1&amp;" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/4XpnKHJAok8&amp;hl=en&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /></object></p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/09/10/git-or-why-should-we-use-decentralized-source-control/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Sorry, can&#8217;t do this, because it doesn&#8217;t scale &#8230;!</title>
		<link>http://bashar.alfallouji.com/2009/08/10/sorry-cant-do-this-because-it-doesnt-scale/</link>
		<comments>http://bashar.alfallouji.com/2009/08/10/sorry-cant-do-this-because-it-doesnt-scale/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 06:59:51 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=409</guid>
		<description><![CDATA[If you ever worked in a software company, you most likely heard once someone saying &#8220;Hey, we can&#8217;t do this, because this doesn&#8217;t scale!&#8221;
I believe &#8220;Scalability&#8221; must be on the top 10 list of the &#8220;buzz words&#8221; most used in the software industry.
It is usually considerated as a killing argument in a conversation. &#8220;If something [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever worked in a software company, you most likely heard once someone saying &#8220;Hey, we can&#8217;t do this, because this doesn&#8217;t <span style="text-decoration: underline;">scale</span>!&#8221;</p>
<p>I believe &#8220;Scalability&#8221; must be on the top 10 list of the &#8220;buzz words&#8221; most used in the software industry.</p>
<p>It is usually considerated as a killing argument in a conversation. &#8220;If something doesn&#8217;t scale, then we don&#8217;t want it.&#8221;</p>
<p>One of the things that fascinates me about &#8220;buzz word&#8221; is that most of the time it has so many different definitions. Everyone has a tendency to give it a personal definition. Often, people will be too shy to ask for a clear definition. Therefore, it makes it even harder to be able to really understand what underlies behind it. So, What does Scalability mean ?</p>
<blockquote><p>In telecommunications and software engineering, <strong>scalability</strong> is a <strong>desirable property</strong> of a system, a network, or a process, which indicates its <strong>ability </strong>to either <strong>handle growing amounts</strong> of work in a <strong>graceful</strong> manner or to be <strong>readily enlarged</strong>. (Source: Wikipedia)</p></blockquote>
<p>In my humble opinion, there are a few important keywords (in bold) in this definition that should be kept in mind when thinking about scalability.</p>
<p>I recently watched that video (I love google tech talks) from Cuong Do Cuong given during a quite old conference in Seattle (in 2007). Cuong is an engineering manager at Google (YouTube).  &#8220;He was part of the engineering team that scaled the YouTube software and hardware infrastructure from its infancy to its current scale. Prior to YouTube/Google, he held various software development and management positions at PayPal and Inktomi.&#8221;</p>
<p>Cuong shares with us some his experiences, the different issues related to scalability he and the team faced. I hope you will enjoy it as much as I did.</p>
<p><object width="400" height="400" data="http://video.google.com/googleplayer.swf?docid=-6304964351441328559&amp;hl=en&amp;fs=true" type="application/x-shockwave-flash"><param name="id" value="VideoPlayback" /><param name="src" value="http://video.google.com/googleplayer.swf?docid=-6304964351441328559&amp;hl=en&amp;fs=true" /><param name="allowfullscreen" value="true" /></object></p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/08/10/sorry-cant-do-this-because-it-doesnt-scale/feed/</wfw:commentRss>
		</item>
		<item>
		<title>More is better&#8230; Is that right ?</title>
		<link>http://bashar.alfallouji.com/2009/06/15/more-is-better-is-that-right/</link>
		<comments>http://bashar.alfallouji.com/2009/06/15/more-is-better-is-that-right/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 22:56:58 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[User Experience]]></category>

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=387</guid>
		<description><![CDATA[Recently, I have been working on building a new search engine.  After performing some searches on Google, I ended up watching a tech talk from Marissa Mayer. Marissa is vice President  (Search Products &#38; User Experience) at Google. Just to give you a background here is a short summary of her bio, she has an [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I have been working on building a new search engine.  After performing some searches on Google, I ended up watching a tech talk from Marissa Mayer. <a title="Marissa Mayer" href="http://www.google.com/corporate/execs.html#marissa" target="_blank">Marissa</a> is vice President  (Search Products &amp; User Experience) at Google. Just to give you a background here is a short summary of her bio, she has an impressive experience (found on the google site) :</p>
<blockquote><p>Marissa leads the company&#8217;s product management efforts on search products – web search, images, news, books, products, maps, Google Earth, the Google Toolbar, Google Desktop, Google Health, Google Labs, and more. She joined Google in 1999 as Google&#8217;s first female engineer and led the user interface and web server teams at that time. Her efforts have included designing and developing Google&#8217;s search interface, internationalizing the site to more than 100 languages, defining Google News, Gmail, and Orkut, and launching more than 100 features and products on Google.com</p></blockquote>
<p><br/>I saw her presentation named &#8220;Scaling Google for Every User&#8221; (June 2007) at the Seattle Conference on Scalability. I really liked the part where she talks about how much features should be presented to end users, how to present them and how basically simplcitiy beats complexity. She kinda destroys the myth that &#8220;having more features is always better&#8221;. </p>
<p><br/></p>
<p><object width="425" height="326" data="http://video.google.com/googleplayer.swf?docId=-7039469220993285507&amp;hl=en" type="application/x-shockwave-flash"><param name="src" value="http://video.google.com/googleplayer.swf?docId=-7039469220993285507&amp;hl=en" /></object></p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/06/15/more-is-better-is-that-right/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP Code Comparator - Detect Code Changes</title>
		<link>http://bashar.alfallouji.com/2009/05/29/php-code-comparator-detect-code-changes/</link>
		<comments>http://bashar.alfallouji.com/2009/05/29/php-code-comparator-detect-code-changes/#comments</comments>
		<pubDate>Fri, 29 May 2009 07:33:59 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=351</guid>
		<description><![CDATA[PHP Code Comparator is a command line script allowing to compare different versions of a library, framework or package. It will detect code changes in terms of class existence or method definitions.
If you ever wanted to have a way to quickly identify what classes have been altered or methods have been changed between two different [...]]]></description>
			<content:encoded><![CDATA[<p>PHP Code Comparator is a command line script allowing to compare different versions of a library, framework or package. It will detect code changes in terms of class existence or method definitions.</p>
<p>If you ever wanted to have a way to quickly identify what classes have been altered or methods have been changed between two different versions of the same package, PHP Code Comparator will help you achieve that.</p>
<p>Instead of using a diff tool, you can simply use this tool. It will basically parse a folder recursively, detect any defined classes and extract the various methods and parameters. This will then be performed on the second version of your library. Finally, a summary will be displayed showing the differences that have been detected.</p>
<p>This tool will detect if a class has been removed, if new classes have been added, if methods have been removed or added and also if the signature of a method has changed.</p>
<h3 style="margin-top:25px;margin-bottom:10px;">Where can I download it ?</h3>
<p>You can browse the code and download it from github.com.</p>
<pre class="php"><code><a href="http://github.com/alfallouji/PHP-Code-Compare/">http://github.com/alfallouji/PHP-Code-Compare/</a></code></pre>
<p>You can also directly get the source code from the git repository.</p>
<pre class="php"><code>git clone git://github.com/alfallouji/PHP-Code-Compare.git</code></pre>
<p><strong>You will find more information on the official page : <a href="http://bashar.alfallouji.com/php-code-comparator">PHP Code Comparator</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/05/29/php-code-comparator-detect-code-changes/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Ultimate Coder Battle</title>
		<link>http://bashar.alfallouji.com/2009/05/25/the-ultimate-coder-battle/</link>
		<comments>http://bashar.alfallouji.com/2009/05/25/the-ultimate-coder-battle/#comments</comments>
		<pubDate>Mon, 25 May 2009 08:34:07 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=320</guid>
		<description><![CDATA[I recently heard about a funny contest handled by Microsft. The concept is really fun, and so is their web site (Check the video!).
Here is a short description of the contest.
Microsoft presents the Ultimate Battle: It’s a knock-down, drag-em-out battle between professional and student developers! And it’s your big chance to show the world your [...]]]></description>
			<content:encoded><![CDATA[<p>I recently heard about a funny contest handled by Microsft. The concept is really fun, and so is their web site (Check the video!).</p>
<p>Here is a short description of the contest.</p>
<blockquote><p>Microsoft presents the Ultimate Battle: It’s a knock-down, drag-em-out battle between professional and student developers! And it’s your big chance to show the world your awe-inspiring PHP application – on the Windows-based platform! We’ll select one student and one professional coder for the Ultimate Throwdown – a final bout at the “Make Web, Not War” Conference! The winner can take home up to <span style="FONT-WEIGHT: bold">$10,000</span> in total prize money!Challenge a friend, invite a colleague, ask anyone – get the whole gang in on the Ultimate Throwdown Action.</p></blockquote>
<p> Last day to enter the contest is June 3rd. So hurry !</p>
<p><a href="http://www.phponwindows.ca/ftw/">http://www.phponwindows.ca/ftw/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/05/25/the-ultimate-coder-battle/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Wolfram Alpha - A Computational Knowledge Engine</title>
		<link>http://bashar.alfallouji.com/2009/05/21/wolfram-alpha-a-computational-knowledge-engine/</link>
		<comments>http://bashar.alfallouji.com/2009/05/21/wolfram-alpha-a-computational-knowledge-engine/#comments</comments>
		<pubDate>Thu, 21 May 2009 05:42:11 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[Knowledge]]></category>

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=297</guid>
		<description><![CDATA[WolframAlpha is a computational knowledge engine created by Stephen Wolfram
It basically generates output by doing computations from its own internal knowledge base (composed of many trillions of elements), instead of searching the web and returning links. 
I spent some time testing it and I gotta say it is awesome. So, no it isn&#8217;t gonna replace Google, [...]]]></description>
			<content:encoded><![CDATA[<p>WolframAlpha is a computational knowledge engine created by Stephen Wolfram</p>
<p>It basically generates output by doing computations from its own internal knowledge base (composed of many trillions of elements), instead of searching the web and returning links. </p>
<p>I spent some time testing it and I gotta say it is awesome. So, no it isn&#8217;t gonna replace Google, it is for a different usage.</p>
<p>It is very usefull when you want to find answers to a particular question, for instance, if you look for earthquake in indonesia, you will get this as a result.</p>
<p><img class="alignnone size-full wp-image-298" title="Wolfram Alpha" src="http://bashar.alfallouji.com/wp-content/uploads/2009/05/wolfram_alpha_earthquake.jpg" alt="Wolfram Alpha" width="582" height="799" /></p>
<p> </p>
<p>I really like the way the information is reported / outputed to the end user. If you try with &#8220;barcelona montreal&#8221;, it will even display the result in a table and compare the results.</p>
<p><img class="alignnone size-full wp-image-299" title="Wolfram Alpha" src="http://bashar.alfallouji.com/wp-content/uploads/2009/05/wolfram_alpha_barcelona_montreal.jpg" alt="Wolfram Alpha" width="580" height="1006" /></p>
<p> </p>
<p> </p>
<p>I took some time to read a little bio about Stephen Wolfram and well, it is pretty impressive. Here is a short summary of what we can read about him on Wikipedia.</p>
<p><strong>Who is Stephen Wolfram ?</strong></p>
<blockquote><p>Stephen Wolfram (born  in 1959) is a British physicist, mathematician,author and businessman, known for his work in theoretical particle physics, cosmology, cellular automata, complexity theory, and computer algebra.</p>
<p>Wolfram is considered as a child prodigy, he published an article on particle physics at age 16 and entered Oxford University at age 17. He received his Ph.D. in particle physics from the California Institute of Technology at age 20. He developed the famous software Mathematica.</p>
<p><em>(Source : Wikipedia)</em> </p></blockquote>
<p>Have fun and try that very nice tool, this is defintely going in my bookmarks folder: <a href="http://www.wolframalpha.com">WolframAlpha.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/05/21/wolfram-alpha-a-computational-knowledge-engine/feed/</wfw:commentRss>
		</item>
		<item>
		<title>PHP can be tricky sometimes &#8230;</title>
		<link>http://bashar.alfallouji.com/2009/05/17/php-can-be-tricky-sometimes/</link>
		<comments>http://bashar.alfallouji.com/2009/05/17/php-can-be-tricky-sometimes/#comments</comments>
		<pubDate>Sun, 17 May 2009 08:21:53 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=269</guid>
		<description><![CDATA[Some people just like to write as few line of codes as possible. Well, PHP can be tricky sometimes.. For instance, be carefull when declaring a variable and passing it as a parameter to a function or to a method at the same time. If that parameter is supposed to be passed by reference, you [...]]]></description>
			<content:encoded><![CDATA[<p>Some people just like to write as few line of codes as possible. Well, PHP can be tricky sometimes.. For instance, be carefull when declaring a variable and passing it as a parameter to a function or to a method at the same time. If that parameter is supposed to be passed by reference, you may end up with some unexpected behaviors !</p>
<p>Look at the following code, What do you think will be outputted ?</p>
<pre class="php"><span style="color: #000000"><span style="color: #007700">function </span><span style="color: #0000BB">changeStr</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">)
{
    </span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo_' </span><span style="color: #007700">. </span><span style="color: #0000BB">$str</span><span style="color: #007700">;
    return </span><span style="color: #0000BB">$str</span><span style="color: #007700">;
}

</span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">changeStr</span><span style="color: #007700">(</span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">);

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">);</span></span></pre>
<p>Here is the result.</p>
<pre class="xdebug-var-dump" dir="ltr"><small>string</small> <span style="color: #cc0000;">'bar'</span> <em>(length=3)</em>
<small>string</small> <span style="color: #cc0000;">'foo_bar'</span> <em>(length=7)</em></pre>
<p><br/></p>
<h2 style="text-align: center;"><strong>Surprise ! </strong>$str didn&#8217;t get passed as a reference.<strong></strong></h2>
<p>So basically, $str hasn&#8217;t been affected by the changeStr() function. Let&#8217;s try with an array now..</p>
<pre class="php"><span style="color: #007700">function </span><span style="color: #0000BB">changeArr</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">)
{
    </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">] = </span><span style="color: #0000BB">10</span><span style="color: #007700">;
    return </span><span style="color: #0000BB">$arr</span><span style="color: #007700">;
}

</span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">changeArr</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr </span><span style="color: #007700">= array(</span><span style="color: #007700">));

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">]);
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">]);
</span></pre>
<p>The result is&#8230;</p>
<pre class="xdebug-var-dump" dir="ltr"><span style="color: #3465a4;">null</span>
<small>int</small> <span style="color: #4e9a06;">10</span></pre>
<p><strong>Well, same behavior as earlier. $arr wasn&#8217;t altered by the changeArr() function !</strong></p>
<p>What about objects, does it do the same thing ?</p>
<pre class="php"><span style="color: #007700">function </span><span style="color: #0000BB">changeObj</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)
{
    </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id </span><span style="color: #007700">= </span><span style="color: #0000BB">10</span><span style="color: #007700">;
    return </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;
}

</span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">changeObj</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">StdClass</span><span style="color: #007700">());

</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">);
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">);</span></pre>
<p>Here is the result.</p>
<pre class="xdebug-var-dump" dir="ltr"><small>int</small> <span style="color: #4e9a06;">10</span>
<small>int</small> <span style="color: #4e9a06;">10</span></pre>
<p><strong>So, it doesn&#8217;t work for a string or an array, but it work with objects ! </strong></p>
<p>Well, I highly suggest any developer not to declare a variable and pass it to a method or a function at the same time.  Just write the additional line of code, it is gonna reduce the risk of error and actually might make your core more readable !</p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/05/17/php-can-be-tricky-sometimes/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Singletons aren&#8217;t always that single&#8230;</title>
		<link>http://bashar.alfallouji.com/2009/05/17/typical-singletons-arent-always-that-single/</link>
		<comments>http://bashar.alfallouji.com/2009/05/17/typical-singletons-arent-always-that-single/#comments</comments>
		<pubDate>Sun, 17 May 2009 07:57:14 +0000</pubDate>
		<dc:creator>Bashar</dc:creator>
		
		<category><![CDATA[Design Pattern]]></category>

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

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

		<guid isPermaLink="false">http://bashar.alfallouji.com/?p=237</guid>
		<description><![CDATA[ ]]></description>
			<content:encoded><![CDATA[<p>I have decided to write a serie of posts around the Singleton design pattern. This first one will discuss the uniqueness aspect of a Singleton.</p>
<p><strong>What is a Singleton again ?</strong></p>
<blockquote><p>In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (Wikipedia)</p></blockquote>
<p>Here is a typical implementation of a Singleton class in PHP. You will see this kind of code in many different existing frameworks.</p>
<pre class="php"><span style="color: #000000"><span style="color: #FF8000">/**
 * A typical implementation of a Singleton
 */
</span><span style="color: #007700">class </span><span style="color: #0000BB">aSingleton
</span><span style="color: #007700">{
    private static </span><span style="color: #0000BB">$_instance </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;

    public </span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;

    private function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()
    {
    }

    public static function </span><span style="color: #0000BB">getInstance</span><span style="color: #007700">()
    {
        if(</span><span style="color: #0000BB">null </span><span style="color: #007700">=== </span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">$_instance</span><span style="color: #007700">)
        {
            </span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">$_instance </span><span style="color: #007700">= new </span><span style="color: #0000BB">aSingleton</span><span style="color: #007700">();
        }

        return </span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">$_instance</span><span style="color: #007700">;
    }
}</span>
</span></pre>
<p><strong>So, is there anything wrong with that code ?</strong></p>
<p>One of the things that I really dislike about this implementation (there are actually many others things I dislike about Singleton, but I will discuss them in others posts), is the fact you cannot (actually this isnt necessarily true as we will see later) have two different instances of a Singleton. I dont understand why a class should enforce a such rule / constraint. Why would someone want to have a such constraint ? What good would it make ?</p>
<p>Some may argue that sometimes in an application, you want to keep using the same instance of an object. Usually, they will say that objects like a logger,  a configuration or a database handler are good candidates for a such use. Well, that&#8217;s fine with me, but why do I have to use a Singleton for that ? Is this really the best solution to do that ?</p>
<p>Let&#8217;s use a parrallel. Imagine, we need to always use the same hammer to do a job for some good reason.  So, we decide you are going to enforce that requirement, by destroying all the others existing hammers and any shop / factory selling or producing hammers. That way, when we use a hammer, we are sure it is the same one (it is the only hammer that exist in the whole universe and it is impossible that for any other hammer to get built).</p>
<p>Doesn&#8217;t that sound crazy and  extreme ? I mean, why wouldn&#8217;t we just mark that hammer (write a name on it or something) and just store it in a specific place, so we know where to get it and we can ensure that we are using the same hammer. Also, if one day we do need to have another hammer, well we would be able to&#8230;</p>
<p>There is also a problem with most existing implementations of a Singleton.  The design pattern is supposed to enforce the number of different instance of a particular class, right ? Well, there are ways to bypass that and end up with different instances of a Singleton.</p>
<p><strong>In PHP, we could use the clone construct.</strong></p>
<pre class="php"><span style="color: #000000"><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">aSingleton</span><span style="color: #007700">::</span><span style="color: #0000BB">getInstance</span><span style="color: #007700">();
</span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #0000BB">10</span><span style="color: #007700">;

</span><span style="color: #0000BB">$b </span><span style="color: #007700">= clone </span><span style="color: #0000BB">$a</span><span style="color: #007700">;
</span><span style="color: #0000BB">$b</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #0000BB">20</span><span style="color: #007700">;

echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">. </span><span style="color: #007700"><span style="color: #dd0000;">PHP_EOL</span></span><span style="color: #007700">; </span><span style="color: #FF8000">// Display 10
</span><span style="color: #007700">echo </span><span style="color: #0000BB">$b</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">. </span><span style="color: #007700"><span style="color: #dd0000;">PHP_EOL</span></span><span style="color: #007700">; </span><span style="color: #FF8000">// Display 20.
                        // Damn this isnt anymore my Singleton!</span>
</span></pre>
<p>To prevent that, we would need to implement the __clone() method (define it as final and throw some kind of exception whenever called). This would  solve the issue with the clone construct.</p>
<p>Well, that still won&#8217;t completely prevent someone from being able to have two different instances of our Singleton. <strong>Someone could still use the serialization.</strong> Look at this code.</p>
<pre class="php"><span style="color: #000000"><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">aSingleton</span><span style="color: #007700">::</span><span style="color: #0000BB">getInstance</span><span style="color: #007700">();
</span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #0000BB">10</span><span style="color: #007700">;
</span><span style="color: #007700">
</span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);
</span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);
</span><span style="color: #0000BB">$b</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">= </span><span style="color: #0000BB">20</span><span style="color: #007700">;

echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">. </span><span style="color: #007700"><span style="color: #dd0000;">PHP_EOL</span></span><span style="color: #007700">; </span><span style="color: #FF8000">// Display 10
</span><span style="color: #007700">echo </span><span style="color: #0000BB">$b</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo </span><span style="color: #007700">. PHP_EOL</span><span style="color: #DD0000">; </span><span style="color: #FF8000">// Display 20.
                        // Damn this isnt anymore my Singleton!</span>
</span></pre>
<p>Again, to prevent that, we could implement the __wakeup() method (also declare it as final and throw some kind of exception).</p>
<p>So, we just added a new constraint on the class : <strong>We cannot unserialize it anymore via the unserialize function. </strong>If we cannot unserialize, then we made serialization pretty much useless&#8230;</p>
<p>That&#8217;s the end of this first post on Singleton. I am hoping that after reading this, you already feel (if you are a Singleton adept) that there might be something wrong or bad using this design pattern.</p>
<p>My next post will discuss other aspects that I really dislike about Singleton, such as how to unit test Singleton, why Singleton is a global state and why global states are bad, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://bashar.alfallouji.com/2009/05/17/typical-singletons-arent-always-that-single/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
