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

<channel>
	<title>Michiel van der Velde &#187; namespaces</title>
	<atom:link href="http://www.michielvdvelde.nl/tag/namespaces/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michielvdvelde.nl</link>
	<description>A statistical impossibility</description>
	<lastBuildDate>Tue, 27 Apr 2010 16:01:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Namespaces in php 5.3</title>
		<link>http://www.michielvdvelde.nl/2010/01/namespaces-in-php-5-3/</link>
		<comments>http://www.michielvdvelde.nl/2010/01/namespaces-in-php-5-3/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 12:47:53 +0000</pubDate>
		<dc:creator>Michiel</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://www.michielvdvelde.nl/?p=149</guid>
		<description><![CDATA[PHP 5.3, introduced in June 2009 and has some really interesting enhancements. Some of you may know about the promise of namespace support in php 6. Because this was a popular requested feature, namespaces have been introduced in php 5.3. What are namespaces and why are they convenient? &#8216;Namespaces&#8217; are default in almost every other [...]]]></description>
			<content:encoded><![CDATA[<p>PHP 5.3, introduced in June 2009 and has some really interesting enhancements. Some of you may know about the promise of namespace support in php 6. Because this was a popular requested feature, namespaces have been introduced in php 5.3.</p>
<h2>What are namespaces and why are they convenient?</h2>
<p>&#8216;Namespaces&#8217; are default in almost every other (OOP) language, such as C++ and C#. They separate classes and other objects (such as methods) in logical units, mostly to avoid name collisions. A program (or web site, as is probably the case in php) may use several frameworks and libraries. These may hold methods and/or classes that are named the same, for example a class named <em>Date</em>. Prior to php 5.3, this would lead to the well known error &#8220;<em>Could not redeclare class <strong>Date</strong> in file/name.php on line no</em>&#8220;. This, of course, could be a problem when you want to use multiple frameworks and/or third-party libraries.</p>
<p>The problem of name colissions was dealt with before by using something called &#8216;poor man&#8217;s namespacing&#8217;, which has been used in, for example, the Zend Framework. This looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">class</span> Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive <span style="color: #000000; font-weight: bold;">extends</span> Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFilter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Search_Lucene_Analysis_TokenFilter_LowerCase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This of course is not very handy all the time. So, how do namespaces solve this problem?</p>
<h2>Namespaces in php 5.3</h2>
<p>A namespace in php can be thought of as an extra layer around (part of) your code. Every class en method name within it is unique and does not conflict with classes or methods with the same name in other namespaces.</p>
<p>To declare a namespace in php, use the keyword &#8216;namespace&#8217; on the first line of your file. Your classes and methods you define below it. All code within that file will be in that namespace. Let&#8217;s look at an example on how to use namespaces:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">namespace</span> MichielvdVelde\Core<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Database
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The class <em>Database</em> will now reside in the namespace <em>MichielvdVelde\Core</em>. If you were to make another namespace, which also holds a class named Database, this would be fine.</p>
<p>So, how do you use classes within a namespace? This is really kind of simple. There are two method.</p>
<h3>Method one</h3>
<p>Add the namespace to the declaration of the class. Like this:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'MichielvdVelde/Core.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MichielvdVelde\Core\Database<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But this method is not really an improvement over poor man's namespacing. Therefore, there is a second method.</p>
<h3>Method two</h3>
<p>By using the 'use' keyward, you cam import namespaces in your code. This more closely resembles namespacing as implemented in other languages such as C++ and C#.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'MichielvdVelde/Core.php'</span><span style="color: #339933;">;</span>
&nbsp;
use MichielvdVelde\Core <span style="color: #b1b100;">as</span> CORE<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db</span><span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CORE\Database<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, this is more aliasing than really importing. But still, this method is very useful.</p>
<h2>Gotcha's</h2>
<h3>Functions are also part of namespaces</h3>
<p>When defining functions in files with the 'namespace' keyword at the top, thsey are also part of that namespace.</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">namespace</span> MichielvdVelde\Core<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getDatabase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'MichielvdVelde/Core.php'</span><span style="color: #339933;">;</span>
&nbsp;
getDatabase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Geeft E_FATAL error: Undefined function getDatabase()</span>
&nbsp;
use MichielvdVelde\Core <span style="color: #b1b100;">as</span> CORE<span style="color: #339933;">;</span>
&nbsp;
CORE\getDatabase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// This does work</span></pre></div></div>

<h3>Autoload changes</h3>
<p>Autoload on Windows does not use the \ very well. You may need to change your autoload function for this:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
<span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$className</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'\\'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">DIRECTORY_SEPARATOR</span><span style="color: #339933;">,</span> <span style="color: #000088;">$className</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">require</span> <span style="color: #000088;">$className</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MichielveVelde\Core\Database<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Also note that you need to define __autoload within the global scope. If you define it within a namespace, php won't find it. If you do want to use the autolaod function from within a namespace, use the <em>spl_autoload_register</em> function:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">spl_autoload_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MichielvdVelde\\Core\\Autoloader'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Conclusion</h2>
<p>PHP 5.3 introduces support for namespaces which will be very handy in organizing and cleaning up your code. Although it may take some time for web hosters to support php 5.3, you can experiment with it by installing the latest version of <a title="XAMPP" href="http://www.apachefriends.org/en/xampp.html" target="_blank">XAMPP</a> on your omputer, which has php 5.3 included.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michielvdvelde.nl/2010/01/namespaces-in-php-5-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
