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

<channel>
	<title>Vance Lucas &#187; Programming</title>
	<atom:link href="http://www.vancelucas.com/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.vancelucas.com</link>
	<description>Web Entrepreneur and Freelance PHP/Javascript Developer</description>
	<lastBuildDate>Thu, 17 Nov 2011 15:30:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Count the Number of Object keys/properties in Node.js</title>
		<link>http://www.vancelucas.com/blog/count-the-number-of-object-keysproperties-in-node-js/</link>
		<comments>http://www.vancelucas.com/blog/count-the-number-of-object-keysproperties-in-node-js/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 17:26:25 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=768</guid>
		<description><![CDATA[When using the excellent formidable library to handle file uploads, I needed to get a count of the number of files unloaded in a multi-part form. Javascript arrays have a .length property that you can use, but objects do not. I instinctively typed: 1 files.length Which returned undefined. So if there is no length property present, an [...]]]></description>
			<content:encoded><![CDATA[<p>When using the excellent <a href="https://github.com/felixge/node-formidable">formidable</a> library to handle file uploads, I needed to get a count of the number of files unloaded in a multi-part form. Javascript arrays have a <code>.length</code> property that you can use, but objects do not. I instinctively typed:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="js" style="font-family:monospace;">files.length</pre></td></tr></table></div>

<p>Which returned <code>undefined</code>. So if there is no length property present, an easy way to count the number of keys or properties of an object in ES5-compliant javascript environments like <a href="http://nodejs.org">Node.js</a> is to use the Object prototype directly:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="js" style="font-family:monospace;">Object.keys(files).length</pre></td></tr></table></div>

<p>A little more typing, but it is fast, efficient, and most importantly: already built-in.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/count-the-number-of-object-keysproperties-in-node-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protected vs Private Scope: Arrogance, Fear, and Handcuffs</title>
		<link>http://www.vancelucas.com/blog/protected-vs-private-scope-arrogance-fear-and-handcuffs/</link>
		<comments>http://www.vancelucas.com/blog/protected-vs-private-scope-arrogance-fear-and-handcuffs/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 14:19:48 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[method scope]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[private vs protected]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=721</guid>
		<description><![CDATA[The age old private vs protected debate has been re-ignited in the PHP community recently following the decision of Doctrine2 and Symfony2 to make all class methods private until there is a very clear and proven reason to change them to protected or public. The intention is a good one &#8211; to ensure they are [...]]]></description>
			<content:encoded><![CDATA[<p>The age old private vs protected debate has been re-ignited in the PHP community recently following the decision of <a href="http://groups.google.com/group/symfony-devs/browse_thread/thread/58a0d015622c13cb">Doctrine2 and Symfony2 to make all class methods private</a> until there is a very clear and proven reason to change them to protected or public. The intention is a good one &#8211; to ensure they are providing a clear and stable API through intentional and known extension points that they can better test and support. Fabien (the creator of Symfony) points to this benefit in an <a href="http://fabien.potencier.org/article/47/pragmatism-over-theory-protected-vs-private">article of his own</a> explaining the thinking behind the decision. The primary started point of Fabien&#8217;s article and the driving thought behind this whole change and philosophy is (emphasis his):</p>
<blockquote><p><strong>Having a few well defined extension points force the developer to extend your library the right way instead of hacking your code.</strong></p></blockquote>
<p>The problem is that this kind of thinking is a slippery slope that kills the spirit of programming. It alienates the more pragmatic developers within communities. Telling other developers that you are going to force them to work with your code in some pre-determined &#8220;right way&#8221; is an incredibly arrogant statement to make.<span id="more-721"></span> Instead of letting the codebase grow organically and be modified or extended at will, you handcuff developers and send them a different message. Allow me to re-phrase it using my own words:</p>
<blockquote><p>I know the right way to use this code and I have thought of all the possible use cases. If you don&#8217;t agree, then you have to prove me wrong and wait until I either agree or figure out a better right way for you.</p></blockquote>
<p>This is the real message developers hear when internal code is private scope or marked with the final keyword &#8211; a message that they have arbitrarily limited power and ability to make changes where they need to. But it doesn&#8217;t stop there.</p>
<p>When you take the position that all methods should be private unless proven otherwise, you plant a seed of fear in the developers that use your code. Fear that whenever they may have to veer off the beaten path to meet a project requirement they won&#8217;t be able to. Fear that they might miss deadlines waiting for the &#8220;right way&#8221;  to enable the functionality they need. Fear that they might have to modify or hack the actual file or class they need to change the behavior of to achieve the functionality they want within the project deadline because they are not able to extend it.</p>
<p>Using private scope removes the fun from programming and kills the hacking spirit by creating fear through the knowledge that you will not have the ability to make changes anywhere in the codebase you need to when you need to. You are no longer in control of your own code. You were forced to submit to a <a href="http://blog.astrumfutura.com/2011/03/private-vs-protected-methods-the-debate-that-never-ends/">nanny state mentality</a> by sacrificing your freedoms to modify the code for the promise of future stability and safety. That&#8217;s not a fun place to be when you&#8217;re on the receiving end, and it&#8217;s not a policy I would ever consider enforcing with my own code. I sincerely hope this line of thinking does not proliferate within the PHP community and infect other projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/protected-vs-private-scope-arrogance-fear-and-handcuffs/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Zero to App in Two Weeks with Titanium</title>
		<link>http://www.vancelucas.com/blog/zero-to-app-in-two-weeks-with-titanium/</link>
		<comments>http://www.vancelucas.com/blog/zero-to-app-in-two-weeks-with-titanium/#comments</comments>
		<pubDate>Mon, 27 Dec 2010 17:05:00 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Titanium]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[appcelerator]]></category>
		<category><![CDATA[autoridge]]></category>
		<category><![CDATA[cross-platform]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[mobile app]]></category>
		<category><![CDATA[titanium]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=638</guid>
		<description><![CDATA[Like any web developer who has been sitting on the sidelines watching this mobile explosion happen in front of my eyes, I was eager to find a way to jump in. Up until about a month ago, I was still evaluating various different mobile development platforms &#8211; Titanium, PhoneGap, and Rhomobile, trying to decide which one [...]]]></description>
			<content:encoded><![CDATA[<p>Like any web developer who has been sitting on the sidelines watching this <a href="http://www.readwriteweb.com/archives/admob_reports_on_mobile_webs_explosive_growth.php">mobile explosion</a> happen in front of my eyes, I was eager to find a way to jump in. Up until about a month ago, I was still evaluating various different mobile development platforms &#8211; <a href="http://www.appcelerator.com/">Titanium</a>, <a href="http://www.phonegap.com/">PhoneGap</a>, and <a href="http://rhomobile.com/">Rhomobile</a>, trying to decide which one I wanted to use to make my first mobile app. My only selection criteria was that the platform would have to be able to produce both iPhone and Android apps with the same code, because I wanted to be able to develop and release both an Android and iPhone apps without having to learn two completely different programming languages and development environments, and without having to do everything twice.<span id="more-638"></span></p>
<p>After playing with all three platforms, I settled on <a href="http://www.appcelerator.com/">Appcelerator Titanium</a> as my tool of choice for a few main reasons:</p>
<ol>
<li>It produces native code, and is not just a simple html/css &#8220;web view&#8221; wrapper</li>
<li>It is javascript-based, which means I don&#8217;t have to learn a new language or try wrap my head around entirely new design paradigms</li>
<li>The application code is UI and event focused, which seems to make the most sense for native apps (&#8220;draw this button here&#8221;, &#8220;do this on click&#8221;, etc)</li>
</ol>
<h3>Starting Out</h3>
<p>Starting your first project with Titanium is a bit rough. There are a ton of steps involved in setting up the Android development environment, and you&#8217;re pretty much on your own on how to do this from Appcelerator&#8217;s standpoint &#8211; the Android Developer website has <a href="http://developer.android.com/sdk/installing.html">instructions</a> on how to do this, and it&#8217;s pretty much expected that you have gotten at least that far before beginning with Titanium. Xcode is easy to setup in comparison, but it&#8217;s a painfully long download at over 3GB.</p>
<p>Note that you&#8217;re going to have to have these environments setup regardless of the platform you choose, so it&#8217;s really not any discredit to the Titanium platform that they don&#8217;t have guides for this, although I think it would be wise for them to make some for the new-to-mobile-development people like me.</p>
<p>From a platform and code standpoint, the main thing that makes starting an Appcelerator project a bit tough is that there is little to no clear direction &#8211; the development process is literally wide open for you to do anything you want to do. This is good because it provides flexibility, but harder for newcomers because there is no defined answer for basic questions like &#8220;how do I organize my files?&#8221; or &#8220;where do I put configuration?&#8221;. It&#8217;s like a programming language without a framework. The answer is almost always &#8220;wherever you want to&#8221;. You try a few things, find what works for you, and then copy that structure and base skeleton files for all your future projects. What helped me most here is looking at a few existing open-source projects (like <a href="https://github.com/givp/Titanium-Mobile-Reference-Directory-App">this one</a>) and figuring out what they did.</p>
<h3>The Development Experience</h3>
<p>After you settle on an organization structure and get your app actually building and running an an emulator, the rest of the process is pretty easy and fairly smooth. The code syntax is calls to custom APIs with basic Javascript and JSON notation for options/settings, so most web developers will feel right at home, especially if they have ever had experience with javascript, jQuery or DOM scripting.</p>
<p>If you&#8217;re approaching Titanium from an MVC perspective, working with Titanium will be like spending most of your time in the &#8220;View&#8221; layer, and there really is no controller. Titanium and most native applications tend to be event-driven, and it works well with Javascript syntax and the way they have it setup. You&#8217;re essentially making direct calls within a script file, telling Z window or button to render at X,Y with given display properties.</p>
<p>Want a new blank window to open?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myWin <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createWindow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'New Window'</span><span style="color: #339933;">,</span>
    url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'mydir/new_window.js'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myWin.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Want a button that scrolls an image slideshow to the previous image on click?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Create button object</span>
<span style="color: #003366; font-weight: bold;">var</span> btnLeft <span style="color: #339933;">=</span> Ti.<span style="color: #660066;">UI</span>.<span style="color: #660066;">createButton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        left<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
        width<span style="color: #339933;">:</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span>
        height<span style="color: #339933;">:</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">,</span>
        backgroundColor<span style="color: #339933;">:</span> <span style="color: #3366CC;">'#333'</span><span style="color: #339933;">,</span>
        image<span style="color: #339933;">:</span><span style="color: #3366CC;">'../images/icon_arrow_left.png'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Add button to current window</span>
win.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span>btnLeft<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Scroll to previous image on click/touch</span>
btnLeft.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>activeViewIndex <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        activeViewIndex<span style="color: #339933;">--;</span>
        scrollView.<span style="color: #660066;">scrollToView</span><span style="color: #009900;">&#40;</span>activeViewIndex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Most other basic operations are similarly easy and trivial to accomplish with Titanium. The code is fairly straight forward, is easy to understand, and does what it says. Furthermore, there really is no setup or boilerplate code that you have to write at all, which means once you get the hang of Titanium, you can create apps pretty quickly.</p>
<h3>Cross-Platform Mobile Development</h3>
<p>If your wish was to find a development platform that smooths out all the idiosyncrasies and differences between all the different mobile platforms and provide you with a single way to achieve the same things on all platforms, Titanium may not be for you.</p>
<p>Most of the provided Titanium UI APIs will work cross-platform, but they are tied to the native platform implementations of those features, so they are not guaranteed to look, work, or even act the same cross-platform. This leads to a lot of &#8220;try it and see&#8221; type development that consists of switching back and forth between code, re-building the project, and seeing how it works in the emulator on on your device. Although this type of blind development can be pretty frustrating at times, this approach makes sense and is actually a benefit to me as a sole developer. It means I don&#8217;t have to spend any time at all on styling, UI, images, or look and feel of the app &#8211; I just get the native look and feel out of the box. This is also good from an end-user standpoint, because users will have the same familiar look and feel with your app as they have with most others they use. Contrast this with development tools like PhoneGap where you use HTML/CSS, and you&#8217;re stuck creating your own whole look and feel with CSS (or using something like <a href="http://jquerymobile.com/">jQuery Mobile</a>) for each app, and having the same look for all mobile platforms. That could be beneficial in some cases, but to an end user, suddenly having an iOS look on an Android app you launch is a bit odd when all the other apps you use have the Android UI.</p>
<h3>The Difficult Pieces</h3>
<p>The most difficult thing to deal with in Titanium is when a particular feature I want to use is well-supported on the iPhone, but causes immediate crashes in Android, with no warning from the compiler that the feature I am trying to use is not supported on the Android platform. This has lead to quite a bit more time-consuming &#8220;try it and see if it works&#8221; type development than I had originally bargained for &#8212; and the Android emulator isn&#8217;t exactly quick to re-compile and re-launch an app after making changes.</p>
<p>These developer headaches have not gone unheard through &#8211; Appcelerator just released <a href="http://developer.appcelerator.com/blog/2010/12/titanium-mobile-1-5-0-is-ga-today.html">Titanium Mobile SDK 1.5.0</a>, which focused on full Android support. I upgraded and re-compiled a few of my apps to the new SDK, and most of the issues have been smoothed out. I have even been able to use code I had previously removed and have it work flawlessly now, so kudos to the Appcelerator Android team on their hard work. They are obviously committed to the platform and making it the best they can for developers that work on it.</p>
<h3>The Finished Product</h3>
<p>The app I made ended up being fairly simple &#8211; It&#8217;s an automotive app for <a href="http://autoridge.com">Autoridge</a> and the goal is to view vehicle photos by vehicle year/make/model, and view part numbers by category. It doubles as a simplistic vehicle database. The goal is to provide select part information for common replacement parts for your vehicle when you need to buy a part, but the part catalog is missing or your vehicle is not represented.</p>
<p>From a technical standpoint it&#8217;s pretty simple as well &#8211; It communicates with a JSON REST API and just displays the results on-screen in a native table view, with little special styling and no animations. The photo gallery was the most difficult and time-consuming piece of the whole app, and the 1.5 SDK release provided <em>much</em> better support for it on Android (even built-in swipe gestures for photo transitions!).</p>
<p><a href="http://autoridge.com/mobile"><img class="alignnone" title="Autoridge Lite for the iPhone" src="http://autoridge.com/assets/images/mobileapp/iphone/ss1-320.png" alt="Autoridge Lite for the iPhone" width="280" /></a> <a href="http://autoridge.com/mobile"><img class="alignnone" title="Autoridge Lite for the Android" src="http://autoridge.com/assets/images/mobileapp/android/ss1-320.png" alt="Autoridge Lite for the Android" width="280" /></a></p>
<h3>Summary</h3>
<p>Overall, I have grown pretty fond of Appcelerator&#8217;s Titanium platform. It allows me to create iOS and Android apps quickly and without having to learn any new programming languages &#8211; a feat I had previously thought was out of my reach. The platform is in a noticeably beta-like status (currently at Mobile SDK v1.5.1 at the time of writing) with frequent updates and even some truly frustrating compatibility-breaking changes and inexplicably missing functionality. If you&#8217;re willing to stick with Titanium through the rough spots, you will be able to outpace native developers both in terms of line-for-line productivity and to-market speed for <del datetime="2010-12-27T18:16:47+00:00">simple</del> most apps, and will be able to compile them for both iOS and Android platforms as an added bonus.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/zero-to-app-in-two-weeks-with-titanium/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Practical Uses for PHP 5.3 Closures</title>
		<link>http://www.vancelucas.com/blog/practical-uses-for-php-5-3-closures/</link>
		<comments>http://www.vancelucas.com/blog/practical-uses-for-php-5-3-closures/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 15:19:47 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[late static binding]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=603</guid>
		<description><![CDATA[Closures are a new language-level feature that has been added to php 5.3, along with namespaces, late static binding, and a slew of other new features, patches, and updates. If you&#8217;re like me, you might be wondering what the practical uses for these new features are before you can rightly justify diving in and using them [...]]]></description>
			<content:encoded><![CDATA[<p>Closures are a new language-level feature that has been added to php 5.3, along with namespaces, late static binding, and a slew of other new features, patches, and updates. If you&#8217;re like me, you might be wondering what the practical uses for these new features are before you can rightly justify diving in and using them in new or existing projects. I experimented a lot with closures and possible uses over the past few weeks, and came up with some compelling reasons to start using them.<br />
<span id="more-603"></span></p>
<h3>Templating</h3>
<p>Templating is perhaps the most compelling and creative use of closures that I have found, yet I haven&#8217;t seen any people or projects using closures this way. Closures are an excellent way to allow custom formatting for re-useable template widgets, like forms, datagrids, and lists. Re-useable forms and datagrids are pretty common in frameworks, because no one likes the monotony of creating a bunch of HTML forms or tables. They all seem to work great, until you have to change a few things in how they output the input fields or format cell data &#8211; then they can get a bit hairy and ugly to work with, and often require inheritance or use some obscure template syntax of their own with tokens that get string replaced and weak-to-no support for basic control structures like loops and if statements. Fabien Potencier published a <a href="http://groups.google.com/group/symfony-devs/browse_thread/thread/fb5fb9732cf5fc1e?pli=1">proof-of-concept for individual field templates</a> in Symfony2. This approach, while much more flexible than string-replaced field tokens, adds quite a bit of overhead because you now have to render one template per input field to produce a complete form. The same end result can now be easily achieved with the use of closures:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Alloy\View\Generic\Datagrid<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'datagrid'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$table</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">posts</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">column</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Post'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">,</span> <span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">column</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Status'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">,</span> <span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">status</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">-&gt;</span><span style="color: #004000;">column</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Date Published'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">,</span> <span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">date_published</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dateFormat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">date_published</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'(Unpublished)'</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$table</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>In this example, our generic &#8220;Datagrid&#8221; view accepts &#8220;$this->posts&#8221; as its data source input, and loops over it with a foreach internally. It then implicitly passes two arguments to any closure given that defines column output &#8211; &#8220;$item&#8221; &#8211; the current item in the posts collection we provided, and &#8220;$view&#8221; &#8211; the view object itself so helpers and other typical functions can be accessed.</p>
<p>The added benefits for using closures in this context are enormous. There is no additional (and limiting) pseudo-syntax to parse, and no additional item templates to render. The custom content is overridden in-line right where you want it to be (instead of being one-step removed in a separate template), and you can still use the native PHP code you know and love. The provided example is from <a href="http://cont-xt.com">Cont-xt CMS</a> built using my own <a href="http://alloyframework.org">Alloy HMVC Framework</a>.</p>
<p>Imagine how powerful and simple something like <a href="http://framework.zend.com/manual/en/zend.navigation.html">Zend_Navigation</a> and the <a href="http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation">resulting navigation view helpers</a> would become with support for closure templates. You would no longer have to rely on a deep hierarchy of nested configuration arrays, custom partial templates, or complex object inter-dependencies to generate page links with your own custom code.</p>
<h3>Dynamic Code Extension</h3>
<p>Dynamically extending a class and modifying the behavior of a code block with the use of closures is the foundation of a new php 5.3 framework called <a href="http://lithify.me/">Lithium</a>. Lithium&#8217;s specific approach uses closure chains inside function bodies to allow end-users to modify core functionality without modifying the core files themselves or having to use inheritance.</p>
<p>A more extreme example uses closures to enable dynamic run-time object creation. The idea is in an article titled <a href="http://dhotson.tumblr.com/post/1167021666/php-object-oriented-programming-reinvented">PHP Object Oriented Programming Reinvented</a>. The entire object and all its functionality is created with closures. The article is a good demonstration of the power of closures, but has little practical use.</p>
<p>One of the more practical (and perhaps controversial) uses I came up with is replacing string configuration values with closures if the return values needs to be dynamic. Here&#8217;s an example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Set in base config file</span>
<span style="color: #000088;">$cfg</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'path_uploads'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cfg</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;/uploads/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cfg</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Set dynamically by database value from logged-in user later in your application</span>
<span style="color: #000088;">$cfg</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'user_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">74</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This way, the &#8216;path_uploads&#8217; config value will not have to be changed when the &#8216;user_id&#8217; config value is. The closure will cause the config value to always be evaluated when called, and will always use the current &#8216;user_id&#8217; value, so it will never be &#8220;out of date&#8221;. This, of course, requires you to have a standard way to retrieve config values from named keys, because closures can&#8217;t just be automatically converted to a string by themselves, and do have to be called a different way (like functions).</p>
<p>This is the only use that comes with a warning: beware how far down this road you travel. Dynamic code extension provides a lot of flexibility, but comes with the downside of code obfuscation and performance penalties. You are essentially adding class methods and modifying code behavior in places other than the code itself, which can lead to confusion when trying to trace code execution. Stack traces and development toolbars can help here, and using an opcode cache like APC can minimize the performance penalty.</p>
<h3>Delayed Execution</h3>
<p>Closures are a great tool to use anywhere you need to delay PHP code execution until a later time. A great example of this use is for a job queue system with multiple workers. Using closures, you can execute any arbitrary PHP code you need to without conforming to a specific structure &#8212; there is no need to create new classes or functions with set names determined by naming conventions, and no need to put &#8220;worker classes&#8221; in any specific place in the filesystem. Your worker task can be passed around and executed in parallel at will by the job queue.</p>
<p>Example: <a href="http://github.com/kore/njq">PHP Native Job Queue</a>.</p>
<h3>Caching</h3>
<p>Using closures for caching is a bit of an extension of the delayed execution idea. Consider a cached block of code inside a view template using a closure:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #666666; font-style: italic;">// Expensive external HTTP request</span>
    <span style="color: #000088;">$blogRss</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">helper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'feedReader'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://github.com/zendframework/zf2/commits/master.atom'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$widget</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \MyApp\View\Generic\FeedList<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'feedlist'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$widget</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">feedContent</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$blogRss</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'atom'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$widget</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'github_zf2_commits'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Using a closure here offers two advantages over the typical &#8216;beginCache&#8217;, &#8216;&#8230;&#8217;, &#8216;endCache&#8217; type calls that are more prevalent in pre-5.3 code. The first benefit is that through the use of a closure, our code block is contained in a variable before it is even executed. This allows us to use a cache backend like Memcache without having to use output buffering at all, saving us a tiny bit of overhead. The second big benefit is that executing our code chunk is entirely optional, without manually checking the cache first. This second benefit could also allow you to disable rendering of certain cached content completely if you need it, without any code changes in your view templates. Heavy-load or traffic spike scenarios come to mind as possible candidates for this functionality. Imagine being able to make a small configuration change to skip certain code blocks like $cache->disable(&#8216;github_zf2_commits&#8217;) to serve a &#8220;lite&#8221; version of your site when you need all the server resources you can get.</p>
<h3>Convenience</h3>
<p>This may sound like a bit of a cop-out reason, but using closures in some cases really does make things a lot easier, and truly better. Consider the case where you have to use a one-off function to apply some sort of custom filtering logic to an array or collection of objects. With closures, you can just specify the logic in-place right where you need it instead of having to create a new function for the job in a separate location and having to figure out where to put it in your application, and then how to call it. It produces the same result in fewer lines of code with no side-effects (like having to make a global function).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Collection of entities</span>
<span style="color: #000088;">$objCollection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> Entity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Entity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> EntityRelation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> OtherRandomJunk<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get only Entity objects</span>
<span style="color: #000088;">$entitiesOnly</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_filter</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$objCollection</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span> instanceof Entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In the above example, it may not make sense to create a separate named or global function for this collection filtering, because it may only ever be used in one place. If this is the case, it makes sense to use a closure, and makes the code more concise and readable because everything is all in one place.</p>
<h3>Summary</h3>
<p>Closures are a game-changer for PHP all by themselves. Throw namespaces and late static binding in the mix as well, and PHP 5.3 reaches a level of power and dynamic flexibility that PHP has never been able to reach before. If you have been sitting on the sidelines, stuck in PHP 5.2 land &#8211; you desperately need to upgrade. This is the future of PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/practical-uses-for-php-5-3-closures/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>MongoDB Gotchas</title>
		<link>http://www.vancelucas.com/blog/mongodb-gotchas/</link>
		<comments>http://www.vancelucas.com/blog/mongodb-gotchas/#comments</comments>
		<pubDate>Wed, 07 Jul 2010 17:37:10 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[mongodb]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=562</guid>
		<description><![CDATA[Most developers are coming from a background with relational database-specific experience, and then trying out some new NoSQL databases like MongoDB. Here are some &#8220;gotchas&#8221; I ran into while using MongoDB with my MySQL hat still on. Queries are case-sensitive Fields and queries in MongoDB are case-sensitive: 1 2 3 var test1 = db.test.find&#40;&#123;'tags': 'jquery'&#125;&#41;.count&#40;&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p>Most developers are coming from a background with relational database-specific experience, and then trying out some new NoSQL databases like <a href="http://mongodb.org">MongoDB</a>. Here are some &#8220;gotchas&#8221; I ran into while using MongoDB with my MySQL hat still on.<span id="more-562"></span></p>
<h3>Queries are case-sensitive</h3>
<p>Fields and queries in MongoDB are case-sensitive:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> test1 <span style="color: #339933;">=</span> db.<span style="color: #660066;">test</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'tags'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'jquery'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> test2 <span style="color: #339933;">=</span> db.<span style="color: #660066;">test</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'tags'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'jQuery'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
test1 <span style="color: #339933;">==</span> test2<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Output is false - they do not query for the same information</span></pre></td></tr></table></div>

<p>This can cause some headaches if you don&#8217;t normalize user input ahead of time. Imagine you already have several posts tagged with &#8216;NoSQL&#8217; and users enter &#8216;nosql&#8217; in a tag search box. If you don&#8217;t normalize how the data is stored internally (like lowercase all tags), your user will see a much smaller set of posts than they are expecting to see. This is something you don&#8217;t have to worry or even think about with MySQL and most other relational databases.</p>
<p>If you can&#8217;t normalize the stored data, but still want a case-insensitive query, then you must perform a slower <a href="http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions">regular expression query</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">db.<span style="color: #660066;">test</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'tags'</span><span style="color: #339933;">:</span> <span style="color: #339933;">/</span>jquery<span style="color: #339933;">/</span>i<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Note the 'i' flag for case-insensitive</span></pre></td></tr></table></div>

<h3>Data is type-sensitive</h3>
<p>Data stored within MongoDB knows it&#8217;s type. There is a small but significant difference between these two records:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'count'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">102</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 'count' is stored as an int</span>
<span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'count'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;102&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 'count' is stored as a string</span></pre></td></tr></table></div>

<p>This is obvious to any programmer when presented like this, but what may not be obvious is that this also affects how you can query for these records:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// This returns 1 instead of 2, because it only matches the integer value</span>
db.<span style="color: #660066;">test</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'count'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">102</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is due to how MongoDB stores documents internally with <a href="http://bsonspec.org/">BSON</a> (Binary JSON), using various <a href="http://www.mongodb.org/display/DOCS/Data+Types+and+Conventions">MongoDB data types</a>. This means &#8211; like the point above &#8211; that you must pay attention to how you are saving data <em>into</em> MongoDB, because it will affect how you can query for it later.</p>
<h3>Documents sizes are capped at 4MB each</h3>
<p>This isn&#8217;t a big issue for most people, but it&#8217;s something to be aware of if you plan on storing large chunks of text or nesting a bunch of objects inside a single document. <a href="http://www.businessinsider.com/how-we-use-mongodb-2009-11">Nesting comments inside article documents</a> is a particular approach that may give you pause knowing there is an upper threshold on document size.</p>
<p>Note that this limitation is not an issue for storing <em>files</em> in MongoDB because <a href="http://www.mongodb.org/display/DOCS/GridFS">GridFS</a> transparently divides the contents of files larger than 4MB across multiple documents.</p>
<h3>Only one index is used per query</h3>
<p>Simply adding more indexes doesn&#8217;t always help queries run faster. MongoDB can&#8217;t use multiple indexes together like MySQL and other RDBMS can (see &#8220;<a href="http://dev.mysql.com/doc/refman/5.0/en/index-merge-optimization.html">Index Merge Optimization</a>&#8220;). This means that if a query is selecting or sorting based on multiple fields, a compound index should be created with those fields for the query to run most efficiently.</p>
<h3>MongoDB is for high-memory and 64-bit systems only</h3>
<p>Although MongoDB can be downloaded, compiled, and installed on 32-bit systems, it should never be run in production on them. This is because <a href="http://www.mongodb.org/display/DOCS/Indexing+Advice+and+FAQ#IndexingAdviceandFAQ-MakesureyourindexescanfitinRAM.">MongoDB stores all indexes in memory</a> as well as <a href="http://www.mongodb.org/display/DOCS/Caching">memory-mapped files</a> for all disk I/O for increased speed and throughput. While these two facts aren&#8217;t &#8220;gotchas&#8221; themselves &#8211; they are clearly explained on the website &#8211; it does mean that MongoDB can quickly use a lot of memory, especially as the size of your database grows and becomes significant. Since 32-bit systems have an effective <a href="http://en.wikipedia.org/wiki/3_GB_barrier">3GB memory limit</a> of addressable memory space, they prevent you from being able to add more memory as the size of your database grows. This is also something to think about if you plan to run MongoDB on a small VPS with limited memory, even if it is 64-bit. You may be forced into a memory upgrade sooner than you are prepared for if your database is large or rapidly growing.</p>
<h3>Subtle Differences</h3>
<p>When starting out with MongoDB, it&#8217;s easy to draw a lot of parallels to MySQL and make a lot of assumptions based on those similarities. Collections are kind of like tables, fields are kind of like columns, a document is kind of like a row. You may find yourself going on to make more assumptions about how it&#8217;s storing data, how you can query it, what you can do with it, etc. without even knowing it. So while MongoDB has a lot of similar features and is one of the easiest NoSQL databases to transition to from a relational database, it has some very distinct differences &#8220;under the hood&#8221; that you have to be aware of and plan for ahead of time. The <a href="http://www.mongodb.org/display/DOCS/Manual">MongoDB documentation</a> is well-written, and is pretty good at explaining any potential differences or &#8220;gotchas&#8221;, so make sure to read it thoroughly before making the jump &#8211; and watch that first step.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/mongodb-gotchas/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>NoSQL First Impressions: Object Databases Missed the Boat</title>
		<link>http://www.vancelucas.com/blog/nosql-first-impressions-object-databases-missed-the-boat/</link>
		<comments>http://www.vancelucas.com/blog/nosql-first-impressions-object-databases-missed-the-boat/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 17:00:57 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[rdbms oodb]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=551</guid>
		<description><![CDATA[I&#8217;ve spent the past few weeks here at work researching and playing with NoSQL databases (and especially MongoDB) for a new feature we&#8217;re developing that doesn&#8217;t easily fit into a relational model. And so far, I really like what I see. The profoundness of the shift away from the relational model and the implications that [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent the past few weeks here at work researching and playing with <a href="http://en.wikipedia.org/wiki/NoSQL">NoSQL</a> databases (and especially <a href="http://mongodb.org">MongoDB</a>) for a new feature we&#8217;re developing that doesn&#8217;t easily fit into a relational model. And so far, I <em>really</em> like what I see. The profoundness of the shift away from the relational model and the implications that has just blow my mind. <strong>You no longer have to fragment your data to persist it</strong><em>.</em> You just store it. That&#8217;s it. No more hours toiling over the design your table schema and how to break apart the data you put together just to fit it into a relational model. You can now store your data exactly how you use it in your application, with no other special needs or <a href="http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch">impedance mismatches</a>. Going back to an <a href="http://en.wikipedia.org/wiki/RDBMS">RDBMS</a> system now just seems illogical &#8211; it&#8217;s like breaking apart a camera tripod just to fit it in the same standard size case you&#8217;ve been using for years instead of just collapsing it and finding a different case that fits it better. All that effort you go though tearing down your tripod and putting it back together every time you use it is wasted and unnecessary. It&#8217;s a symptom of the larger problem that your case doesn&#8217;t fit your tripod.<span id="more-551"></span></p>
<p>Once you fully get the NoSQL viewpoint and the implications it has on  the future of data storage, you have one of those &#8220;why didn&#8217;t anyone  think of this sooner?&#8221; moments. And yes, I know key/value stores like <a href="http://memcached.org/">memcached</a> existed long before this whole new &#8220;NoSQL&#8221; movement, but it&#8217;s not quite  the same thing.</p>
<h3>Object Databases</h3>
<p>Object databases are an interesting case. They are in the &#8220;NoSQL&#8221; category and solve the same problem other NoSQL databases do &#8211; creating a storage mechanism that fits your data instead of making your data fit a predefined rigid storage mechanism. So why didn&#8217;t they catch on? What gives? It&#8217;s anyone&#8217;s guess why object databases didn&#8217;t catch on and achieve widespread adoption, but one thing is for sure &#8211; <strong>object databases really missed the boat</strong> while other NoSQL database technologies are running rings around them.</p>
<p>The primary problem is that most object databases are designed for a specific language, like Java or .NET. Some have more language support than others, but the problem is still the same &#8211; by integrating in directly with a language&#8217;s object model they depend on specific language implementations, which severely limits adoption. They solve the impedance mismatch problem that RDBMSes present, but <strong>fail to provide language-agnostic data persistence and access</strong>, effectively tying their use to specific technology stacks and platforms.</p>
<h3>The Best of Both Worlds</h3>
<p>NoSQL databases are gaining traction like no other database technology has in over 30 years. The reason is that <strong>good NoSQL databases solve both key data storage problems: impedance mismatches AND language-agnosticism.</strong> By storing the data in an intermediary format like JSON (or BSON) and allowing custom file content types, the focus is on the actual data itself instead of specific technologies or data models. The technology stack becomes irrelevant, and the walls to gaining user adoption crumble.</p>
<p>The bottom line is that NoSQL databases are not just the latest fad. They represent a fundamental paradigm shift in data storage and are an entirely new way of thinking about how to store and access your data in a way that makes sense for your actual usage. NoSQL databases are absolutely something you should be paying attention to and following closely &#8211; they are a strong contender to the RDBMS model and will likely become the de-facto data storage choice for most new web applications within a decade.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/nosql-first-impressions-object-databases-missed-the-boat/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Get Only Public Class Properties for the Current Class in PHP</title>
		<link>http://www.vancelucas.com/blog/get-only-public-class-properties-for-current-class-in-php/</link>
		<comments>http://www.vancelucas.com/blog/get-only-public-class-properties-for-current-class-in-php/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 23:06:01 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[class properties]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[oo]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=525</guid>
		<description><![CDATA[PHP provides two built-in functions to retrieve properties of a given class &#8211; get_object_vars and get_class_vars. Both these functions behave the same exact way, one taking an object as a variable and the other taking a string class name. The tricky thing about the two functions is that they behave differently depending on the call [...]]]></description>
			<content:encoded><![CDATA[<p>PHP provides two built-in functions to retrieve properties of a given class &#8211; <a href="http://php.net/get_object_vars">get_object_vars</a> and <a href="http://php.net/get_class_vars">get_class_vars</a>. Both these functions behave the same exact way, one taking an object as a variable and the other taking a string class name. The tricky thing about the two functions is that they behave differently depending on the call scope, returning all of the class variables available within the called scope. So if you call either function within the current class you need properties from, all properties are returned &#8211; public, protected, and private &#8211; because the current scope has access to them all. This makes seemingly simple things like returning all the public properties within the current class a bit of a pain if you want to keep the code inside the class itself.<br />
<span id="more-525"></span><br />
The obvious solution is to use the functions from a different call scope, which means either moving the call outside the class to the implementation code (yuck), or creating a new function outside the class for it. A lot of suggestions in the PHP manual say to create a new function below the class definition and call it within the class (kind of a proxy function as a workaround), but that seems tacky. Luckily, PHP also provides another way to get a new call scope: <a href="http://php.net/manual/en/functions.anonymous.php">anonymous functions</a> (and <em>closures</em> as of PHP 5.3).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> BobUser
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bob'</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$publicFlag</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$internalFlag</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFields<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$getFields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">create_function</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'$obj'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'return get_object_vars($obj);'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$getFields</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns only 'name' and 'publicFlag'</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We can also do this the PHP 5.3 way with a much nicer-looking closure:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> BobUser
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// ...</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFields<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$getFields</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #990000;">get_object_vars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$obj</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$getFields</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/get-only-public-class-properties-for-current-class-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why WordPress Should Not Have Won the Open Source CMS Award</title>
		<link>http://www.vancelucas.com/blog/wordpress-and-the-open-source-cms-award/</link>
		<comments>http://www.vancelucas.com/blog/wordpress-and-the-open-source-cms-award/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 19:10:26 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[awards]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[Drupal]]></category>
		<category><![CDATA[Joomla]]></category>
		<category><![CDATA[MODx]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[open source cms awards]]></category>
		<category><![CDATA[SilverStripe]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=498</guid>
		<description><![CDATA[Packt Publishing announced the winners for their annual Open Source CMS Award in November, and since then I have been a bit disturbed that the 2009 winner was WordPress. My first reaction was this: &#8220;&#8230; So a blogging platform won the content management system award? How sad is that?&#8221; My knee-jerk &#8220;how sad is that?&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>Packt Publishing announced the winners for their annual <a href="http://www.packtpub.com/award">Open Source CMS Award</a> in November, and since then I have been a bit disturbed that the 2009 <a href="http://wordpress.org/development/2009/11/wordpress-wins-cms-award/">winner was WordPress</a>. My first reaction was this:</p>
<blockquote><p>&#8220;&#8230; So a <em>blogging platform</em> won the <em>content management system</em> award? How sad is that?&#8221;</p></blockquote>
<p>My knee-jerk &#8220;how sad is that?&#8221; reaction comes not because I don&#8217;t think WordPress is worthy, but because of what it implies about the state of other open source CMS projects. The reaction comes from the fact that <em>a </em><strong>blogging platform is kicking your CMS&#8217;s ass in its own category</strong>.</p>
<p><span id="more-498"></span></p>
<p>WordPress bridges both the blogging and CMS categories due to the &#8216;Pages&#8217; feature, and is extremely useful for managing a blog-focused website. Mostly. That is, until you want to do something that a CMS should be good at, like have an event calendar, custom form, photo gallery, etc. &#8211; which is why WordPress is not focused on being a CMS in the first place. Yet <em>it does such a better job at the basic things</em> like creating new pages, tagging, categorizing, comments, and having custom SEO-friendly URLs out of the box that it edged out other software projects like <del><a href="http://drupal.org/">Drupal</a></del> <a href="http://modxcms.com/">MODx</a> and <del><a href="http://www.joomla.org/">Joomla!</a></del> <a href="http://www.silverstripe.com/">SilverStripe</a> whose sole focus is content management. Sad indeed.</p>
<p>The fact that I can go through <a href="http://php.opensourcecms.com/scripts/show.php?catid=1&amp;cat=CMS%20/%20Portals">literally hundreds</a> of open source content management systems and still end up settling on WordPress because I know it&#8217;s the only one that won&#8217;t totally confuse my client is what&#8217;s sad. Usability and ease of use matter. They are the number one feature to the end user. If you&#8217;re involved in a CMS project, you need to do better. A <em>lot</em> better. <strong>Right now</strong>.</p>
<p><strong>UPDATE</strong>: Some commenters have pointed out that the awards website has a specific rule:</p>
<blockquote><p>Previous winners of the Overall category are not eligible for the Overall category in 2009. Previous winners compete amongst one another in a separate Hall of Fame category designed specifically for them.</p></blockquote>
<p>Since both Drupal and Joomla! have won the award previously and thus were automatically excluded, I replaced their names with the CMS projects that were 2nd and 3rd behind WordPress (MODx and SilverStripe) instead. The premise of the post still holds true.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/wordpress-and-the-open-source-cms-award/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>CodeWorks 2009 Dallas</title>
		<link>http://www.vancelucas.com/blog/codeworks-2009-dallas/</link>
		<comments>http://www.vancelucas.com/blog/codeworks-2009-dallas/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 05:03:52 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Speaking Engagements]]></category>
		<category><![CDATA[codeworks]]></category>
		<category><![CDATA[cw09]]></category>
		<category><![CDATA[speaking]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=436</guid>
		<description><![CDATA[I was fortunate enough to be selected as the regional speaker for the Dallas CodeWorks 2009 stop by the Dallas PHP User Group through a community voting and selection process. My talk was entitled Object Oriented Apologetics, and was essentially about letting people know what good object-oriented code is, when to use it, how to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://cw.mtacon.com/schedule/speakers#vance_lucas"><img class="alignright" src="http://www.vancelucas.com/wp-content/uploads/2009/09/CW09_Speaker.png" alt="CodeWorks 2009 Speaker" width="150" height="200" /></a><br />
I was fortunate enough to be selected as the regional speaker for the Dallas <a href="http://cw.mtacon.com/">CodeWorks 2009</a> stop by the <a href="http://dallasphp.org">Dallas PHP User Group</a> through a community voting and selection process. My talk was entitled <strong>Object Oriented Apologetics</strong>, and was essentially about letting people know what good object-oriented code is, when to use it, how to use it, and more specifically <em>why</em> to use it over traditional procedural PHP code.<span id="more-436"></span></p>
<h3>Object Oriented Apologetics</h3>
<blockquote><p>In defense of object-oriented programming &#8211; How and why you should use object oriented programming for your next project.This talk is for PHP programmers who are just learning about object oriented code, who cling to old excuses(&#8220;object oriented code is slower&#8221;), or who are otherwise unconvinced of its usefulness. Concrete real-world examples of commonscenarios and challenges that programmers face will be presented, and how taking an object oriented approach is better than a proceduralone in most cases. Copious code examples in both object oriented and procedural approaches will be provided throughout, and thedifferences and benefits of the object oriented approach will be explained.</p></blockquote>
<div id="__ss_2092025" style="width: 425px; text-align: left;"><object style="margin:0px" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="355" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=objectorientedapologetics-090929144850-phpapp02&amp;rel=0&amp;stripped_title=object-oriented-apologetics" /><param name="allowfullscreen" value="true" /><embed style="margin:0px" type="application/x-shockwave-flash" width="425" height="355" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=objectorientedapologetics-090929144850-phpapp02&amp;rel=0&amp;stripped_title=object-oriented-apologetics" allowscriptaccess="always" allowfullscreen="true"></embed></object></div>
<p><a href="http://www.slideshare.net/vlucas/object-oriented-apologetics">Download Object Oriented Apologetics on SlideShare</a></p>
<p>If you were in the talk, <a href="http://joind.in/talk/view/722">please rate it on Joind.in</a></p>
<h3>The CodeWorks Experience</h3>
<p>All in all, the CodeWorks roadshow Dallas stop was much smaller than I expected. There were about 20 people in the talk I gave. I suppose it was both a good a bad thing. On one hand, I had a lot of fun connecting with the other speakers and attendees on a more personal level than I would have had the opportunity to do otherwise. I met a lot of new people in the PHP community that I will probably stay connected with on some level, even if it is just <a href="http://twitter.com">Twitter</a> and IRC. We had a lot of fun the night before the presentation day eating together and hanging out.</p>
<p>On the other hand, I know that as a business, the relatively low attendance levels coupled with the high travel expenses could mean that something like this can&#8217;t happen again, which is a shame. This was one of the best efforts I have seen in a while to really lower the price of the conference for attendees by bringing the speakers directly to their cities or at least ones that are close by. Going to <a href="http://www.zendcon.com">ZendCon</a>, for instance, could easily cost up to $2,500 for the whole trip with airfare, hotel stays, and food, if not more.</p>
<p>The bottom line here is that CodeWorks was a good conference that offered a great value for your money. I had a great time meeting and connecting with new people and finally meeting people in real life that I had been communicating with for years. I learned some new things and finally got the kick in the pants I needed to jump into <a href="http://en.wikipedia.org/wiki/Test-driven_development">Test Driven Development</a>, thanks to <a href="http://blog.casey-sweat.us/">Jason Sweat&#8217;</a>s TDD tutorial and hand-holding.</p>
<p>Now I&#8217;m just looking forward to php|tek in May 2010.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/codeworks-2009-dallas/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The One Character Block Comment</title>
		<link>http://www.vancelucas.com/blog/the-one-character-block-comment/</link>
		<comments>http://www.vancelucas.com/blog/the-one-character-block-comment/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 14:58:10 +0000</pubDate>
		<dc:creator>Vance Lucas</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code comments]]></category>
		<category><![CDATA[comments]]></category>

		<guid isPermaLink="false">http://www.vancelucas.com/?p=401</guid>
		<description><![CDATA[When debugging, I often find that I have to comment and un-comment a block of code several times during the process of trying to find out what&#8217;s going on. That used to mean typing and deleting comment block characters repetitively, but not anymore. Here&#8217;s a simple solution to that problem: Comment or un-comment an entire [...]]]></description>
			<content:encoded><![CDATA[<p>When debugging, I often find that I have to comment and un-comment a block of code several times during the process of trying to find out what&#8217;s going on. That used to mean typing and deleting comment block characters repetitively, but not anymore. Here&#8217;s a simple solution to that problem: Comment or un-comment an entire code block of code by typing or deleting a single character.</p>
<p>I was able to arrive at this solution by combining the one-line comment with the comment block in a way that takes advantage of the rules the different types of comments have to follow.<br />
<span id="more-401"></span></p>
<h3>The One-Line Comment</h3>
<p>One-line comment rules dictate that everything after the comment characters must be ignored for the rest of that line. They look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// My code below</span>
<span style="color: #000088;">$someVar</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;blah&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;code&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$someVar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/code&gt;&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>The Block Comment</h3>
<p>Block comment rules dictate that once the beginning characters are started, everything up until the ending characters is ignored. They look like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* My code below
$someVar = array(&quot;foo&quot;, &quot;bar&quot;, &quot;blah&quot;);
echo &quot;&lt;code&gt;&quot;;
print_r($someVar);
echo &quot;&lt;/code&gt;&quot;;
*/</span></pre></td></tr></table></div>

<h3>Combining The Two: The Best of Both Worlds</h3>
<p>Using both those comment style rules, we can combine the two comment styles in a way that will allow us to comment and un-comment a block of code with by adding or deleting a single character. The trick is to make both the beginning and ending lines both single-line AND block style comments, like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//* My code below</span>
<span style="color: #000088;">$someVar</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;bar&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;blah&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;code&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$someVar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/code&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// */</span></pre></td></tr></table></div>

<p>In the above example, you notice that the code will still run &#8211; it&#8217;s not commented out because the first line is following the rules of the single-line comment &#8211; ignoring the block comment declaration on the same line. By removing the first backslash at the beginning of the block, we can comment out the entire code block by enabling the block comment style instead of the single-line style. The last single-comment line is ignored because the block comment rules ignore everything up until the end block comment declaration at the end of the line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* My code below
$someVar = array(&quot;foo&quot;, &quot;bar&quot;, &quot;blah&quot;);
echo &quot;&lt;code&gt;&quot;;
print_r($someVar);
echo &quot;&lt;/code&gt;&quot;;
// */</span></pre></td></tr></table></div>

<p>So a single character &#8211; a backslash at the very beginning of the block declaration can now comment or un-comment the entire block by switching the comment rules back and forth between single-line and block style. The code examples provided are in PHP, but this trick will work with any language that supports both single-line and block style comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.vancelucas.com/blog/the-one-character-block-comment/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

