<?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>LAMPlights &#187; autoloading</title>
	<atom:link href="http://www.hermanradtke.com/blog/tag/autoloading/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hermanradtke.com</link>
	<description>Personal anecdotes from my experiences using the LAMP stack</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:14:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Don&#8217;t Overuse Use</title>
		<link>http://www.hermanradtke.com/blog/dont-overuse-use/</link>
		<comments>http://www.hermanradtke.com/blog/dont-overuse-use/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 05:46:27 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[autoloading]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php53]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=435</guid>
		<description><![CDATA[Nate Abele just announced Lithium 0.9 on the rad-dev blog.  I think Lithium is a great looking framework and can't wait for it to get a 1.0 release and really start to take off.  However, looking through the examples I started to notice that the use of the "use" namespace keyword was an often used [...]]]></description>
			<content:encoded><![CDATA[<p>Nate Abele just announced <a href="http://rad-dev.org/lithium/wiki/blog/Lithium-0-9-The-Lambdas-are-awesome-Edition">Lithium 0.9</a> on the rad-dev blog.  I think Lithium is a great looking framework and can't wait for it to get a 1.0 release and really start to take off.  However, looking through the examples I started to notice that the use of the "use" namespace keyword was an often used convention.  It reminded me of the require/require_once creep of the days of old.  I was discussing it with Nate via Twitter, but I couldn't seem to get my point across.  Maybe I will have better luck here...</p>
<p><span id="more-435"></span>Before PHP  went OO and autoloading existed, users had little choice but to place require_once statements throughout their code to satisfy the application dependencies.  At the start of a project this was fine as the dependencies were thoughtfully mapped out and the require_once statements strategically placed.  However, as time goes on and code goes through maintenance the require_once creep starts to set in.</p>
<p>What do I mean by require_once creep?  I simply mean that there are require_once statements including code that doesn't need to be included.  This caused a couple of problems:</p>
<ol>
<li>More code for the PHP parser to parse and thus a slower application</li>
<li>Confusing code dependencies</li>
</ol>
<p>Now, admittedly problem 2 is not the major issue problem 1 is.  However, nothing is more frustrating than removing a innocuous require_once from a file only to have some other random file break because it was including that file and depending on that require_once.  There is an entire PHP extension dedicated to helping people solve this very problem: <a href="http://us.php.net/inclued">inclued</a>.  I still remember Mozilla's conference talk about refactoring TikiWiki to not simply include every nearly every single library file on every single page.</p>
<p>To fix this problem, autoloading was introduced.  This allowed people to stop using require_once and spawned a method of pseudo-namespacing files.  One of the more popular methods of psuedo-namespacing was the PEAR standard.  This standard said to name the class based on the directory location of the class.  So if I have class in herman/awesome/Solution.php I would name the class Herman_Awesome_Solution.  This obviously gave rise to some pretty long class names, but people generally loved it as it saved them from require_once creep.</p>
<p>Let's fast forward to PHP 5.3.  The "use" keyword is introduced to let users alias long namespaces to a shorter name.  So I can reference the namespace \this\is\a\really\long\namespace as longns.  This is great, now I can reference a really long namespace with a single word.  All I have to do is remember to alias it.  And the aliases are descending, so if I alias a class that alias two other classes, I can use those additional aliases free of charge.</p>
<p>But wait, doesn't it seem like we just went full circle with naming conventions?  We started with simple class names with require_once creep, graduated to autoloading and using super long class names and are now back using simple class names with use creep.  The use creep doesn't have the performance halting side effects of require_once, but it sure seems like it can cause quite a dependency maintenance nightmare.  It may be no problem for you, but what about the guy coding next to you who really doesn't care?</p>
<p>I think I will stick to using the fully qualified namespace (FQN) of a class.  It will take a few more keystrokes, but it keep my dependencies cleaner.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/dont-overuse-use/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hidden features with spl_autoload() and namespaces</title>
		<link>http://www.hermanradtke.com/blog/hidden-features-with-spl_autoload-and-namespaces/</link>
		<comments>http://www.hermanradtke.com/blog/hidden-features-with-spl_autoload-and-namespaces/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 21:58:45 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[autoloading]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php53]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=171</guid>
		<description><![CDATA[The namespace operator in PHP 5.3 is a backslash (\). One of the criticisms of this operator is that the code starts to look like directory paths on Windows. The added side benefit of this is that spl_autoload() knows how to autoload classes that use a namespace style that matches the directory layout. $baz = [...]]]></description>
			<content:encoded><![CDATA[<p>The namespace operator in PHP 5.3 is a backslash (\).  One of the criticisms of this operator is that the code starts to look like directory paths on Windows.  The added side benefit of this is that spl_autoload() knows how to autoload classes that use a namespace style that matches the directory layout.</p>
<p><span id="more-171"></span></p>

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

<p>The spl_autoload() function gets passed the fully qualified namespace as well as the class name.  This is very similar to the PEAR class naming convention of using underscore characters (_) to denote the path to a class.</p>
<p>Consider the following:</p>
<p>The spl_autoload() funtion is passed the string 'foo\Bar'.  The extensions registered by spl_autoload_extensions() are then used in conjunction with include paths to look for a valid file.  The spl_autoload_extensions() function has the extensions .inc and .php registered by default.  The default include_path is the current directory, so let's assume the current directory is D:\herman\php.</p>
<p>List of attempts by spl_autoload() to load the class from the above example:</p>
<ul>
<li>D:\herman\php\foo\Bar.inc</li>
<li>D:\herman\php\foo\Bar.php</li>
</ul>
<p>This means that you can map each namespace to a directory in your application.  Consider the following directory layout.</p>
<p>blog\<br />
models\<br />
Author.php<br />
controllers\<br />
PostController.php</p>
<p>The namespace for the Author class in the Author.php file should be 'blog\models'.  The namespace for the PostController class in the PostController.php file should be 'blog\controllers'.  Using this namespacing strategy along with a proper include path allows php to autoload your classes out of the box.</p>
<p><span style="color: #ff0000;">Remember, this only works with PHP on Windows.  I have a pending patch to make spl_autoload() work with namespacing by default on Linux.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/hidden-features-with-spl_autoload-and-namespaces/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

