<?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>Philip Klauzinski</title>
	<atom:link href="http://klauzinski.com/feed" rel="self" type="application/rss+xml" />
	<link>http://klauzinski.com</link>
	<description>PHP, CakePHP, JavaScript, jQuery &#38; More</description>
	<lastBuildDate>Mon, 09 Jan 2012 20:52:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Introducing jScroll</title>
		<link>http://klauzinski.com/javascript/introducing-jscroll</link>
		<comments>http://klauzinski.com/javascript/introducing-jscroll#comments</comments>
		<pubDate>Mon, 08 Aug 2011 05:30:13 +0000</pubDate>
		<dc:creator>Philip Klauzinski</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://klauzinski.com/?p=181</guid>
		<description><![CDATA[jScroll is a jQuery plugin I have developed for the simple purpose of infinite scrolling, lazy loading, or whatever catchy phrase you may know it as. A real-world example of this behavior is your Facebook News Feed, which automatically loads content as you scroll down and reach the end of the page. I decided to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://jscroll.com/" rel="external">jScroll</a> is a jQuery plugin I have developed for the simple purpose of <em>infinite scrolling</em>, <em>lazy loading</em>, or whatever catchy phrase you may know it as. A real-world example of this behavior is your Facebook News Feed, which automatically loads content as you scroll down and reach the end of the page.</p>
<p>I decided to develop this plugin because I did not find any jQuery plugins out there that suited my needs precisely, or any that I could even get to work very well. For more information on jScroll and its use, please visit <a href="http://jscroll.com/" rel="external">jscroll.com</a>. There is no extensive documentation at this time, as the plugin is fairly simple and was developed ad hoc. I plan to continue its development and add features, however, over an indefinite period of time.</p>
<p>Learn more at <a href="http://jscroll.com/" rel="external">jscroll.com</a>.<br />
Example use at <a href="http://jscroll.com/#example" rel="external">jscroll.com/#example</a>.<br />
Download the latest version at <a href="https://github.com/pklauzinski/jscroll" rel="external">https://github.com/pklauzinski/jscroll</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://klauzinski.com/javascript/introducing-jscroll/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Safe Firebug Console in Javascript</title>
		<link>http://klauzinski.com/javascript/safe-firebug-console-in-javascript</link>
		<comments>http://klauzinski.com/javascript/safe-firebug-console-in-javascript#comments</comments>
		<pubDate>Thu, 24 Mar 2011 06:15:20 +0000</pubDate>
		<dc:creator>Philip Klauzinski</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://klauzinski.com/?p=115</guid>
		<description><![CDATA[Firebug&#8216;s console object is an extremely useful tool for debugging and logging javascript interactions and responses, especially when working with AJAX and JSONP. What is not useful, is that when using the console object&#8217;s methods directly in your included javascript code, it will not work in most browsers other than Firefox, and of course it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://getfirebug.com/" rel="external">Firebug</a>&#8216;s <a href="http://getfirebug.com/wiki/index.php/Console_API" rel="external"><code>console</code> object</a> is an extremely useful tool for debugging and logging javascript interactions and responses, especially when working with AJAX and JSONP. What is not useful, is that when using the <code>console</code> object&#8217;s methods directly in your included javascript code, it will not work in most browsers other than <a href="http://www.mozilla.com/en-US/firefox/firefox.html" rel="external">Firefox</a>, and of course it will cause errors if you (or your users) do not have Firebug installed.</p>
<p>Many of us have made the mistake of sometimes forgetting to delete our <code>console</code> code after testing and before launching our javascript to our production site, thus causing javascript errors, breaking the UI, or perhaps even breaking an entire application, depending on how javascript-dependent it is.</p>
<p>I aimed to solve this problem, and not just the problem of leaving the <code>console</code> code in my javascript by accident, but to allow it intentionally, if I wish, without breaking any browser that does not support it. <span id="more-115"></span></p>
<h2>First Create a Utility Object to Provide a Namespace</h2>
<p>I always find it useful to have a set of javascript utility methods that I can use in any website project. I typically contain these methods in an object literal to provide a namespace to contain them, so that they are not in the global (<code>window</code>) scope. I also like to use a debug parameter that I can set to true or false explicitly, or set it based on the current domain name, in order to disable any javascript code I may have written that is intended for debugging.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> U <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    bDebug<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    console<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Using the New Console Function</h2>
<p>Since the console methods are something that can be used across scripts and even across websites, it makes sense to keep access to them within a global namespace container such as this. Thus, the console methods will be accessed by using a property of the <code>U</code> namespace called <code>console</code>, which is a function, rather than an object. The console method that you wish to access is then passed as a string for the first argument. Any remaining arguments will be passed as the arguments for that method and applied to it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">U.<span style="color: #660066;">console</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'log'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'This is a log test.'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'This is another log test.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code above will safely call <code>console.log()</code> and pass the second and third arguments as two arguments to that method, delimited by a space.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">U.<span style="color: #660066;">console</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'info'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'This is some info.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
U.<span style="color: #660066;">console</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'warn'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'This is a warning.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The two lines above will make a call to <code>console.info()</code>, showing the &#8220;info&#8221; icon, and to <code>console.warn()</code> showing the &#8220;warning&#8221; icon. Any other console methods will work in the same fashion.</p>
<h2>Make Successive Calls to the Console With Ease</h2>
<p>This method of making calls to the console methods is great for doing one call at time, such as showing a single line of info, logging, or displaying a warning. Making several successive calls to the <code>console</code> methods, however, sometimes can be irritating and looks a bit messy. For this reason, the function will also handle an object literal passed in as the first argument which contains console method names as the keys and their values as the arguments.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">U.<span style="color: #660066;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    group<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Group message'</span><span style="color: #339933;">,</span>
    info<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Display some info here.'</span><span style="color: #339933;">,</span>
    dir<span style="color: #339933;">:</span> objectVar<span style="color: #339933;">,</span>
    groupEnd<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code above will make four successive calls to the console methods indicated by the key names, with the values passed as the arguments, and will be called in the order for which they are contained. If a method takes no arguments, <code>null</code>, <code>false</code>, or <code>[]</code> will suffice, but something must be passed in order for the object literal to validate.</p>
<p>Most console methods only take one argument, but for those that allow for more than one argument and you wish to take advantage of this, the object literal notation will handle this as well. Simply pass the arguments for that method in an array, rather than as one explicit value.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">U.<span style="color: #660066;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    groupCollapsed<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Group message'</span><span style="color: #339933;">,</span>
    debug<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Debug message'</span><span style="color: #339933;">,</span>
    info<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Info'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'that is'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'space delimited'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    dir<span style="color: #339933;">:</span> objectVar<span style="color: #339933;">,</span>
    groupEnd<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This code will make five consecutive calls to the console in the order for which they are contained, and will pass three arguments for the call to <code>console.info()</code>, which are contained in an array for the third name/value pair shown above.</p>
<h2>The Safe Console Code</h2>
<p>And that&#8217;s it. I think you will find that this is much more user-friendly than using the native Firebug console code in your scripts, and you can run the code only when you wish to debug, and without having to worry about breaking your application in any browsers that do not support it. This code not only ensures that the <code>console</code> object exists, but also ensures that the methods you are calling exist as well.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> U <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    bDebug<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #006600; font-style: italic;">/**
     * Use any console.method without throwing error if console is not present.
     * 
     * Allows you to pass in a single method as a string with the subsequent parameters being
     * that method's arguments, OR pass in an object containing keys as method names and
     * values as method arguments (single argument or array of multiple arguments).
     * 
     * @author pklauzinski
     * @param mixed m: string || object
     */</span>
    console<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>m<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>U.<span style="color: #660066;">bDebug</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> console <span style="color: #339933;">===</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">&amp;&amp;</span>
                <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> m <span style="color: #339933;">===</span> <span style="color: #3366CC;">'object'</span> <span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">typeof</span> console<span style="color: #009900;">&#91;</span>m<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> m <span style="color: #339933;">===</span> <span style="color: #3366CC;">'object'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> sMethod <span style="color: #000066; font-weight: bold;">in</span> m<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><span style="color: #000066; font-weight: bold;">typeof</span> console<span style="color: #009900;">&#91;</span>sMethod<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        args <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#91;</span>sMethod<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> m<span style="color: #009900;">&#91;</span>sMethod<span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>m<span style="color: #009900;">&#91;</span>sMethod<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                        console<span style="color: #009900;">&#91;</span>sMethod<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>console<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                console<span style="color: #009900;">&#91;</span>m<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>console<span style="color: #339933;">,</span> Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">slice</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://klauzinski.com/javascript/safe-firebug-console-in-javascript/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Search Engine Friendly URLs in CakePHP</title>
		<link>http://klauzinski.com/php/cakephp/search-engine-friendly-urls-in-cakephp</link>
		<comments>http://klauzinski.com/php/cakephp/search-engine-friendly-urls-in-cakephp#comments</comments>
		<pubDate>Tue, 03 Feb 2009 20:07:12 +0000</pubDate>
		<dc:creator>Philip Klauzinski</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[friendly urls]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[seo]]></category>

		<guid isPermaLink="false">http://klauzinski.com/?p=8</guid>
		<description><![CDATA[One thing that is not built-in to CakePHP is the ability to use search engine friendly controller and action names when those names consist of more than one word. CakePHP does an excellent job of allowing friendly URLs in general, but this typically involves single-word controller and action names. So what about controller and action [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that is not built-in to <a href="http://cakephp.org/" class="external" rel="nofollow">CakePHP</a> is the ability to use <em>search engine friendly</em> controller and action names when those names consist of more than one word. CakePHP does an excellent job of allowing friendly URLs in general, but this typically involves single-word controller and action names. So what about controller and action names with multiple words? In CakePHP, this is handled with class names defined in <code>CamelCase</code> and method names defined in <code>camelBack</code>. <span id="more-8"></span></p>
<p>For instance, you may have a blog system built with CakePHP that uses a typical controller name, such as <code>PostsController</code>, and a typical action name, such as <code>view</code>, which takes a blog post <em>slug</em> (search engine friendly name) as its first argument. In the MVC paradigm, this would be accessed with a URL like the one below.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">http://myblog.com/posts/view/search-engine-friendly-url</pre></div></div>

<p>Most everyone would agree that this is a &#8220;friendly&#8221; URL, and in this case, the <a href="http://bakery.cakephp.org/articles/view/sluggable-behavior" class="external">sluggable behavior</a>, or something similar, would allow you to easily provide a slug as the first argument to the <code>PostsController->view()</code> method, as shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> PostsController <span style="color: #000000; font-weight: bold;">extends</span> AppController <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Posts'</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> view<span style="color: #009900;">&#40;</span><span style="color: #000088;">$slug</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$aPost</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findBySlug</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$slug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'aPost'</span><span style="color: #009900;">&#41;</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></div></div>

<p>The purpose of using a slug is to provide a dash-separated (hyphen-separated) version of the blog post title in the URL for <abbr title="Search Engine Optimization">SEO</abbr>, a common feature of most popular blog publishing systems.</p>
<h2>Multiple Word Controller Names</h2>
<p>CakePHP allows multiple-word controller names to be accessed from the URL in several ways.</p>
<ol>
<li><code>CamelCase ControllerName</code>, as in &#8220;http://myblog.com/ControllerName&#8221;;</li>
<li><code>camelBack controllerName</code>, as in &#8220;http://myblog.com/controllerName&#8221;;</li>
<li>underscore-separated, as in &#8220;http://myblog.com/controller_name&#8221;;</li>
<li>or any combination of the above.</li>
</ol>
<p>This gives you a lot of options for selecting your URL scheme of choice, but one option that is not available out of the box with CakePHP is a <em>dash-separated</em> controller name in the URL. This troubles me because I prefer dash-separated URLs, and <a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&#038;answer=76329" rel="external">Google recommends hyphens instead of underscores in your URLs</a>.</p>
<p>I also prefer to avoid uppercase letters in URLs entirely, so with CakePHP, the only viable option out of the box for multiple word controller and action names is to use underscore-separated names. For these reasons, I decided to implement my own way of allowing for dash-separated controller and action names in the URL to map to their respective <code>CamelCase</code> and <code>camelBack</code> names in CakePHP.</p>
<h2>Implementing Dash-separated Controller and Action Names in CakePHP URLs</h2>
<p>I will not claim that my method for implementing dash-separated controller and action names in CakePHP is the best way, and it does have its downside, but it is the quickest way that I could think of to accomplish this task without too much trouble.</p>
<p>To do this, I simply added the following snippet of code to the <code>/app/config/routes.php</code> file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$aUri</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bControllerDash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aUri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bActionDash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aUri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aUri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bControllerDash</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$bActionDash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sController</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bControllerDash</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aUri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$aUri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sAction</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bActionDash</span><span style="color: #009900;">&#41;</span> ? Inflector<span style="color: #339933;">::</span><span style="color: #004000;">variable</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'_'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aUri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'index'</span><span style="color: #339933;">;</span>
    Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$aUri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aUri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$aUri</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/*'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$sController</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$sAction</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this block of code, the default <code>$_GET['url']</code> parameter that gets passed from the query string to CakePHP is analyzed for the existence of dashes (hyphens) in either the controller or action portion of the URL. If either the controller or action name contains any dashes, those dashes are replaced with underscores, and in the case of the action name, it is converted to its <code>camelBack</code> name using CakePHP&#8217;s <a href="http://api.cakephp.org/class/inflector#method-Inflectorvariable" rel="external">Inflector::variable</a> method. This only applies if you are using action names defined in <code>camelBack</code>, of course.</p>
<h2>The Downside</h2>
<p>The downside to using this method is that it prevents the proper use of CakePHP&#8217;s built-in <a href="http://book.cakephp.org/view/543/Passing-parameters-to-action" rel="external">reverse routing</a>. In other words, using the <a href="http://api.cakephp.org/class/html-helper#method-HtmlHelperlink" rel="external">HtmlHelper::link</a> method to create a link with a code block like the one below will not map to a dash-separated version of the URL in the generated link.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #000088;">$html</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'My link'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'controllerName'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'actionName'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Improving Reverse Routing in CakePHP in the Future</h2>
<p>I would recommend that the CakePHP team look at adding a configuration option in the future that would allow you to define the type of URLs you would like to be generated when using reverse routing. This way you could always have clean URLs in a consistent format by simply using reverse routing, the <code>/app/config/routes.php</code> configuration file, and a URL <em>type</em> or <em>style</em> setting.</p>
<p>For example, a method could be called inside <code>routes.php</code> with an array of parameters to set the style for all generated URLs:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Router<span style="color: #339933;">::</span><span style="color: #004000;">setUrlStyle</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'case'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'lower'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'separator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'hyphen'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this way, such preferences as the <em>case</em> and the word <em>separator</em> could be set for consistency and to ensure search engine friendly URLs.</p>
]]></content:encoded>
			<wfw:commentRss>http://klauzinski.com/php/cakephp/search-engine-friendly-urls-in-cakephp/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP Coding Guidelines</title>
		<link>http://klauzinski.com/php/php-coding-guidelines</link>
		<comments>http://klauzinski.com/php/php-coding-guidelines#comments</comments>
		<pubDate>Fri, 31 Oct 2008 18:37:38 +0000</pubDate>
		<dc:creator>Philip Klauzinski</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[coding guidelines]]></category>

		<guid isPermaLink="false">http://klauzinski.com/?p=12</guid>
		<description><![CDATA[The following PHP coding guidelines represent my own conventions and standards which I code by, along with some CakePHP conventions when developing with CakePHP, my PHP MVC framework of choice. These PHP coding guidelines are by no means an industry standard, nor are they intended to tell you PHP coders out there how things should [...]]]></description>
			<content:encoded><![CDATA[<p>The following <abbr title="Hypertext Preprocessor">PHP</abbr> coding guidelines represent my own conventions and standards which I code by, along with some <a class="external" rel="nofollow" href="http://book.cakephp.org/view/328/Cake-Conventions">CakePHP conventions</a> when developing with <a class="external" rel="nofollow" href="http://cakephp.org/">CakePHP</a>, my PHP <abbr title="Model View Controller">MVC</abbr> framework of choice.</p>
<p>These PHP coding guidelines are by no means an industry standard, nor are they intended to tell you PHP coders out there how things should be done. I simply want to share the PHP coding guidelines which I follow and enforce when working with other developers on larger projects.</p>
<p>Laying down some coding guidelines like these helps to provide a more structured and rapid development environment, and makes the PHP code more predictable and easier to debug. <span id="more-12"></span></p>
<h2>PHP Code Delimitation</h2>
<p>PHP code must always be delimited by the full-form, standard PHP tags:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Short tags are never allowed:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The PHP <em>echo()</em> shortcut syntax is also not allowed:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">// Do not use:
<span style="color: #000000; font-weight: bold;">&lt;?=</span><span style="color: #000088;">$sVarName</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
// Instead use:
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">print</span> <span style="color: #000088;">$sVarName</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Always use <em>four spaces</em> for code indentation. Do not use actual <em>tabs</em>. This prevents spacing issues when opening documents in different types of editors and when viewing source code in browsers. Most editors (e.g. Eclipse, Dreamweaver) allow you to set the tab key to insert a defined number of spaces instead of an actual <em>tab</em>. Setting this preference in your editor is strongly encouraged so as to speed up development and to prevent tabs from accidentally being inserted.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sVar</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is a function.'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 4 spaces (1 tab)</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>bar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sVar</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// 4 spaces (1 tab)</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$sVar</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 8 spaces (2 tabs)</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// 4 spaces (1 tab)</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 8 spaces (2 tabs)</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// 4 spaces (1 tab)</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Note:</strong> To configure this in <a class="external text" title="http://www.eclipse.org/pdt/" rel="nofollow" href="http://www.eclipse.org/pdt/">Eclipse PDT</a>, go to <em>Window -> Preferences -> PHP -> Formatter</em> and set &#8220;Tab Policy&#8221; to &#8220;Spaces&#8221; and &#8220;Indentation size&#8221; to &#8220;4&#8243;.</p>
<h2>String literals</h2>
<p>When a string is literal (contains no variable substitutions), the <em>apostrophe</em> or <em>single quote</em> must always be used to demarcate the string, unless that string itself contains apostrophes. PHP will parse all double-quoted strings for occurences of variable substitution, thus taking more time to process. For enterprise-level applications with the possibility of hundreds of concurrent users, this can amount to a significant reduction in performance.</p>
<h3>Standard string literals</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sVar</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A string'</span><span style="color: #339933;">;</span></pre></div></div>

<p>This rule goes for strings within function calls and constructs as well:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">fooBar<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'string1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'string2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'This is a string'</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Note:</strong> Using double quotes for string demarcation throughout programs can increase processing time significantly, thus they are not permitted unless the string itself contains apostrophes.</p>
<h3>String literals containing apostrophes</h3>
<p>When a literal string itself contains apostrophes, it is permitted and preferred to demarcate the string with quotation marks or &#8220;double quotes.&#8221; This is especially encouraged for SQL statements:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id, name FROM people WHERE name = 'Fred' OR name = 'Susan'&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Note:</strong> The above syntax is preferred over <em>escaping</em> apostrophes so as to improve readability. Double quotes are only permitted when the string itself contains apostrophes.</p>
<h2>Variable substitution</h2>
<p>All variable substitution must be done with concatenation and should never occur within double quotes, due to a loss in both performance and readability. Substituting variables within double quotes can increase processing time by a factor of three. See <a class="external text" title="http://riyono.com/archives/2004/09/30/variables-within-double-quoted-string-seriously-slows-down-php-application/" rel="nofollow" href="http://riyono.com/archives/2004/09/30/variables-within-double-quoted-string-seriously-slows-down-php-application/">this article</a> showing some speed tests that were performed. The PHP manual itself also states, in regard to variable substitution within double quotes, <em><a class="external text" title="http://us2.php.net/manual/en/language.types.string.php#language.types.string.parsing.simple" rel="nofollow" href="http://us2.php.net/manual/en/language.types.string.php#language.types.string.parsing.simple">&#8220;If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name.&#8221;</a></em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Incorrect syntax:</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Hello, my name is <span style="color: #006699; font-weight: bold;">$sName</span>.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO companies (name, phone, fax) VALUES ('<span style="color: #006699; font-weight: bold;">$sName</span>', '<span style="color: #006699; font-weight: bold;">$sPhone</span>', '<span style="color: #006699; font-weight: bold;">$sFax</span>')&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Correct syntax:</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'Hello, my name is '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sName</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO companies (name, phone, fax) VALUES ('&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;', '&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sPhone</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;', '&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sFax</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;')&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>String concatenation</h2>
<p>Strings are concatenated using the &#8220;.&#8221; (period) concatenation operator. A space must always be added before and after the &#8220;.&#8221; operator to improve readability.</p>
<h3>Inline concatenation</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Concatenate this string with '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sVar1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' and '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sVar2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Multiple line concatenation</h3>
<p>When concatenating strings with the &#8220;.&#8221; operator, it is permitted to break the statement into multiple lines to improve readability. In these cases, each successive line should be indented with <em>at least four spaces (1 tab)</em>, and the <em>same number of spaces</em> must be used for each indented line such that the &#8220;.&#8221; operators are aligned on the left side.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'SELECT id, name FROM people '</span>
       <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;WHERE name = 'Susan' &quot;</span>
       <span style="color: #339933;">.</span> <span style="color: #0000ff;">'ORDER BY name ASC'</span><span style="color: #339933;">;</span></pre></div></div>

<p>You may also concatenate a string by redefining it with the concatenation operator (when necessary):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'SELECT id, name FROM people '</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;WHERE name = 'Susan' &quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'ORDER BY name ASC'</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Concatenation when printing to the screen</h3>
<p>When printing a string to ouput which includes concatenation, the <em>print()</em> construct must be used.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'Concatenate this string with '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$var1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' and '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$var2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span></pre></div></div>

<p>An alternative to concatenating ouput is using the <em>echo()</em> construct with multiple parameters, separated by commas:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Concatenate this string with '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' and '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Although the concatenation operator ( . ) may be used with the <em>echo()</em> construct, the above syntax should be used instead for optimal performance. Using the concatenation operator with the <em>echo()</em> construct stores each concatenated section of the string as a separate object in memory before printing to the screen. Therefore, <strong>concatenating with the <em>echo()</em> construct is not permitted</strong>.</p>
<h3>print() vs. echo()</h3>
<p>You can find much discussion regarding this on <a class="external text" title="http://www.google.com/search?hl=en&amp;q=print+vs.+echo&amp;btnG=Search" rel="nofollow" href="http://www.google.com/search?hl=en&amp;q=print+vs.+echo&amp;btnG=Search">the web</a>. <em>echo()</em> is shown to have a marginal but insignificant advantage in speed over <em>print()</em> when used in simple, non-concatenated instances. This difference is not enough to be a concern, however.</p>
<p>For sake of consistency, it is preferred that developers use <em>print()</em> in all cases. The <em>print()</em> construct also provides a few advantages over <em>echo()</em> which are favorable from a programming point of view:</p>
<ul>
<li>Although it is a <em>construct</em> and not a <em>function</em>, <em>print()</em> will return a value of <em>1</em> (true), whereas <em>echo()</em> has no return value.</li>
<li>Concatenating strings with the <em>print()</em> construct does not require additional stores to memory before executing.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Because echo() does not behave like a function, the following code is invalid.</span>
<span style="color: #b1b100;">return</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'true'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// However, the following example will work:</span>
<span style="color: #b1b100;">return</span> <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'true'</span><span style="color: #339933;">;</span></pre></div></div>

<p><em>Derived from: <a class="external free" title="http://us3.php.net/manual/en/function.echo.php" rel="nofollow" href="http://us3.php.net/manual/en/function.echo.php">http://us3.php.net/manual/en/function.echo.php</a></em></p>
<h2>Class declaration</h2>
<p>Classes must be named by following the <a class="external text" title="http://manual.cakephp.org/view/328/Cake-Conventions" rel="nofollow" href="http://manual.cakephp.org/view/328/Cake-Conventions">CakePHP naming conventions</a>, and must use <a class="external text" title="https://trac.cakephp.org/wiki/Developement/CodingStandards" rel="nofollow" href="https://trac.cakephp.org/wiki/Developement/CodingStandards">CamelCase</a> notation. There should always be a <em>single space</em> between the class name and the opening bracket, and the opening bracket must be on the same line as the class name. The closing bracket must be on its own line at the end of the class declaration.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyFlippinSweetClass <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Class member variables</h3>
<ul>
<li>Member variables must be named by following the variable <a class="external text" title="http://manual.cakephp.org/view/328/Cake-Conventions" rel="nofollow" href="http://manual.cakephp.org/view/328/Cake-Conventions">naming conventions</a>.</li>
<li>Any variables declared in a class must be listed at the top of the class, prior to declaring any functions.</li>
<li><strong>PHP 5+ only:</strong> The <em>var</em> construct is not permitted. Always declare the visibility of member variables by using one of the <em>private</em>, <em>protected</em>, or <em>public</em> constructs. Accessing member variables directly by making them public is permitted but discouraged in favor of accessor methods (typically prefixed with &#8220;set&#8221; or &#8220;get&#8221;).</li>
<li>All <em>protected</em> member variable names should begin with a single <em>underscore</em> (&#8220;_&#8221;), and all <em>private</em> member variable names should begin with <em>two underscores</em> (&#8220;__&#8221;). This is the only acceptable usage of underscores in a variable names. Member variables declared &#8220;public&#8221; may never start with an underscore.</li>
</ul>
<h3>Class member functions</h3>
<p>If using PHP 5+, we have the advantage of using the <em>public</em>, <em>protected</em>, and <em>private</em> constructs for declaring functions. For sake of convention and compliance with CakePHP, however; all <em>protected</em> member function names should begin with a single <em>underscore</em> (&#8220;_&#8221;), and all <em>private</em> member function names should begin with <em>two underscores</em> (&#8220;__&#8221;). This is the only acceptable usage of an underscore in a function name. Public member functions may never start with an underscore.</p>
<p>Additionally, The return value must not be enclosed in parentheses. This can hinder readability and can also break code if a method is later changed to return by reference.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$sVar1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A public string var'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_aVar2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'A'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'protected'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'array'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'var'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$__bVar3</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</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> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sVar1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _bar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_aVar2<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __fooBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__bVar3<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Class instantiation</h3>
<p>Always instantiate a class by using a variable of the same name:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$MyClass</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span></pre></div></div>

<p><strong>Note:</strong> Only variables representing objects may begin with a capital letter (as do class names). This helps to differentiate these declarations from other variable types.</p>
<h2>Function declaration</h2>
<p>Function names should be written using <em>camelBack</em> notation. There is no space between the function name and the opening parenthesis for the arguments. Like classes, there should always be a <em>single space</em> between the function name and the opening bracket, and the opening bracket must be on the same line as the function name. The closing bracket must be on its own line at the end of the function declaration.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> myFlippinSweetFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Additionally, functions should always be declared within a class, and should never stand alone. This provides a <a class="external text" title="http://en.wikipedia.org/wiki/Namespace" rel="nofollow" href="http://en.wikipedia.org/wiki/Namespace">namespace</a> for all functions and helps to identify their classification and location. For example, we would like to create a set of common functions that may be used throughout our application.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Common <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setGets<span style="color: #009900;">&#40;</span><span style="color: #000088;">$aGets</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$aGets</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$mGet</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$mGet</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$$mGet</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$$mGet</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$mGet</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> arraySearch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mFind</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aArr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aKeysFound</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aArr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$aArr</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$mKey</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$mVal</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mVal</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">arraySearch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mFind</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mVal</span><span style="color: #339933;">,</span> <span style="color: #000088;">$aKeysFound</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$mFind</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mVal</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$aKeysFound</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mKey</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$aKeysFound</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now that we have defined our functions within the class <em>Common</em>, we can access them in our application using the <a class="external text" title="http://php.mirrors.ilisys.com.au/manual/en/language.oop5.paamayim-nekudotayim.php" rel="nofollow" href="http://php.mirrors.ilisys.com.au/manual/en/language.oop5.paamayim-nekudotayim.php">scope resolution operator</a>, thus providing the proper namespace.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">Common<span style="color: #339933;">::</span><span style="color: #004000;">setGets</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sSearchStr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sSearchScope'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$aSearchRes</span> <span style="color: #339933;">=</span> Common<span style="color: #339933;">::</span><span style="color: #004000;">arraySearch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sSearchStr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$$sSearchScope</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Variable declaration</h2>
<p>Variable names should always begin with a <em>lowercase</em> key letter defining the data type, except for variables representing objects which begin with a <em>capital</em> letter. Object variables representing classes are the only variables that may begin with a capital letter, and they must match the class name. All variable names should be written in <em>camelBack</em> notation (or <em>CamelCase</em> for object variable names).</p>
<h3>Key letters</h3>
<p>The following key letters must be used to define variable data types:</p>
<ul>
<li>i = integer;</li>
<li>f = float;</li>
<li>s = string;</li>
<li>b = boolean;</li>
<li>a = array;</li>
<li>r = resource id / reference;</li>
<li>m = mixed.</li>
</ul>
<p><strong>Note:</strong> &#8220;Mixed&#8221; refers to any variable that may change data types during a request, such as inside a <em>foreach</em> loop.</p>
<h3>Examples</h3>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$iIntVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sStrVar</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bBoolVar</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$aArrVar</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'iKey1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$iIntVar</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'sKey2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$sStrVar</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'bKey3'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$bBoolVar</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$aArrVar</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$sKey</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$mVal</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$aNewArr</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$sKey</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mVal</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$MyClass</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #009900;">&#40;</span><span style="color: #000088;">$aNewArr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sFile</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'file.xml'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rHandle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sFile</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Variable names should also be descriptive, but as short as possible. Single character variable names are not permitted, except when used for incrementing / decrementing inside for and while loops. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    doSomething<span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>NOTE:</strong> The rules for defining variable names apply to naming <em>associative array keys</em> as well. An exception to this rule for <em>associative array keys</em> is when you intend to use the key names for literal output to the user agent.</p>
<h2>Control statements</h2>
<p>All control statements must have a single space before the opening parenthesis of the conditional, and a single space after the closing parenthesis.</p>
<h3>If / else / elseif statements</h3>
<p>For &#8220;if&#8221; statements that include &#8220;elseif&#8221; or &#8220;else&#8221;, the formatting must be as in these examples:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bool</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Switch statements</h3>
<p>Each &#8220;case&#8221; within the &#8220;switch&#8221; statement must be indented <em>4 spaces (1 tab)</em>. Content under each &#8220;case&#8221; statement must be indented an additional 4 spaces. The &#8220;default&#8221; construct may never be omitted from a switch statement. The &#8220;break&#8221; for each case should always align with its respective &#8220;case&#8221; declaration.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iNumPeople</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
        <span style="color: #b1b100;">print</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
        <span style="color: #b1b100;">print</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
        <span style="color: #b1b100;">print</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>NOTE:</strong> It is sometimes useful to write a case statement which falls through to the next case by not including a break or return in that case. To distinguish these cases from bugs, any case statement where break or return are omitted must contain the comment &#8220;// break intentionally omitted&#8221;.</p>
<h3>Ternary operator</h3>
<p>The <em>ternary operator</em> (?:) is permitted only when assigning values to variables, printing output, or returning values. It may not be used in place of <em>if / else</em> statements which define program logic. To enhance readability, always place parentheses around the conditional statement.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Proper use of the ternary operator</span>
<span style="color: #000088;">$bShowView</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sAction</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'view'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sAction</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'view'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'Viewing '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$sViewType</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'Welcome'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sAction</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'view'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'View'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'Default'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Improper use of the ternary operator</span>
<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sAction</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'edit'</span><span style="color: #009900;">&#41;</span> ? doAction<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'edit'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Including files</h2>
<p>When including files that are used only once in a single request (e.g. classes, libraries, configuration), use only and always the <a class="external text" title="http://us3.php.net/manual/en/function.require-once.php" rel="nofollow" href="http://us3.php.net/manual/en/function.require-once.php">require_once()</a> function. This prevents the accidental inclusion of a file more than once. Only use the <a class="external text" title="http://us3.php.net/manual/en/function.require.php" rel="nofollow" href="http://us3.php.net/manual/en/function.require.php">require()</a> (not include()) function for files that may be used more than once in the same request.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'config.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'classes.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;h2&gt;Welcome to our website!&lt;/h2&gt;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'disclaimer.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The above example illustrates requiring classes just once which then lend themselves to reuse by instantiating multiple objects, whereas the <em>require()</em> statement implies that the program output is consumable HTML.</p>
<h3>require() vs. include()</h3>
<p>The <a class="external text" title="http://us3.php.net/manual/en/function.require.php" rel="nofollow" href="http://us3.php.net/manual/en/function.require.php">require()</a> and <a class="external text" title="http://us3.php.net/manual/en/function.require-once.php" rel="nofollow" href="http://us3.php.net/manual/en/function.require-once.php">require_once()</a> functions are always preferred over the <a class="external text" title="http://us3.php.net/manual/en/function.include.php" rel="nofollow" href="http://us3.php.net/manual/en/function.include.php">include()</a> and <a class="external text" title="http://us3.php.net/manual/en/function.include-once.php" rel="nofollow" href="http://us3.php.net/manual/en/function.include-once.php">include_once()</a> functions. <em>require()</em> and <em>require_once()</em> will generate a fatal error and prevent the script from continuing to run if the specified file is not found, which in most cases would be a desirable outcome since the rest of the script will likely not run correctly without that file. <em>include()</em> and <em>include_once()</em> will only generate a warning if the file is missing and PHP will attempt to run the remainder of the script. <em>require()</em> and <em>require_once()</em> should be used in all cases so that it is always evident when an included file is missing (ideally during development), even if warnings are suppressed.</p>
<h2>Constants</h2>
<p>Constants should be defined in capital letters with words separated by an underscore &#8220;_&#8221;. Each constant should begin with an appropriate namespace to help derive its meaning:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'COMPANY_NAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ABC, Inc.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'COMPANY_ABBR'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ABC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PHONE_MAIN'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'800-555-5555'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PHONE_FAX'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'800-555-4444'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>While you should try and keep constant names short, always ensure that the names are unique enough to prevent collisions with other constants defined in any third party plugins.</p>
<h2>Documentation formatting / comments</h2>
<p>All documentation blocks (&#8220;docblocks&#8221;) must be compatible with the <a class="external text" title="http://phpdoc.org/" rel="nofollow" href="http://phpdoc.org/">phpDocumentor</a> format.</p>
<p>All source code files written for the framework or that operate within the framework must contain a &#8220;file-level&#8221; docblock at the top of each file and a &#8220;class-level&#8221; docblock immediately above each class. Below are examples of such docblocks.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * @copyright  2007 ABC, Inc.
 * @author     jdeveloper
 * @version    SVN: $Id:$
 * @link       http://intranet/docs/file_name
 * @since      File available since Release 1.2.0
 */</span></pre></div></div>

<p>PhpDoc tags are very much like JavaDoc tags in Java. Tags are only processed if they are the first thing in a DocBlock line, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Tag example.
 * @author this tag is parsed, but this @version is ignored
 * @version 1.0 this tag is also parsed
 */</span></pre></div></div>

<p>For docblocks within the CakePHP framework, always include the appropriate package, subpackage, and SVN keywords ($) as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * @package      abc
 * @subpackage   abc.controllers
 * @author       jdeveloper
 * @version      $Revision$
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date$
 */</span></pre></div></div>

<p><strong>Note:</strong> The $ SVN keywords will be automatically updated by Subversion when the svn:keywords property is added to the file. Whenever creating a new PHP file to add to the Subversion repository, be sure to add the property like so:</p>
<pre><strong>In Shell:</strong>
svn propset svn:keywords "HeadURL Revision LastChangedBy Date" file_name.php

<strong>In Eclipse PDT (with Subversive):</strong>
1. Right click file -> Team -> Set Property...
2. Enter Property name: "svn:keywords"
3. Enter Property content: "HeadURL Revision LastChangedBy Date"</pre>
<p>If a function / method may throw an exception, use <em>@throws</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @throws exception [description]
 */</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://klauzinski.com/php/php-coding-guidelines/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows Vista Woes</title>
		<link>http://klauzinski.com/technology/windows-vista-woes</link>
		<comments>http://klauzinski.com/technology/windows-vista-woes#comments</comments>
		<pubDate>Wed, 07 Feb 2007 01:51:33 +0000</pubDate>
		<dc:creator>Philip Klauzinski</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows vista]]></category>

		<guid isPermaLink="false">http://klauzinski.com/wordpress/?p=1</guid>
		<description><![CDATA[I recently caught a glimpse of Windows Vista at a local Best Buy while shopping for a new notebook computer. To say I was unimpressed would be an understatement. Aside from a few UI updates and some fancy transparent window effects, Windows Vista is really not all that different from Windows XP. In fact, much [...]]]></description>
			<content:encoded><![CDATA[<p>I recently caught a glimpse of Windows Vista at a local Best Buy while shopping for a new notebook computer. To say I was <em>unimpressed</em> would be an understatement.</p>
<p>Aside from a few <abbr title="User Interface">UI</abbr> updates and some fancy transparent window effects, Windows Vista is really not all that different from Windows XP. In fact, much has evidenced that <a rel="external" href="http://www.tomshardware.com/2007/01/29/xp-vs-vista/">Windows Vista is lacking in software support</a> as compared to Windows XP.</p>
<p>And Microsoft certainly wasn&#8217;t shy about <a rel="external" href="http://www.eweek.com/article2/0,1895,1842175,00.asp">copying ideas</a> from <a rel="external" href="http://www.apple.com/macosx/">Mac OS X</a>. Vista features a search-as-you-type input from its <a rel="external" href="http://www.bentuser.com/article.aspx?ID=332&amp;page=1">start menu</a>, with the same functionality and a strikingly similar search icon to the <a rel="external" href="http://www.apple.com/macosx/features/spotlight/">Mac OS X Spotlight</a> feature. Vista&#8217;s newly entitled<em> Window Switcher</em> feature, accessible via Start-Tab, also closely resembles <a href="http://www.apple.com/macosx/features/expose/">Mac OS X Expose</a>. And the list goes on. <span id="more-1"></span></p>
<h3>What is so wrong about taking ideas from Apple&#8217;s cutting edge operating system?</h3>
<p>Nothing at all. But my point here is that from what I have seen, Windows Vista doesn&#8217;t have anything that I haven&#8217;t already seen in OS X, or in any modern web application for that matter, and Vista doesn&#8217;t do anything to improve upon those features.</p>
<p>Actually, it seems that Microsoft can barely keep up with the competition these days, in both technology and web standards; the latter being supported by its crippled release of IE7 to the world. Microsoft has also announced that its release of <a href="http://www.campaignmonitor.com/blog/archives/2007/01/microsoft_takes_email_design_b.html">Outlook 2007 will support <em>fewer</em> standards than its predecessors</a>, back to the 2000 version. This is due to their decision to stop using <em>Internet Explorer</em> to render HTML emails and instead rely solely upon the archaic and proprietary <em>Microsoft Word</em> rendering engine.</p>
<h3>Aero Glass is just a pretty face</h3>
<p>Just because Microsoft&#8217;s new interface, dubbed <em>Aero Glass</em>, looks and feels a lot like OS X doesn&#8217;t mean its engine runs just as smoothly. Windows Vista is so resource intensive, it requires a <em>minimum</em> of a 1 <abbr title="Gigahertz">GHz</abbr> <abbr title="Central Processing Unit">CPU</abbr>, 1 <abbr title="Gigabyte">GB</abbr> of <abbr title="Random Access Memory">RAM</abbr>, a 40 GB hard drive with at least 15 GB free, and 128 <abbr title="Megabytes">MB</abbr> of graphics memory, according to a <a href="http://www.macdailynews.com/index.php/weblog/comments/12468/">MacDailyNews article</a>.</p>
<p>Most of us don&#8217;t even own or are barely beginning to buy computers with this kind of power, especially in the realm of 1 GB of RAM, which is only recently becoming a standard minimum among new computers. Now it will <em>have</em> to be the minimum.</p>
<h3>Why does Microsoft keep doing this to us?</h3>
<p>Because they can. The grip that this company has on the market share is unbelievable. So much in fact, that when I recently attempted to purchase a new laptop, I couldn&#8217;t find <em>one</em> modern notebook model that wasn&#8217;t out of stock, online or in the store. As I learned, this was due to manufacturers around the world halting production in anticipation of the Windows Vista release.</p>
<p>It used to be that Microsoft was an innovator, a pioneer, and boasted itself as such. Today, this company is struggling to even step on the heels of <a href="http://www.apple.com/">Apple</a>, a company that is doing everything right. Still, Microsoft will never cease to amaze us &#8212; but when will it be in a good way? Time will tell.</p>
]]></content:encoded>
			<wfw:commentRss>http://klauzinski.com/technology/windows-vista-woes/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

