<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/css/rss20.xsl" type="text/xsl"?>
<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/" xmlns:pheedo="http://www.pheedo.com/namespace/pheedo">
	<channel>
		<title>SitePoint &#187; PHP</title>
		<atom:link href="http://www.sitepoint.com/blogs/category/php/feed/" rel="self" type="application/rss+xml"/>
		<link>http://www.sitepoint.com/blogs</link>
		<description>News, opinion, and fresh thinking for web developers and designers. The official podcast of sitepoint.com.</description>
		<lastBuildDate>Wed, 10 Feb 2010 01:44:47 +0000</lastBuildDate>
		<generator>http://wordpress.org/?v=2.8.6</generator>
		<language>en</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
		<item>
			<title>Debug PHP with Firebug and FirePHP</title>
			<link>http://www.pheedcontent.com/click.phdo?i=54c354de229fedfd8c5cb67d2a8992cb</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2010/02/09/debug-php-firebug-firephp/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2010/02/09/debug-php-firebug-firephp/#comments</comments>
			<pubDate>Tue, 09 Feb 2010 06:07:33 +0000</pubDate>
			<dc:creator>Louis Simoneau</dc:creator>
			<category><![CDATA[PHP]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=18058</guid>
			<description><![CDATA[If you're anything like me, you'd sooner forgo water than Firebug when working on a web project. The little ’bug is a fantastically useful HTML/CSS/JavaScript/Ajax debugger. But did you know it can also be used to debug PHP? Yes, thanks to an additional Firefox extension called FirePHP.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2005/02/23/user-errors-in-php/' rel='bookmark' title='Permanent Link: User Errors in PHP'>User Errors in PHP</a></li><li><a href='http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/' rel='bookmark' title='Permanent Link: Serializing PHP data structures for Javascript'>Serializing PHP data structures for Javascript</a></li><li><a href='http://www.sitepoint.com/blogs/2008/05/13/useful-in-browser-development-tools-for-php/' rel='bookmark' title='Permanent Link: Useful in-browser development tools for PHP'>Useful in-browser development tools for PHP</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=54c354de229fedfd8c5cb67d2a8992cb&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=54c354de229fedfd8c5cb67d2a8992cb&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2010/02/firephp.png" alt="" title="Feature" width="284" height="252" class="imgright" />If you&#8217;re anything like me, you&#8217;d sooner forgo water than <a href="http://getfirebug.com">Firebug</a> when working on a web project. The little ’bug is a fantastically useful HTML/CSS/JavaScript/Ajax debugger. But did you know it can also be used to debug PHP? Yes, thanks to an additional Firefox extension called FirePHP.</p>
<p>By combining this extension, which sits on top of Firebug, with a server-side library, your PHP scripts will be able to send debugging information to the browser, handily encoded in the HTTP response headers. Once you&#8217;re set up, you can log warnings and errors in your PHP scripts to the Firebug console, just as if you were developing JavaScript.</p>
<p>To start, you first need to <a href="https://addons.mozilla.org/en-US/firefox/addon/6149">install the FirePHP extension from Mozilla&#8217;s Firefox Add-ons site</a>. This requires that you already have Firebug installed. Once FirePHP is installed, when you next open your Firebug panel, you&#8217;ll now see an additional blue bug. Click on that bug and a menu will appear allowing you to enable or disable FirePHP:</p>
<p><img class="size-full wp-image-18061" title="FirePHP Menu" src="http://www.sitepoint.com/blogs/wp-content/uploads/2010/02/Screen-shot-2010-02-09-at-1.59.56-PM.png" alt="FirePHP Menu" width="255" height="275" /></p>
<div id="adz" class="vertical"></div><p>This, of course, won&#8217;t do anything yet. You also need to install the FirePHP server-side library, which is <a href="http://www.firephp.org/HQ/Install.htm">available here</a>. This is a stand-alone version of the library that can either be downloaded manually or installed using PEAR. After that, you simply need to include the library in your code. There are also versions designed to integrate with various frameworks or content management systems, such as the <a href="http://wordpress.org/extend/plugins/wp-firephp/">WP-FirePHP plugin for WordPress</a> or the <a href="http://joomlacode.org/gf/project/kunena/frs/?action=FrsReleaseView&amp;release_id=11823">JFirePHP plugin for Joomla</a>. For the sake of this walk-through, I&#8217;ll focus on the stand-alone functionality.</p>
<p>Once you have the FirePHP library on your server, you need to include it in your script with a line like:</p>
<code>require_once('FirePHPCore/fb.php');</code>
<p>Because FirePHP sends its logging data via the HTTP headers, you&#8217;ll need to buffer your script&#8217;s output so that the response headers can include content generated further down the script. In PHP, this is accomplished by calling <code>ob_start</code> near the top of your script:</p>
<code>ob_start();</code>
<p>With these steps done, you can start using FirePHP. All you need to do is call the <code>fb</code> function with whatever you&#8217;d like to log, along with an optional label and an optional constant to define the message as a standard log, a warning, an error, or information. For example:</p>
<pre><code>$var = array('a'=&gt;'pizza', 'b'=&gt;'cookies', 'c'=&gt;'celery');
fb($var);
fb($var, "An array");
fb($var, FirePHP::WARN);
fb($var, FirePHP::INFO);
fb($var, 'An array with an Error type', FirePHP::ERROR);
</code></pre>
<p>This code will produce the following output in the Firebug console:</p>
<p><img class="size-full wp-image-18073" title="FirePHP Console Output" src="http://www.sitepoint.com/blogs/wp-content/uploads/2010/02/Screen-shot-2010-02-09-at-2.39.22-PM.png" alt="FirePHP Console Output" width="555" height="125" /></p>
<p>You can also use FirePHP to give you a trace of your application&#8217;s execution: by passing in the <code>FirePHP::TRACE</code> constant, you&#8217;ll get to see the line number, class name, and function name from within which <code>fb</code> was called. So this code:</p>
<pre><code>
function hello() {
  fb('Hello World!', FirePHP::TRACE);
}
function greet() {
  hello();
}
greet();
</code></pre>
<p>Will produce an output as follows:</p>
<p><img class="size-full wp-image-18082" title="FirePHP Trace Output" src="http://www.sitepoint.com/blogs/wp-content/uploads/2010/02/Screen-shot-2010-02-09-at-3.00.40-PM.png" alt="FirePHP Trace Output" width="375" height="143" /></p>
<p>This trace functionality can be fantastic for debugging more involved scripts, as it lets you know exactly from where your functions are being called. </p>
<p>Of course, you need to remember to remove your debugging statements before your code goes live! </p>
<p>There&#8217;s a lot more to FirePHP than what I&#8217;ve covered here. I&#8217;ve been showing you the simplified procedural API for FirePHP, but there&#8217;s a more advanced object-oriented API available with a number of additional features. You can learn all about it on the <a href="http://www.firephp.org/HQ/Use.htm">FirePHP site</a>, so be sure to check it out. </p>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2005/02/23/user-errors-in-php/' rel='bookmark' title='Permanent Link: User Errors in PHP'>User Errors in PHP</a></li><li><a href='http://www.sitepoint.com/blogs/2004/04/22/serializing-php-data-structures-for-javascript/' rel='bookmark' title='Permanent Link: Serializing PHP data structures for Javascript'>Serializing PHP data structures for Javascript</a></li><li><a href='http://www.sitepoint.com/blogs/2008/05/13/useful-in-browser-development-tools-for-php/' rel='bookmark' title='Permanent Link: Useful in-browser development tools for PHP'>Useful in-browser development tools for PHP</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=54c354de229fedfd8c5cb67d2a8992cb&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=54c354de229fedfd8c5cb67d2a8992cb&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2010/02/09/debug-php-firebug-firephp/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		</item>
		<item>
			<title>How to Split WordPress Content Into Two or More Columns</title>
			<link>http://www.pheedcontent.com/click.phdo?i=467b5cd681ce2324127adf4f92e069df</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2010/02/04/split-wordpress-content-into-multiple-sections/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2010/02/04/split-wordpress-content-into-multiple-sections/#comments</comments>
			<pubDate>Thu, 04 Feb 2010 09:55:30 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category><![CDATA[columns]]></category>
			<category><![CDATA[content]]></category>
			<category><![CDATA[Design]]></category>
			<category><![CDATA[split]]></category>
			<category><![CDATA[theme]]></category>
			<category><![CDATA[wordpress]]></category>
			<category>columns</category>
			<category>content</category>
			<category>design</category>
			<category>split</category>
			<category>theme</category>
			<category>wordpress</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=17359</guid>
			<description><![CDATA[Craig provides a useful technique which splits WordPress page or post content into multiple sections. You can add it to any theme and do not need a plugin.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2006/06/23/the-power-of-stringprototypesplit-almost/' rel='bookmark' title='Permanent Link: The power of String.prototype.split() &#8230; almost'>The power of String.prototype.split() &#8230; almost</a></li><li><a href='http://www.sitepoint.com/blogs/2005/08/18/the-wordpress-security-update/' rel='bookmark' title='Permanent Link: The WordPress Security Update'>The WordPress Security Update</a></li><li><a href='http://www.sitepoint.com/blogs/2008/11/11/how-to-expose-phps-private-parts/' rel='bookmark' title='Permanent Link: How to Expose PHP&#8217;s Private Parts'>How to Expose PHP&#8217;s Private Parts</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=467b5cd681ce2324127adf4f92e069df&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=467b5cd681ce2324127adf4f92e069df&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://blogs.sitepointstatic.com/images/tech/246-wp-split-content.png" width="200" height="200" alt="split WordPress content" class="imgright" />WordPress is a great <acronym title="Content Management System">CMS</acronym>, but implementing some features within your theme can require a little lateral thinking. The content for your page or post is usually output by the theme code using a single function call:</p>
<code>the_content(<em>args</em>);</code>
<p>But what if you need to split the content into two or more blocks? That might be necessary if your theme requires multiple columns or sections on the page. WordPress provides a <code>get_the_content()</code> function to return content as a PHP variable, but how do you determine where the divisions occur? There are a few solutions on the web, but most involve either:</p>
<ol>
<li>Splitting the content at HTML tags such as <code>h2</code> headings. Unfortunately, that requires the content author to know a little HTML and it&#8217;s not very versatile &#8212; you couldn&#8217;t allow two headings in one column.</li>
<li>Using a WordPress shortcode. That&#8217;s more flexible, but it still puts the onus on the content editor to remember and use the right code.</li>
</ol>
<p>The WordPress <code>&lt;!--more--&gt;</code> tag may offer a better solution. It&#8217;s normally used to split a long article into two or more pages, but not all themes use that facility and it only works for WordPress posts by default (not pages). Using the <code>&lt;!--more--&gt;</code> tag offers several advantages:</p>
<div id="adz" class="horizontal"></div><ul>
<li>A &#8220;more&#8221; toolbar button is available in both the visual and HTML editing pane.</li>
<li>Divisions can be placed anywhere in the content.</li>
<li>It&#8217;s easy for non-technical users to understand how the content will be split.</li>
</ul>
<p><img src="http://blogs.sitepointstatic.com/images/tech/246-wp-split-content-screen.png" width="465" height="309" alt="WordPress editing pane showing 'more' button" class="imgcenter" style="display:block;margin:15px auto;" /></p>
<p>To split your content, locate your theme folder (wp-content/themes), edit or create a <strong>functions.php</strong> file and add the following function within a &lt;?php &hellip; ?&gt; block:</p>
<pre><code class="php">
// split content at the more tag and return an array
function split_content() {

	global $more;
	$more = true;
	$content = preg_split('/&lt;span id=&quot;more-\d+&quot;&gt;&lt;\/span&gt;/i', get_the_content('more'));
	for($c = 0, $csize = count($content); $c &lt; $csize; $c++) {
		$content[$c] = apply_filters('the_content', $content[$c]);
	}
	return $content;

}
</code></pre>
<p>You now need to locate the theme files which call <code>the_content()</code> within the <a href="http://codex.wordpress.org/The_Loop">WordPress loop</a>. You should find it in <strong>single.php</strong> and <strong>page.php</strong> since these are used to display single posts and pages respectively. It may also be found in <strong>index.php</strong>, <strong>archive.php</strong> and <strong>search.php</strong>, however, these normally show more than one article so be careful how multiple content blocks are handled.</p>
<p>Once you&#8217;ve found the relevant code, comment out <code>the_content()</code> and call the <code>split_content()</code> function. It returns the content as an array; each element contains a single content block split at the <code>&lt;!--more--&gt;</code> tag, e.g. $content[0], $content[1], $content[2] &#8230; etc. The HTML can then be output as required, e.g.</p>
<pre><code class="php">
< ?php
// original content display
// the_content('&lt;p&gt;Read the rest of this page &amp;raquo;&lt;/p&gt;');

// split content into array
$content = split_content();

// output first content sections in column1
echo '&lt;div id=&quot;column1&quot;&gt;', $content[0], '&lt;/div&gt;';

// output remaining content sections in column2
echo '&lt;div id=&quot;column2&quot;&gt;', implode(array_shift($content)), '&lt;/div&gt;';
?>
</code></pre>
<p>I hope you find it useful.</p>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=horizontal" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2006/06/23/the-power-of-stringprototypesplit-almost/' rel='bookmark' title='Permanent Link: The power of String.prototype.split() &#8230; almost'>The power of String.prototype.split() &#8230; almost</a></li><li><a href='http://www.sitepoint.com/blogs/2005/08/18/the-wordpress-security-update/' rel='bookmark' title='Permanent Link: The WordPress Security Update'>The WordPress Security Update</a></li><li><a href='http://www.sitepoint.com/blogs/2008/11/11/how-to-expose-phps-private-parts/' rel='bookmark' title='Permanent Link: How to Expose PHP&#8217;s Private Parts'>How to Expose PHP&#8217;s Private Parts</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=467b5cd681ce2324127adf4f92e069df&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=467b5cd681ce2324127adf4f92e069df&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2010/02/04/split-wordpress-content-into-multiple-sections/feed/</wfw:commentRss>
			<slash:comments>13</slash:comments>
		</item>
		<item>
			<title>Boost your PHP Performance 50% with HipHop</title>
			<link>http://www.pheedcontent.com/click.phdo?i=985ee2811252a2c51c17efc1f4c8be6c</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2010/02/04/boost-php-performance-with-hiphop/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2010/02/04/boost-php-performance-with-hiphop/#comments</comments>
			<pubDate>Thu, 04 Feb 2010 04:58:47 +0000</pubDate>
			<dc:creator>Mal Curtis</dc:creator>
			<category><![CDATA[PHP]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=17853</guid>
			<description><![CDATA[Facebook's soon-to-be-released PHP "code transformer," dubbed HipHop, promises performance gains of up to 50% by transforming PHP source code into compiled C++. Mal gives us the lowdown on this exciting development in the PHP world.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/04/21/free-php-webinar/' rel='bookmark' title='Permanent Link: Free PHP Webinar: How to Increase Performance with Caching'>Free PHP Webinar: How to Increase Performance with Caching</a></li><li><a href='http://www.sitepoint.com/blogs/2005/01/11/oop-and-performance/' rel='bookmark' title='Permanent Link: OOP and Performance'>OOP and Performance</a></li><li><a href='http://www.sitepoint.com/blogs/2009/09/08/free-performance-with-mysql-table-types/' rel='bookmark' title='Permanent Link: Free Performance with MySQL Table Types'>Free Performance with MySQL Table Types</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=985ee2811252a2c51c17efc1f4c8be6c&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=985ee2811252a2c51c17efc1f4c8be6c&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img class="imgright" title="HipHop" src="http://www.sitepoint.com/blogs/wp-content/uploads/2010/02/HipHop_logo_white2.png" alt="HipHop" width="211" height="211" />No, I&#8217;m not talking about a break-dance move that will magically give you extra PHP performance. I&#8217;m referring, rather, to a new open source project announced by Facebook. HipHop is a &#8220;source code transformer&#8221; designed to reduce CPU and memory usage by transforming your PHP code into C++ code.</p>
<p>To understand why this helps, you need to know a little bit about how PHP works. PHP is a scripting language, like Ruby, Python, and Perl. This is great for developer productivity, as there&#8217;s no need for the  code to be compiled. The trade-off for this flexibility is in performance, with a scripting language requiring more CPU power and memory than a compiled language such as C++. What HipHop does is transform your PHP code into true C++ code, which can then be compiled into machine code. This is not the sort of thing you would do while developing, but when you are deploying your work you can compile it into the superior C++ code.</p>
<p><strong>HipHop Performance Boosts:</strong></p>
<ul>
<li>Web &#8212; 50% less CPU usage with the same traffic</li>
<li>API &#8212; 30% less CPU usage with double the traffic</li>
</ul>
<div id="adz" class="vertical"></div><p>The performance boosts that Facebook has seen is based on its own real-world experience, with over 90% of Facebook servers being converted to HipHop code in the last six months. The benefits released were split into two categories, the Web tier and API tier, and the benefits witnessed for each. The web tier reduced 50% of server load with the same amount of connections, and the API tier saw a 30% decrease, even with a twofold increase in traffic. The difference is explained by the web layer requiring more presentation logic and more abstract information (related information) than the API tier.</p>
<p><strong>What&#8217;s the catch?</strong><br />
You&#8217;ll have to avoid a few PHP functions, such as <code>eval()</code> and <code>create_function()</code>. Because of the dynamic nature of these functions, the HipHop team was unable to transform them into C++ code; however, these functions are rarely used in most PHP applications in general.</p>
<p>Currently your HipHop code has to run through a custom-written HTTP server bundled with HipHop; however, using HipHop code with Apache is high on the development road map.</p>
<p>While HipHop will increase the performance of your PHP code, it&#8217;s not necessarily going to be the silver bullet for your performance woes; for example, if your bottleneck is in your database performance, HipHop will be unable to help you. There&#8217;s no need to worry about it making your code slower though, as the entire PHP runtime has been emulated in a way that even the most complicated code will run at the same speed as pure PHP code.</p>
<p><strong>Where can I find it?</strong><br />
HipHop will be released very soon to the open source community, but in the meantime you can read the full write-up on the <a href=" http://developers.facebook.com/news.php?blog=1&amp;story=358">Facebook blog</a>, or view the <a href="http://www.ustream.tv/recorded/4409735">Facebook presentation</a>.</p>
<p><strong>What do you think?</strong><br />
I&#8217;m interested in doing some benchmarks and seeing how HipHop performs with the SitePoint codebase. Do you think you&#8217;ll be checking it out when it&#8217;s released?</p>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/04/21/free-php-webinar/' rel='bookmark' title='Permanent Link: Free PHP Webinar: How to Increase Performance with Caching'>Free PHP Webinar: How to Increase Performance with Caching</a></li><li><a href='http://www.sitepoint.com/blogs/2005/01/11/oop-and-performance/' rel='bookmark' title='Permanent Link: OOP and Performance'>OOP and Performance</a></li><li><a href='http://www.sitepoint.com/blogs/2009/09/08/free-performance-with-mysql-table-types/' rel='bookmark' title='Permanent Link: Free Performance with MySQL Table Types'>Free Performance with MySQL Table Types</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=985ee2811252a2c51c17efc1f4c8be6c&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=985ee2811252a2c51c17efc1f4c8be6c&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2010/02/04/boost-php-performance-with-hiphop/feed/</wfw:commentRss>
			<slash:comments>23</slash:comments>
		</item>
		<item>
			<title>Introducing pearhub</title>
			<link>http://www.pheedcontent.com/click.phdo?i=d076afb692c49afeeb21ef8df5bd1cb0</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2010/01/08/introducing-pearhub/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2010/01/08/introducing-pearhub/#comments</comments>
			<pubDate>Thu, 07 Jan 2010 22:35:05 +0000</pubDate>
			<dc:creator>Troels Knak-Nielsen</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category>php pear git svn</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=16992</guid>
			<description><![CDATA[pearhub.org provides a place where you can register a project, that is hosted on Github, Google code or similar.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2008/04/03/subversion-gits-the-last-train/' rel='bookmark' title='Permanent Link: Subversion gits the last train'>Subversion gits the last train</a></li><li><a href='http://www.sitepoint.com/blogs/2008/07/24/github-gist-is-pastie-on-steroids/' rel='bookmark' title='Permanent Link: GitHub Gist is Pastie on Steroids'>GitHub Gist is Pastie on Steroids</a></li><li><a href='http://www.sitepoint.com/blogs/2008/04/14/2424/' rel='bookmark' title='Permanent Link: Ruby On Rails Linkage: GitHub, Passenger and more&#8230;'>Ruby On Rails Linkage: GitHub, Passenger and more&#8230;</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=d076afb692c49afeeb21ef8df5bd1cb0&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=d076afb692c49afeeb21ef8df5bd1cb0&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p>Happy New Year.</p>
<p>In the previous year, I have spent a great deal of time with Ruby on Rails, and one of the things that works better in the Ruby world is the package distribution system (gems, as they are called). A lot of Ruby developers make their libraries available on <a href="http://github.com/">github</a>, which until recently had an integrated feature to publish the code as a gem. Recently they discontinued this service and instead a dedicated site, <a href="http://gemcutter.org/">gemcutter</a> emerged. Gemcutter is strictly a package hosting service/channel.</p>
<p>I think services like these are an important reason why gems are so popular amongst Ruby developers, and I figured that PHP really needs something similar. So over the Christmas period, I began brewing a service, which is now stable enough that I&#8217;ll make it available to the community at large.</p>
<p><a href="http://pearhub.org/">pearhub.org</a> provides a place where you can register a project that is hosted on Github, Google code or similar (Currently only git and subversion are supported). The service will generate a PEAR package and put it on a PEAR channel. This means that installation and upgrade can be done from the PEAR installer. New releases are made by making a tag in the linked repository. Pearhub will check routinely for any new tags, and generate a new package if so.</p>
<div id="adz" class="horizontal"></div><p>I hope you&#8217;ll find it useful, and feedback is of course welcome.</p>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=horizontal" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2008/04/03/subversion-gits-the-last-train/' rel='bookmark' title='Permanent Link: Subversion gits the last train'>Subversion gits the last train</a></li><li><a href='http://www.sitepoint.com/blogs/2008/07/24/github-gist-is-pastie-on-steroids/' rel='bookmark' title='Permanent Link: GitHub Gist is Pastie on Steroids'>GitHub Gist is Pastie on Steroids</a></li><li><a href='http://www.sitepoint.com/blogs/2008/04/14/2424/' rel='bookmark' title='Permanent Link: Ruby On Rails Linkage: GitHub, Passenger and more&#8230;'>Ruby On Rails Linkage: GitHub, Passenger and more&#8230;</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=d076afb692c49afeeb21ef8df5bd1cb0&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=d076afb692c49afeeb21ef8df5bd1cb0&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2010/01/08/introducing-pearhub/feed/</wfw:commentRss>
			<slash:comments>12</slash:comments>
		</item>
		<item>
			<title>Build a Lifestream with SimplePie</title>
			<link>http://www.pheedcontent.com/click.phdo?i=61fffb40d01863049515eef97d10df33</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/09/29/build-a-lifestream-with-simplepie/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/09/29/build-a-lifestream-with-simplepie/#comments</comments>
			<pubDate>Tue, 29 Sep 2009 09:11:54 +0000</pubDate>
			<dc:creator>raena</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category><![CDATA[Web Tech]]></category>
			<category><![CDATA[rss]]></category>
			<category><![CDATA[simplepie]]></category>
			<category><![CDATA[Tutorials]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=14555</guid>
			<description><![CDATA[If you're anything like Raena, you have bits and pieces of your online life scattered absolutely everywhere — photos, comments, tweets, blogs, favorite links — stashed away in all kinds of profiles. Wouldn’t it be nice to aggregate all those on one page? Find out how in this introduction to the SimplePie feed parser.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/' rel='bookmark' title='Permanent Link: SimpleXML and namespaces'>SimpleXML and namespaces</a></li><li><a href='http://www.sitepoint.com/blogs/2004/04/23/new-article-build-an-rss-datalist-control-in-aspnet/' rel='bookmark' title='Permanent Link: New Article: Build an RSS DataList Control in ASP.NET'>New Article: Build an RSS DataList Control in ASP.NET</a></li><li><a href='http://www.sitepoint.com/blogs/2009/03/03/build-a-buzzword-bingo-card-in-php/' rel='bookmark' title='Permanent Link: Build a Buzzword Bingo Card in PHP'>Build a Buzzword Bingo Card in PHP</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=61fffb40d01863049515eef97d10df33&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=61fffb40d01863049515eef97d10df33&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img class=" imgright size-thumbnail wp-image-14559" title="A berry pie" src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/09/290909_red_current_pie-150x150.jpg" alt="A berry pie" width="150" height="150" />If you&#8217;re like me, you have bits and pieces of your online life scattered absolutely everywhere &#8212; photos, comments, tweets, blogs, favorite links &#8212; stashed away in all kinds of profiles. And, of course, many of those profiles offer RSS feeds, ripe for the mashing. Wouldn&#8217;t it be nice to aggregate all those on one page? I think so!</p>
<p>In this tutorial we&#8217;ll build a page that gathers up the RSS feeds of all those little bits of your life and presents them all together in one spot. To do that, we&#8217;ll use SimplePie, a feed parsing class written in PHP. It&#8217;s powerful, it&#8217;s open source, and it&#8217;s easy to use. We&#8217;ll then hack on the output a little, and make it all look gorgeous with HTML and CSS. The techniques we&#8217;ll cover are also useful if you find that you need to aggregate RSS from many sources, like a news page. Let&#8217;s begin!</p>
<h2>What You&#8217;ll Need</h2>
<ul>
<li> The <a href="http://simplepie.org">SimplePie</a> library.</li>
<li> A web server that&#8217;s capable of running it &#8212; there&#8217;s a compatibility test in the SimplePie package that will tell you whether your server is good to go.</li>
<li> If you tweet, <a href="http://www.snipe.net/2009/09/php-twitter-clickable-links/">grab this function by Alison Gianotto</a> to help auto-link those tweets</li>
<li> Of course, you&#8217;ll need your trusty text editor!</li>
<li> For brevity, some of the code is excluded from this tutorial. If you want to see the full picture, <a href="http://sitepoint.com/examples/lifestream/lifestream.phps">view the completed example</a> or <a href="http://sitepoint.com/examples/lifestream/lifestream.phps.zip">download it.</a></li>
</ul>
<div id="adz" class="vertical"></div><h2>Let&#8217;s Begin</h2>
<p>First, of course, we&#8217;ll need to identify the feeds we want to include on the page. Like I said, I have profiles in lots of places, but for now I&#8217;ll keep it short and sweet &#8212; I&#8217;ll grab my blog, <a href="http://twitter.com/">Twitter</a>, <a href="http://flickr.com/">Flickr</a> photos, and favorited tracks from <a href="http://last.fm/">Last.fm.</a></p>
<p>You&#8217;ll need to gather up the URLs for the feeds you want, and stash them someplace safe. To do that, look for RSS icons on the page or in your browser&#8217;s address bar. Paste those URLs in a text file for later reference.</p>
<p>Next, we&#8217;ll need to check that SimplePie is compatible with our hosting environment. There&#8217;s plenty of stuff in this download &#8212; for now, we can just upload the <code>compatibility_test</code> directory in a web-accessible location on the server, and take a look at it in a web browser. Hopefully, there&#8217;ll be a series of messages informing you that a number of dependencies are enabled. If so, it&#8217;s time to upload the rest of the library. (If you have a folder in your download called <code>test</code>, it&#8217;s safe to exclude that one.)</p>
<p>While you&#8217;re poking around in your web server, now&#8217;s a good time to set up a directory for SimplePie to use as a cache. You should make this directory writable. I&#8217;ve chosen <code>cache</code>.</p>
<p>Make a note of where you uploaded SimplePie, and your cache directory. In my case, both are in the same directory as where I plan to put my lifestream page.</p>
<h2>Prepare Your Feeds</h2>
<p>Let&#8217;s grab those feeds! Open up a new text editor and begin a new PHP file, which we&#8217;ll call <code>lifestream.php</code>. This will be the page that eventually shows our lifestream.</p>
<p>First, let&#8217;s include a very important component &#8212; the SimplePie library itself:</p>
<pre><code>&lt;?php
  require_once('./simplepie/simplepie.inc');</code></pre>
<p>Now, we need to define a couple of important variables: the first is to define the cache location, and the second is to specify the amount of time (in seconds) we want to cache the feeds. Hitting feeds repeatedly is rude, and some feed providers will bar you for excessive feed requests. Also, if you have a busy site, it&#8217;s sensible to keep your resource consumption low. I&#8217;ve chosen thirty minutes, or 1800 seconds, which is a nice tradeoff between freshness and politeness. Here are those variables:</p>
<pre><code>$cache = "./cache";
$duration = 1800;</code></pre>
<p>Next, time to create some <code>SimplePie</code> objects. The easiest way to turn a feed into a <code>SimplePie</code> object is to <a href="http://simplepie.org/wiki/reference/simplepie/start">call it in its short form:</a> pass it a feed URL, the location of your cache directory, and the cache duration. All my objects look roughly like this:</p>
<pre><code>$object = new SimplePie(
 "http://example.com/feed.xml",
  $cache, $duration);</code></pre>
<p>You&#8217;ll see I&#8217;m passing those <code>$cache</code> and <code>$duration</code> variables in to save repeating myself. Create objects for each of your feeds &#8212; mine are called <code>$blog</code>, <code>$twitter</code>, <code>$flickr</code>, and <code>$lastfm</code>.</p>
<p>At this stage, since I&#8217;m going to be including tweets, I&#8217;m going to paste in Alison Gianotto&#8217;s <code><a href="http://www.snipe.net/2009/09/php-twitter-clickable-links/">twitterify</a></code><a href="http://www.snipe.net/2009/09/php-twitter-clickable-links/"> function.</a> This function simply accepts some text and looks for items that would be clickable in Twitter &#8212; references to usernames with @, tags with #, and regular URLs &#8212; and wraps them in nice hyperlinks. The function inserts hyperlinks with a <code>target</code> attribute; I&#8217;ve removed those from my example.</p>
<h2>Create a Shell</h2>
<p>It&#8217;s markup time! Create a HTML shell immediately below the closing <code>?&gt;</code> of your PHP script. In this example, I&#8217;ve just shown the contents of the <code>body</code> element for brevity, though of course I&#8217;d include a <code>!DOCTYPE</code>, <code>head</code>, and other necessities:</p>
<pre><code>&lt;h1&gt;My awesome lifestream&lt;/h1&gt;

&lt;div id="blog"&gt;
  &lt;h2&gt;My blog&lt;/h2&gt;
  &lt;ul&gt;

  &lt;/ul&gt;
&lt;/div&gt;
&lt;div id="twitter"&gt;
  &lt;h2&gt;My tweets&lt;/h2&gt;
  &lt;ul&gt;

  &lt;/ul&gt;
&lt;/div&gt;
&lt;div id="flickr"&gt;
  &lt;h2&gt;My pictures&lt;/h2&gt;
  &lt;ul&gt;

  &lt;/ul&gt;
&lt;/div&gt;
&lt;div id="lastfm"&gt;
  &lt;h2&gt;My tunes&lt;/h2&gt;
  &lt;ul&gt;

  &lt;/ul&gt;
&lt;/div&gt;</code></pre>
<h2 id="reveal_the_items">Reveal the Items</h2>
<p>Now that we have a shell of a document, it&#8217;s time to start adding some more PHP to this page to loop through each of the items and echo the relevant parts of each item &#8212; titles, descriptions, enclosures, links, and more &#8212; and wrap the appropriate markup around them. To find out what&#8217;s available, examine the source of your feeds, and take a look at the SimplePie documentation, specifically the stuff that begins with <code>get_</code>. <a href="http://simplepie.org/wiki/reference/simplepie_item/start#g">You&#8217;ll find a list here.</a></p>
<p>Thinking of my own four feeds, each set of items should be slightly different. With my blog posts, I&#8217;ll want to include a title, description, date, and hyperlink. In the Twitter RSS feed, the title and description are identical, so I&#8217;ll just use the title. For the Flickr items, I just want to show the picture. The tracks on Last.fm just need a title, link, and date. So, I&#8217;ll need to create four separate loops.</p>
<p>Here&#8217;s the loop to spit out my blog items, which I&#8217;ve placed inside the unordered list inside <code>div#blog</code>. This one&#8217;s fairly straightforward:</p>
<pre><code>&lt;?php foreach ($blog-&gt;get_items()
  as $item) : ?&gt;
&lt;li&gt;
  &lt;h3&gt;
    &lt;a href="&lt;?php
      echo $item-&gt;get_link(); ?&gt;"&gt;
      &lt;?php echo $item-&gt;get_title(); ?&gt;
    &lt;/a&gt;
  &lt;/h3&gt;
  &lt;p&gt;
    &lt;?php echo $item-&gt;get_description(); ?&gt;
  &lt;/p&gt;
  &lt;p class="date"&gt;
    &lt;a href="&lt;?php
      echo $item-&gt;get_link(); ?&gt;"&gt;
      &lt;?php echo $item-&gt;get_date(); ?&gt;
    &lt;/a&gt;
  &lt;/p&gt;
&lt;/li&gt;
&lt;?php endforeach; ?&gt;</code></pre>
<p>This loop should generate a series of list-items like this, for each blog entry in my feed:</p>
<pre><code>&lt;li&gt;
  &lt;h3&gt;
    &lt;a href="http://example.com/blogentry/"&gt;
      Title
    &lt;/a&gt;
  &lt;/h3&gt;
  &lt;p&gt;Blog entry description text&lt;/p&gt;
  &lt;p class="date"&gt;
    &lt;a href="http://example.com/blogentry/"&gt;
      01 January 2010, 12:00 am
    &lt;/a&gt;
  &lt;/p&gt;
&lt;/li&gt;</code></pre>
<p>Too easy!</p>
<p>When examining my Twitter feed, I&#8217;ve noticed that each tweet is prefixed with my name, a colon, and a space. In this instance, I&#8217;ll want to remove that text. I&#8217;d also like to convert those hashtags, username references and URLs into clickable hyperlinks. In this loop, when I echo the <code>title</code> of the feed item, I&#8217;ll wrap <code>$item-&gt;get_title()</code> in a <code>str_replace</code> statement, and then wrap <em>that</em> in the <code>twitterify</code> function:</p>
<pre><code>&lt;?php foreach ($twitter-&gt;get_items()
  as $item) : ?&gt;
&lt;li&gt;
  &lt;p&gt;
    &lt;?php echo twitterify(
      str_replace(
        "raena: ", "", $item-&gt;get_title()
      )
    ); ?&gt;
  &lt;/p&gt;
  &lt;p class="date"&gt;
    &lt;a href="&lt;?php
        echo $item-&gt;get_link(); ?&gt;"&gt;
      &lt;?php echo $item-&gt;get_date(); ?&gt;
    &lt;/a&gt;
  &lt;/p&gt;
&lt;/li&gt;
&lt;?php endforeach; ?&gt;</code></pre>
<p>Flickr feeds&#8217; descriptions are less than ideal for my purposes: they include the text &#8220;<em>username</em> posted a photo&#8221;, the medium sized image surrounded by a link to the photo page, and the Flickr photo&#8217;s description. However, I&#8217;d just like to present the littlest version of the image and hyperlink, and use the title of the image as the <code>alt</code> text. Fortunately, Flickr provides the URL to the image in the form of an enclosure, so I&#8217;ll use that instead.</p>
<p>Flickr&#8217;s enclosures are still medium-sized, but I want the cute, square thumbnail. That&#8217;s as simple as replacing any reference to <code>_m.jpg</code> with <code>_s.jpg</code>.</p>
<pre><code>&lt;?php foreach ($flickr-&gt;get_items()
  as $item) : ?&gt;
  &lt;li class="item"&gt;
    &lt;a href="&lt;?php echo $item-&gt;get_link(); ?&gt;"&gt;
    &lt;?php foreach ($item-&gt;get_enclosures()
      as $enclosure) : ?&gt;
      &lt;img src="&lt;?php echo str_replace(
        "_m.jpg","_s.jpg",
          $enclosure-&gt;get_link()
      ); ?&gt;"
        alt="&lt;?php
          echo $item-&gt;get_title(); ?&gt;" /&gt;
      &lt;?php endforeach; ?&gt;
    &lt;/a&gt;
  &lt;/li&gt;
&lt;?php endforeach; ?&gt;</code></pre>
<p>Finally, I only need the titles and dates from my list of loved tracks from Last.fm:</p>
<pre><code>&lt;?php foreach ($lastfm-&gt;get_items()
  as $item) : ?&gt;
  &lt;li class="item"&gt;
  &lt;p&gt;
    &lt;a href="&lt;?php
      echo $item-&gt;get_link(); ?&gt;"&gt;
      &lt;?php echo $item-&gt;get_title(); ?&gt;
    &lt;/a&gt;
  &lt;/p&gt;
  &lt;p class="date"&gt;
    &lt;?php echo $item-&gt;get_date(); ?&gt;
  &lt;/p&gt;
   &lt;/li&gt;
 &lt;?php endforeach; ?&gt;</code></pre>
<p>You&#8217;ll find my completed example as <code>lifestream.phps</code> in the code download, or again, <a href="http://sitepoint.com/examples/lifestream/lifestream.phps">view it in full here.</a> If you plan to use it as a basis for your own work, remember to change the placeholder feed URLs to your own!</p>
<p>When you&#8217;re done fine-tuning your items, it&#8217;s time to upload it to the web server, and view it in your browser. If all went well, you should see a series of unordered lists, each containing different goodies from those RSS feeds.</p>
<p>All that&#8217;s left now is to style the page and add any other interesting goodies you feel are necessary. In the example, you&#8217;ll find an extremely simple set of styles: feel free to use them as a basis for your own.</p>
<h2>There&#8217;s More&#8230;</h2>
<p>Of course, there&#8217;s plenty more to SimplePie than the very easy example we&#8217;ve looked at today. Dive into <a href="http://simplepie.org/wiki">the wiki</a> and see what&#8217;s possible &#8212; there&#8217;s heaps of support for enclosure display, the ability to merge multiple feeds into one giant stream, and easy ways to add social media sharing buttons to items. <a href="http://simplepie.org/wiki/ideas/start">You&#8217;ll find some great examples of SimplePie-powered sites on the wiki.</a> Enjoy!</p>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2005/10/20/simplexml-and-namespaces/' rel='bookmark' title='Permanent Link: SimpleXML and namespaces'>SimpleXML and namespaces</a></li><li><a href='http://www.sitepoint.com/blogs/2004/04/23/new-article-build-an-rss-datalist-control-in-aspnet/' rel='bookmark' title='Permanent Link: New Article: Build an RSS DataList Control in ASP.NET'>New Article: Build an RSS DataList Control in ASP.NET</a></li><li><a href='http://www.sitepoint.com/blogs/2009/03/03/build-a-buzzword-bingo-card-in-php/' rel='bookmark' title='Permanent Link: Build a Buzzword Bingo Card in PHP'>Build a Buzzword Bingo Card in PHP</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=61fffb40d01863049515eef97d10df33&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=61fffb40d01863049515eef97d10df33&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/09/29/build-a-lifestream-with-simplepie/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		</item>
		<item>
			<title>Free Performance with MySQL Table Types</title>
			<link>http://www.pheedcontent.com/click.phdo?i=6798d6940d37773ff4f0da31bc390106</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/09/08/free-performance-with-mysql-table-types/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/09/08/free-performance-with-mysql-table-types/#comments</comments>
			<pubDate>Tue, 08 Sep 2009 05:38:01 +0000</pubDate>
			<dc:creator>Kevin Yank</dc:creator>
			<category><![CDATA[PHP]]></category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=14004</guid>
			<description><![CDATA[Last week, SitePoint launched its second Twitaway, giving away a free PDF of The Principles of Successful Freelancing to anyone kind enough to follow us on Twitter. Unfortunately, with the traffic this attracted, our MySQL server ground to a halt in the first few hours of the giveaway. Kevin explains why, and how we fixed it.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/03/26/mysql-innodb-table-pros-cons/' rel='bookmark' title='Permanent Link: MySQL: the Pros and Cons of InnoDB Tables'>MySQL: the Pros and Cons of InnoDB Tables</a></li><li><a href='http://www.sitepoint.com/blogs/2009/05/15/mysql-table-naming/' rel='bookmark' title='Permanent Link: MySQL Cross-Platform Table Naming'>MySQL Cross-Platform Table Naming</a></li><li><a href='http://www.sitepoint.com/blogs/2009/03/25/mysql-myisam-table-pros-con/' rel='bookmark' title='Permanent Link: MySQL: the Pros and Cons of MyISAM Tables'>MySQL: the Pros and Cons of MyISAM Tables</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=6798d6940d37773ff4f0da31bc390106&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=6798d6940d37773ff4f0da31bc390106&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><strong>Last week, SitePoint launched its second <a href="http://twitaway.sitepoint.com/">Twitaway</a>, giving away a free PDF of <a href="http://www.sitepoint.com/books/freelancer1/"><em>The Principles of Successful Freelancing</em></a> to anyone kind enough to <a href="http://twitter.com/sitepointdotcom/">follow us on Twitter</a>. Predictably, the site we built for readers to claim the PDF was swamped with traffic in the first few hours of the giveaway, and the MySQL server behind the scenes quickly ground to a halt.</strong></p>
<p>First, a little background: at the time of our first Twitaway in 2008, Twitter HQ had been kind enough to relax the limit it places on the maximum number of Twitter direct messages (DMs) that it would allow us to send in a single day. To distribute the free PDFs, then, we simply wrote a PHP script that would look through our Twitter followers every few minutes, identify the new ones and send them a DM with the download link.</p>
<p>This time around, when we sat down to plan Twitaway 2009, it was clear our job would be more difficult. Much less cooperative than they had been previously, Twitter HQ stonewalled our requests to relax the daily DM limit—they wouldn’t even tell us what our daily limit <em>was</em>. For awhile there, we weren’t sure how we would be able to deliver a download link privately to our Twitter followers.</p>
<p>The solution finally came from Mal, a new developer on the team here at SitePoint HQ. We would use Twitter’s new <a href="http://apiwiki.twitter.com/OAuth-FAQ">OAuth support</a> to request temporary access to your Twitter account, use it to verify that you were indeed following us, and then simply display the PDF download link in your browser. No DM required!</p>
<p>Of course, writing a PHP application that supported this OAuth-based workflow was a lot more complicated than writing a static web page with a “follow us on Twitter” link. Not only would it be harder to write, but it would be harder to handle large amounts of traffic hitting it, too.</p>
<p>So there we were: just past midnight on launch day of the Twitaway, and the application had stopped responding. Our monitoring systems made it clear that, while our PHP servers were weathering the storm nicely, the MySQL database server had spiked to 100% CPU usage, stuck its fingers in its ears and begun rocking back and forth, chanting “na-na-na-na-na-I’m-not-listening!”</p>
<div id="adz" class="vertical"></div><p>As it turned out, this was an important lesson for our development team about the importance of choosing appropriate MySQL table types. Read on for how we solved the problem, and why we should have predicted it.</p>
<h2>Choosing The Right Table Type</h2>
<p>When we logged into the swamped MySQL server, we could see (using the <a href="http://dev.mysql.com/doc/refman/5.4/en/show-processlist.html">SHOW PROCESSLIST</a> command) that the overwhelming majority of the queries that it was receiving were for the database table we had set up to contain PHP session data.</p>
<p>By default, PHP stores your users’ session variables in files on the web server’s hard drive. In our case, however, we had multiple web servers that all needed to share the same session data, so we had configured PHP to store its sessions on the MySQL database server instead.</p>
<p>Unfortunately, we had failed to specify a table type when creating the session table in the database:</p>
<pre><code class="sql">CREATE TABLE sessions (
  sesskey VARCHAR(32) NOT NULL DEFAULT '' PRIMARY KEY,
  expiry INT(11),
  expireref VARCHAR(64),
  sessdata LONGTEXT
);</code></pre>
<p>MySQL supports a number of different <a href="http://dev.mysql.com/doc/refman/5.4/en/storage-engines.html">storage engines</a>. When you create a table, by default MySQL will use the <a href="http://dev.mysql.com/doc/refman/5.4/en/myisam-storage-engine.html"><strong>MyISAM storage engine</strong></a> to create a MyISAM table. MyISAM is a relatively simple table type, which forgoes several advanced features in order to offer improved performance. In the early days of MySQL, the MyISAM table type was instrumental in making it one of the best performing databases around.</p>
<p>These characteristics would seem to make MyISAM a natural choice for something like a session data table. Unfortunately, MyISAM has an achilles heel: <strong>table-level locking</strong>. In order to write a change to a MyISAM table, MySQL must lock the whole table, preventing any other query from reading the table until the change is complete.</p>
<p>As thousands upon thousands of SitePoint readers flocked to our Twitaway site to claim their book, multiple PHP processes competed to create, update, and read session data in this single table. And because of MyISAM’s table-level locking, they had to do so one at a time, waiting their turn on an increasingly backed-up database server.</p>
<p>On the surface, the <a href="http://dev.mysql.com/doc/refman/5.4/en/innodb.html"><strong>InnoDB storage engine</strong></a> would seem to be a poor choice for improving database performance. It supports a bunch of features that MyISAM doesn’t, including <a href="http://dev.mysql.com/doc/refman/5.4/en/storage-engine-compare-transactions.html">transactions</a> and <a href="http://dev.mysql.com/doc/refman/5.4/en/innodb-foreign-key-constraints.html">enforced referential integrity</a>, and all the literature will tell you that writing data to an InnoDB table is slower than with a MyISAM table.</p>
<p>But where MyISAM has an achilles heel, InnoDB has a secret weapon: <strong>row-level locking</strong>. When writing data to an InnoDB table, only the affected rows are locked, not the whole table. Queries accessing other parts of the same table can happen simultaneously. In a situation like session data, where each browser request being processed is likely to relate to a different session, row-level locking is just what the doctor ordered!</p>
<p>With this in mind, you can probably guess how we cured our swamped database server. We simply converted the session table to an InnoDB table:</p>
<pre><code class="sql">ALTER TABLE sessions ENGINE = InnoDB;</code></pre>
<p>Instantly, the CPU load on the database server dropped to more reasonable levels, the Twitaway site came back online, and there was much rejoicing. We could have saved ourselves a lot of grief simply by specifying the table type when we first created the table:</p>
<pre><code class="sql">CREATE TABLE sessions (
  sesskey VARCHAR(32) NOT NULL DEFAULT '' PRIMARY KEY,
  expiry INT(11),
  expireref VARCHAR(64),
  sessdata LONGTEXT
) ENGINE = InnoDB;</code></pre>
<p>Switching to InnoDB got us through that first night, but by the next morning we were wondering if there might be an even better table type for us to use. After a little research, the <a href="http://dev.mysql.com/doc/refman/5.4/en/memory-storage-engine.html"><strong>Memory storage engine</strong></a> looked like a likely candidate.</p>
<p>If MyISAM sacrifices features for performance, the Memory table type takes this to extremes. Memory tables are never written to disk, so if you restart your database server, the data stored in any Memory tables is lost. The fact that Memory tables require no disk access, however, makes them blazing fast! Since session data is intended to be volatile anyway, we didn’t really mind if this particular table wouldn’t survive a server restart.</p>
<p>According to the documentation, Memory tables have the same table-level locking weakness as MyISAM. Nevertheless, when we switched our session table to the Memory storage engine, our database server’s CPU usage dropped to virtually zero!</p>
<p>Memory tables have some harsh restrictions that mean they aren’t a cure-all for performance issues. Disappearing data aside, Memory tables cannot contain large column types like TEXT or BLOB. Indeed, the largest piece of data you can store in a Memory table row is a 255 character CHAR or VARCHAR. In our case, we actually had to shorten a couple of our session variable names to make sure they would fit!</p>
<p>At this point, our session table is gliding along happily under moderate load. As the traffic from this article and some other promotion we have planned hits the server, we will be watching closely to decide if the Memory storage engine is up to the task, or if we should switch back to InnoDB to take advantage of row-level locking. In either case, we learned an important lesson: <strong>when performance counts, take the time to think about you choice of MySQL storage engine.</strong> In our case, it took us from a swamped database server to one showing zero CPU usage.</p>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/03/26/mysql-innodb-table-pros-cons/' rel='bookmark' title='Permanent Link: MySQL: the Pros and Cons of InnoDB Tables'>MySQL: the Pros and Cons of InnoDB Tables</a></li><li><a href='http://www.sitepoint.com/blogs/2009/05/15/mysql-table-naming/' rel='bookmark' title='Permanent Link: MySQL Cross-Platform Table Naming'>MySQL Cross-Platform Table Naming</a></li><li><a href='http://www.sitepoint.com/blogs/2009/03/25/mysql-myisam-table-pros-con/' rel='bookmark' title='Permanent Link: MySQL: the Pros and Cons of MyISAM Tables'>MySQL: the Pros and Cons of MyISAM Tables</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=6798d6940d37773ff4f0da31bc390106&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=6798d6940d37773ff4f0da31bc390106&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/09/08/free-performance-with-mysql-table-types/feed/</wfw:commentRss>
			<slash:comments>22</slash:comments>
		</item>
		<item>
			<title>Are PHP Namespaces Really So Bad?</title>
			<link>http://www.pheedcontent.com/click.phdo?i=372966f3b686f9bf33bcd13a89aba1b5</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/#comments</comments>
			<pubDate>Wed, 12 Aug 2009 16:52:18 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category>namespaces</category>
			<category>php</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=12356</guid>
			<description><![CDATA[Namespaces have caused a divide amongst developers who either love or hate their implementation in PHP 5.3. Craig looks at the issues and assures us there is little need to worry.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 1: The Basics'>How to Use PHP Namespaces, Part 1: The Basics</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution'>How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 3: Keywords and Autoloading'>How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=372966f3b686f9bf33bcd13a89aba1b5&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=372966f3b686f9bf33bcd13a89aba1b5&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/07/php-namespaces1.png" alt="PHP namespaces" title="PHP namespaces" width="240" height="240" class="imgright" />PHP developers have been <a href="http://www.petitiononline.com/phpns/petition.html">demanding namespaces</a> for some time. As PHP applications have grown larger and more complex, namespaces have become essential to prevent code clashes.</p>
<p>My <a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/">recent tutorials</a> received a number of comments complaining about namespace implementation in PHP. The main issues were the syntax and the backslash character. Before I tackle those issues, let&#8217;s take a quick look back at the history of PHP.</p>
<h2>PHP is a Mess</h2>
<p>Languages such as C# and Java were designed and follow rigorous syntax standards. PHP has <em>evolved</em>. The original version was released in 1995 and, by version 3, it was a popular procedural programming language. Version 4 introduced rudimentary object orientation and version 5 provides a reasonably standard OOP model. Namespaces have now been added to version 5.3.</p>
<div id="adz" class="vertical"></div><p>PHP critics will argue that the language is a mess. Function names are inconsistent (e.g. strpos, str_split, substr), object handling has been tagged on, and some of the syntax is different &#8212; if not bizarre &#8212; when compared with other languages.</p>
<p>However, PHP remains the most widely-used server-side development language. Its versatility is one of its primary strengths:</p>
<ul>
<li>Novice developers can start with simple procedural programming. They need never touch OOP techniques and can still be productive.</li>
<li>Code written 10 years ago in PHP 3 still works today in PHP 5.3. A few minor tweaks may be required, but major rewrites are rarely necessary.</li>
</ul>
<p>PHP code may not always be pretty, logical, or elegant, but development is rapid and often easier to comprehend than the alternatives.</p>
<h2>PHP Namespace Implementation</h2>
<p>Unlike C# and Java, PHP has to retain compatibility with non-namespaced code. That has been achieved and you can choose whether to use namespaces or not. However, if you&#8217;re using PHP 5.3 (or above), I would recommend them &#8212; even if you simply use the same name throughout the whole of your project.</p>
<p>The choice of <code>namespace</code> and <code>use</code> as namespace operators seems logical. Some developers may disagree, but that would have been the case no matter what they&#8217;d been named. </p>
<p>Finally, we come to the backslash character. Most critics complain that it&#8217;s ugly, difficult to read, and awkward to type on a Mac. However, I still consider it preferable to the double-colon that was originally proposed. Examine the following static method call:</p>
<pre><code class="php">
// PHP 5.3 beta static method call
echo ::App::Lib1::MyClass::WhoAmI();

// PHP 5.3 final static method call
echo \App\Lib1\MyClass::WhoAmI();
</code></pre>
<p>The second line is quicker to type, less error-prone, easier to read, and simpler to understand. If you see a backslash outside of a string, you know namespacing must be involved.</p>
<p>Of course, it would be great if PHP used a &#8216;.&#8217; period for public methods, static methods, and namespaces. That would make it consistent with Java, C#, JavaScript, Python and many other languages. Unfortunately, PHP&#8217;s history and backwards compatibility makes that difficult to achieve.</p>
<p>No language is perfect, and PHP is far from it! However, namespacing has been implemented well, especially when you consider the restrictions and problems it could have caused. I&#8217;m sure you&#8217;ll learn to love that backslash!</p>
<p>Related reading:</p>
<ul>
<li><a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/">How to Use PHP Namespaces, Part 1: The Basics</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/">How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/">How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li>
</ul>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 1: The Basics'>How to Use PHP Namespaces, Part 1: The Basics</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution'>How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 3: Keywords and Autoloading'>How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=372966f3b686f9bf33bcd13a89aba1b5&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=372966f3b686f9bf33bcd13a89aba1b5&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/feed/</wfw:commentRss>
			<slash:comments>23</slash:comments>
		</item>
		<item>
			<title>How to Use PHP Namespaces, Part 3: Keywords and Autoloading</title>
			<link>http://www.pheedcontent.com/click.phdo?i=4f625e60e98f456998e327cde7f111a5</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/#comments</comments>
			<pubDate>Wed, 15 Jul 2009 08:56:07 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category>namespaces</category>
			<category>php</category>
			<category>php5.3</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=12194</guid>
			<description><![CDATA[In the final part of his series explaining PHP namespaces, Craig covers more advanced topics such as the keywords and class autoloading.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 1: The Basics'>How to Use PHP Namespaces, Part 1: The Basics</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution'>How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li><li><a href='http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/' rel='bookmark' title='Permanent Link: Are PHP Namespaces Really So Bad?'>Are PHP Namespaces Really So Bad?</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=4f625e60e98f456998e327cde7f111a5&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=4f625e60e98f456998e327cde7f111a5&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/07/php-namespaces1.png" alt="PHP namespaces" title="PHP namespaces" width="240" height="240" class="imgright" />In parts 1 and 2 of this series, we looked at PHP <a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics">namespace basics</a>, the <a href="http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/">use operator, and name resolution</a>. In this final article we discuss some of the more advanced namespace options.</p>
<h2>The __NAMESPACE__ Constant</h2>
<p>__NAMESPACE__ is a PHP string that always returns the current namespace name. In the global space it will be an empty string.</p>
<pre><code class="php">
&lt;?php
namespace App\Lib1;
echo __NAMESPACE__; // outputs: App\Lib1
?&gt;
</code></pre>
<p>The value has obvious benefits during debugging. It can also be used to dynamically generate a fully-qualified class name, e.g.</p>
<div id="adz" class="horizontal"></div><pre><code class="php">
&lt;?php
namespace App\Lib1;

class MyClass {
	public function WhoAmI() {
		return __METHOD__;
	}
}

$c = __NAMESPACE__ . '\\MyClass';
$m = new $c;
echo $m->WhoAmI(); // outputs: App\Lib1\MyClass::WhoAmI
?&gt;
</code></pre>
<h2>The namespace Keyword</h2>
<p>The <code>namespace</code> keyword can be used to explicitly reference an item within the current namespace or a sub-namespace. It is the namespace equivalent of <code>self</code> within classes:</p>
<pre><code class="php">
&lt;?php
namespace App\Lib1;

class MyClass {
	public function WhoAmI() {
		return __METHOD__;
	}
}

$m = new namespace\MyClass;
echo $m->WhoAmI(); // outputs: App\Lib1\MyClass::WhoAmI
?&gt;
</code></pre>
<h2>Autoloading Namespaced Classes</h2>
<p>One of the best time-saving features of PHP 5 is autoloading. In global (non-namespaced) PHP code, a standard autoload function could be written:</p>
<pre><code class="php">
&lt;?php
$obj1 = new MyClass1(); // classes/MyClass1.php is auto-loaded
$obj2 = new MyClass2(); // classes/MyClass2.php is auto-loaded

// autoload function
function __autoload($class_name) {
	require_once("classes/$class_name.php");
}
?&gt;
</code></pre>
<p>In PHP 5.3, you can create an instance of a namespaced class. In that situation, the fully-qualified namespace and class name is passed to the __autoload function, e.g. the value of $class_name could be &#8220;App\Lib1\MyClass&#8221;. You could continue to place all your PHP class files in the same folder and strip the namespace from the string, however, that could result in file name clashes.</p>
<p>Alternatively, your class file hierarchy could be organized in the same way as your namespace structure. For example, a MyClass.php file could be created in the folder /classes/App/Lib1:</p>
<p><strong>/classes/App/Lib1/MyClass.php:</strong></p>
<pre><code class="php">
&lt;?php
namespace App\Lib1;

class MyClass {
	public function WhoAmI() {
		return __METHOD__;
	}
}
?&gt;
</code></pre>
<p>A file in root folder could then use the following code:</p>
<p><strong>myapp.php:</strong></p>
<pre><code class="php">
&lt;?php
use App\Lib1\MyClass as MC;

$obj = new MC();
echo $obj->WhoAmI();

// autoload function
function __autoload($class) {

	// convert namespace to full file path
	$class = 'classes/' . str_replace('\\', '/', $class) . '.php';
	require_once($class);

}
?&gt;
</code></pre>
<p>Explanation:</p>
<ol>
<li>The class App\Lib1\MyClass is aliased as MC.</li>
<li><code>new MC()</code> is translated to <code>new App\Lib1\MyClass()</code> during compilation.</li>
<li>The string &#8220;App\Lib1\MyClass&#8221; is passed to the __autoload function. This replaces all namespace backslashes with file path forward slashes, and modifies the string so the file &#8220;classes\App\Lib1\MyClass.php&#8221; is loaded.</li>
</ol>
<p>I hope you found this series of PHP namespace articles useful. Will you be namespacing in your PHP code?</p>
<p>See also:</p>
<ul>
<li><a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics">How to Use PHP Namespaces, Part 1: The Basics</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/">How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/07/install-php53-windows/">How to Install PHP 5.3 on Windows</a></li>
</ul>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=horizontal" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 1: The Basics'>How to Use PHP Namespaces, Part 1: The Basics</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution'>How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li><li><a href='http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/' rel='bookmark' title='Permanent Link: Are PHP Namespaces Really So Bad?'>Are PHP Namespaces Really So Bad?</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=4f625e60e98f456998e327cde7f111a5&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=4f625e60e98f456998e327cde7f111a5&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/feed/</wfw:commentRss>
			<slash:comments>10</slash:comments>
		</item>
		<item>
			<title>How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</title>
			<link>http://www.pheedcontent.com/click.phdo?i=a2cb6fb629a7af1f1d9884d913e98492</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/#comments</comments>
			<pubDate>Tue, 14 Jul 2009 08:00:15 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category>namespaces</category>
			<category>php</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=12070</guid>
			<description><![CDATA[In the second part of Craig's PHP namespaces series, he looks at importing, aliasing, and identifier resolution.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 1: The Basics'>How to Use PHP Namespaces, Part 1: The Basics</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 3: Keywords and Autoloading'>How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li><li><a href='http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/' rel='bookmark' title='Permanent Link: Are PHP Namespaces Really So Bad?'>Are PHP Namespaces Really So Bad?</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=a2cb6fb629a7af1f1d9884d913e98492&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=a2cb6fb629a7af1f1d9884d913e98492&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/07/php-namespaces1.png" alt="PHP namespaces" title="PHP namespaces" width="240" height="240" class="imgright" />In <a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics">part 1</a>, we discussed why PHP namespaces are useful and the <code>namespace</code> keyword. In this article, we examine the <code>use</code> command and the way PHP resolves namespace names.</p>
<p>For the purposes of this example, we will define two almost identical code blocks; the only difference is their namespace:</p>
<p style="clear:right;"><strong>lib1.php:</strong></p>
<pre><code class="php">
&lt;?php
// application library 1
namespace App\Lib1;

const MYCONST = 'App\Lib1\MYCONST';

function MyFunction() {
	return __FUNCTION__;
}

class MyClass {
	static function WhoAmI() {
		return __METHOD__;
	}
}
?&gt;
</code></pre>
<p><strong>lib2.php:</strong></p>
<pre><code class="php">
&lt;?php
// application library 2
namespace App\Lib2;

const MYCONST = 'App\Lib2\MYCONST';

function MyFunction() {
	return __FUNCTION__;
}

class MyClass {
	static function WhoAmI() {
		return __METHOD__;
	}
}
?&gt;
</code></pre>
<p>There is a little PHP terminology to understand before we begin&#8230;</p>
<div id="adz" class="vertical"></div><p><strong>Fully-qualified name</strong><br />
Any PHP code can refer to a fully-qualified name &#8212; an identifier starting with the namespace backslash separator, e.g. \App\Lib1\MYCONST, \App\Lib2\MyFunction(), etc. </p>
<p>Fully-qualified names have no ambiguity. The initial backslash operates in a similar way to a file path; it signifies the <em>&#8216;root&#8217;</em> global space. If we implemented a different MyFunction() in our global space, it could be called from lib1.php or lib2.php using <code>\MyFunction()</code>.</p>
<p>Fully-qualified names are useful for one-off function calls or object initialization. However, they can become impractical when you are making lots of calls. As we will discover below, PHP offers other options to save us from namespace typing cramps.</p>
<p><strong>Qualified name</strong><br />
An identifier with at least one namespace separator, e.g. Lib1\MyFunction().</p>
<p><strong>Unqualified name</strong><br />
An identifier without a namespace separator, e.g. MyFunction().</p>
<h2>Working Within the Same Namespace</h2>
<p>Consider the following code:</p>
<p><strong>myapp1.php:</strong></p>
<pre><code class="php">
&lt;?php
namespace App\Lib1;

require_once('lib1.php');
require_once('lib2.php');

header('Content-type: text/plain');
echo MYCONST . "\n";
echo MyFunction() . "\n";
echo MyClass::WhoAmI() . "\n";
?&gt;
</code></pre>
<p>Although we include both <strong>lib1.php</strong> and <strong>lib2.php</strong>, the identifiers MYCONST, MyFunction, and MyClass will only reference code in <strong>lib1.php</strong>. This occurs because the <strong>myapp1.php</strong> code is within the same App\Lib1 namespace:</p>
<p><em>result:</em></p>
<pre><code class="text">
App\Lib1\MYCONST
App\Lib1\MyFunction
App\Lib1\MyClass::WhoAmI
</code></pre>
<h2>Namespace Importing</h2>
<p>Namespaces can be imported with the <code>use</code> operator, e.g.</p>
<p><strong>myapp2.php:</strong></p>
<pre><code class="php">
&lt;?php
use App\Lib2;

require_once('lib1.php');
require_once('lib2.php');

header('Content-type: text/plain');
echo Lib2\MYCONST . "\n";
echo Lib2\MyFunction() . "\n";
echo Lib2\MyClass::WhoAmI() . "\n";
?&gt;
</code></pre>
<p>Any number of <code>use</code> statements can be defined or you can separate individual namespaces with a comma. In this example we have imported the App\Lib2 namespace. We still cannot refer directly to MYCONST, MyFunction or MyClass because our code is in the global space and PHP will look for them there. However, if we add a prefix of &#8216;Lib2\&#8217;, they become qualified names; PHP will search through the imported namespaces until it finds a match.</p>
<p><em>result:</em></p>
<pre><code class="text">
App\Lib2\MYCONST
App\Lib2\MyFunction
App\Lib2\MyClass::WhoAmI
</code></pre>
<h2>Namespace Aliases</h2>
<p>Namespace aliases are perhaps the most useful construct. Aliases allow us to reference long namespaces using a shorter name.</p>
<p><strong>myapp3.php:</strong></p>
<pre><code class="php">
&lt;?php
use App\Lib1 as L;
use App\Lib2\MyClass as Obj;

header('Content-type: text/plain');
require_once('lib1.php');
require_once('lib2.php');

echo L\MYCONST . "\n";
echo L\MyFunction() . "\n";
echo L\MyClass::WhoAmI() . "\n";
echo Obj::WhoAmI() . "\n";
?&gt;
</code></pre>
<p>The first <code>use</code> statement defines App\Lib1 as &#8216;L&#8217;. Any qualified names using &#8216;L&#8217; will be translated to &#8216;App\Lib1&#8242; at compile-time. We can therefore refer to L\MYCONST and L\MyFunction rather than the fully-qualified name.</p>
<p>The second <code>use</code> statement is more interesting. It defines &#8216;Obj&#8217; as an alias for the <strong>class</strong> &#8216;MyClass&#8217; within the App\Lib2\ namespace. This is only possible for classes &#8212; not constants or functions. We can now use <code>new Obj()</code> or run static methods as shown above.</p>
<p><em>result:</em></p>
<pre><code class="text">
App\Lib1\MYCONST
App\Lib1\MyFunction
App\Lib1\MyClass::WhoAmI
App\Lib2\MyClass::WhoAmI
</code></pre>
<h2>PHP Name Resolution Rules</h2>
<p>PHP identifier names are resolved using the following namespace rules. Refer to the <a href="http://www.php.net/manual/pl/language.namespaces.rules.php">PHP manual</a> for more information.</p>
<p>1. Calls to fully-qualified functions, classes or constants are resolved at compile-time.</p>
<p>2. Unqualified and qualified names are translated according to the import rules, e.g. if the namespace A\B\C is imported as C, a call to <code>C\D\e()</code> is translated to <code>A\B\C\D\e()</code>.</p>
<p>3. Inside a namespace, all qualified names not already translated according to import rules have the current namespace prepended, e.g. if a call to <code>C\D\e()</code> is performed within namespace A\B, it is translated to <code>A\B\C\D\e()</code>.</p>
<p>4. Unqualified class names are translated according to current import rules and the full name is substituted for short imported name, e.g. if class C in namespace A\B is imported as X, <code>new X()</code> is translated to <code>new A\B\C()</code>.</p>
<p>5. Unqualified function calls within a namespace are resolved at run-time. For example, if MyFunction() is called within namespace A\B, PHP first looks for the function \A\B\MyFunction(). If that is not found, it looks for \MyFunction() in the global space.</p>
<p>6. Calls to unqualified or qualified class names are resolved at run-time. For example, if we call <code>new C()</code> within namespace A\B, PHP will look for the class A\B\C. If that is not found, it will attempt to autoload A\B\C.</p>
<p>See also:</p>
<ul>
<li><a href="http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics">How to Use PHP Namespaces, Part 1: The Basics</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/">How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/07/install-php53-windows/">How to Install PHP 5.3 on Windows</a></li>
</ul>
<p>In tomorrow&#8217;s final article we will cover autoloading and other advanced options.</p>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=vertical" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 1: The Basics'>How to Use PHP Namespaces, Part 1: The Basics</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 3: Keywords and Autoloading'>How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li><li><a href='http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/' rel='bookmark' title='Permanent Link: Are PHP Namespaces Really So Bad?'>Are PHP Namespaces Really So Bad?</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=a2cb6fb629a7af1f1d9884d913e98492&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=a2cb6fb629a7af1f1d9884d913e98492&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/feed/</wfw:commentRss>
			<slash:comments>15</slash:comments>
		</item>
		<item>
			<title>How to Use PHP Namespaces, Part 1: The Basics</title>
			<link>http://www.pheedcontent.com/click.phdo?i=488f0362821ca188101d1436f1751ca0</link>
			<pheedo:origLink>http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/</pheedo:origLink>
			<comments>http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/#comments</comments>
			<pubDate>Mon, 13 Jul 2009 06:49:04 +0000</pubDate>
			<dc:creator>Craig Buckler</dc:creator>
			<category><![CDATA[PHP]]></category>
			<category>namespaces</category>
			<category>php</category>
			<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=12000</guid>
			<description><![CDATA[In the first part of a series of articles, Craig explains why namespaces were required in PHP and how they can be used in your code.


Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution'>How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 3: Keywords and Autoloading'>How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li><li><a href='http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/' rel='bookmark' title='Permanent Link: Are PHP Namespaces Really So Bad?'>Are PHP Namespaces Really So Bad?</a></li></ol><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=488f0362821ca188101d1436f1751ca0&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=488f0362821ca188101d1436f1751ca0&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.sitepoint.com/blogs/wp-content/uploads/2009/07/php-namespaces.png" alt="PHP 5.3 namespaces" title="PHP 5.3 namespaces" width="240" height="240" class="imgright" />Namespaces are one of the most significant changes in PHP 5.3. They will be familiar to C# and Java developers, and they are likely to change the structure of PHP applications for the better.</p>
<h2>Why Do We Need Namespaces?</h2>
<p>As the size of your PHP code library increases, there is increased risk of accidentally re-defining a function or class name that has been declared before. The problem is exacerbated when you attempt to add third-party components or plugins; what if two or more code sets implement a &#8216;Database&#8217; or &#8216;User&#8217; class?</p>
<p>Until now, the only solution has been long class/function names. For example, WordPress prefixes every name with &#8216;WP_&#8217;. The Zend Framework uses a highly descriptive naming convention that results in long-winded class names such as <code>Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive</code>.</p>
<div id="adz" class="horizontal"></div><p>Name collision problems can be solved with namespaces. PHP constants, classes, and functions can be grouped into namespaced libraries.</p>
<h2>How are Namespaces Defined?</h2>
<p>By default, all constant, class, and function names are placed in the global space &#8212; like they were before PHP supported namespaces.</p>
<p>Namespaced code is defined using a single <code>namespace</code> keyword at the top of your PHP file. It <strong>must</strong> be the first command (with the exception of <code>declare</code>) and no non-PHP code, HTML, or white-space can precede the command, e.g.</p>
<pre><code class="php">
&lt;?php
// define this code in the 'MyProject' namespace
namespace MyProject;

// ... code ...
</code></pre>
<p>The code following this line will be assigned to the &#8216;MyProject&#8217; namespace. It is not possible to nest namespaces or define more than one namespace for the same code block (only the last will be recognized). However, you can define different namespaced code in the same file, e.g.</p>
<pre><code class="php">
&lt;?php
namespace MyProject1;
// PHP code for the MyProject1 namespace

namespace MyProject2;
// PHP code for the MyProject2 namespace

// Alternative syntax
namespace MyProject3 {
	// PHP code for the MyProject3 namespace
}
?&gt;
</code></pre>
<p>Although this is possible, I would advise against it: retain your sanity by defining a single namespace per file.</p>
<p><strong>Sub-namespaces</strong><br />
PHP allows you to define a hierarchy of namespaces so libraries can be sub-divided. Sub-namespaces are separated using a backslash (\) character, e.g.</p>
<ul>
<li>MyProject\SubName</li>
<li>MyProject\Database\MySQL</li>
<li>CompanyName\MyProject\Library\Common\Widget1</li>
</ul>
<h2>Calling Namespaced Code</h2>
<p>In a file named lib1.php, we will define a constant, a function, and a class within the App\Lib1 namespace:</p>
<p><strong>lib1.php</strong></p>
<pre><code class="php">
&lt;?php
// application library 1
namespace App\Lib1;

const MYCONST = 'App\Lib1\MYCONST';

function MyFunction() {
	return __FUNCTION__;
}

class MyClass {
	static function WhoAmI() {
		return __METHOD__;
	}
}
?&gt;
</code></pre>
<p>We can now include this code in another PHP file, e.g.</p>
<p><strong>myapp.php</strong></p>
<pre><code class="php">
&lt;?php
header('Content-type: text/plain');
require_once('lib1.php');

echo \App\Lib1\MYCONST . "\n";
echo \App\Lib1\MyFunction() . "\n";
echo \App\Lib1\MyClass::WhoAmI() . "\n";
?&gt;
</code></pre>
<p>No namespace is defined in myapp.php so the code exists in the global space. Any direct reference to MYCONST, MyFunction or MyClass will fail because they exist in the App\Lib1 namespace. To call code in lib1.php, we can add a prefix of \App\Lib1 to define fully-qualified names. The following result is output when we load myapp.php:</p>
<pre><code class="text">
App\Lib1\MYCONST
App\Lib1\MyFunction
App\Lib1\MyClass::WhoAmI
</code></pre>
<p>Fully-qualified names can become quite long and there are few obvious benefits over defining long class names such as App-Lib1-MyClass. Therefore, in the next article, we will discuss aliasing and take a closer look at how PHP resolves namespace names.</p>
<p>See also:</p>
<ul>
<li><a href="http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/">How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/">How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li>
<li><a href="http://www.sitepoint.com/blogs/2009/07/07/install-php53-windows/">How to Install PHP 5.3 on Windows</a></li>
</ul>
<script src="http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&amp;did=adz&amp;adtype=horizontal" type="text/javascript"></script>

<p>Related posts:<ol><li><a href='http://www.sitepoint.com/blogs/2009/07/14/php-namespaces-import-alias-resolution/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution'>How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution</a></li><li><a href='http://www.sitepoint.com/blogs/2009/07/15/how-to-use-php-namespaces-part-3-keywords-and-autoloading/' rel='bookmark' title='Permanent Link: How to Use PHP Namespaces, Part 3: Keywords and Autoloading'>How to Use PHP Namespaces, Part 3: Keywords and Autoloading</a></li><li><a href='http://www.sitepoint.com/blogs/2009/08/13/are-php-namespaces-bad/' rel='bookmark' title='Permanent Link: Are PHP Namespaces Really So Bad?'>Are PHP Namespaces Really So Bad?</a></li></ol></p><br clear="both" style="clear: both;"/>
<br clear="both" style="clear: both;"/>
<a href="http://ads.pheedo.com/click.phdo?s=488f0362821ca188101d1436f1751ca0&p=1"><img alt="" style="border: 0;" border="0" src="http://ads.pheedo.com/img.phdo?s=488f0362821ca188101d1436f1751ca0&p=1"/></a>
<img alt="" height="0" width="0" border="0" style="display:none" src="http://a.rfihub.com/eus.gif?eui=2225"/>]]></content:encoded>
			<wfw:commentRss>http://www.sitepoint.com/blogs/2009/07/13/php-53-namespaces-basics/feed/</wfw:commentRss>
			<slash:comments>56</slash:comments>
		</item>
	</channel>
</rss>
<!-- Dynamic Page Served (once) in 8.647 seconds -->
<!-- Cached page served by WP-Cache -->