<?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>Straylight Run &#187; autoload</title> <atom:link href="http://blog.straylightrun.net/tag/autoload/feed/" rel="self" type="application/rss+xml" /><link>http://blog.straylightrun.net</link> <description>Software, Technology, PHP</description> <lastBuildDate>Mon, 07 Nov 2011 19:26:59 +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>Autoload Magic</title><link>http://blog.straylightrun.net/2009/05/06/autoload-magic/</link> <comments>http://blog.straylightrun.net/2009/05/06/autoload-magic/#comments</comments> <pubDate>Thu, 07 May 2009 00:21:36 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Coding]]></category> <category><![CDATA[autoload]]></category> <category><![CDATA[classpath]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/2009/05/06/autoload-magic/</guid> <description><![CDATA[So to use a class in PHP, usually, we first have to include the file that contains the definition for that class. require&#40;'myclass.php'&#41;; &#160; $class = new MyClass; But after you start instantiating a few classes here and there, problems arise. If you call a lot of classes, the number calls to require() becomes big. [...]]]></description> <content:encoded><![CDATA[<p>So to use a class in PHP, usually, we first have to include the file that contains the definition for that class.</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'myclass.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyClass<span style="color: #339933;">;</span></pre></div></div><p>But after you start instantiating a few classes here and there, problems arise.</p><ul><li>If you call a lot of classes, the number calls to <code>require()</code> becomes big.</li><li>When you start including files in other files, you are not sure whether or not you&#8217;ve already included a certain file, so you use <code>require_once()</code>, which is inefficient.</li><li>Worst of all, included files become disorganized, and you may accidentally remove a <code>require()</code> that is needed, not in the current file, but in a file downstream, leading to a Fatal Error (if you use <code>require()</code> and not <code>include()</code>).</li></ul><p>The solution: <a
href="http://php.net/autoload"><code>__autoload()</code></a> The PHP autoload feature is one of the coolest features in the whole language. Basically, if PHP tries to load a class and cannot find the class definition, it will call the <code>__autoload()</code> function that you provide giving it the name of the class it can&#8217;t find. At that point, you&#8217;re on your own. But how to find the file located on disk? There are four strategies for finding the right class file.</p><ol><li>Keep all class files in one directory. Not a very attractive method.</li><li>Maintain a global array of class names to class definition files. In this case, the name of the class is the key, and the location of the file on disk is the value. This global hash could be created in memory upon server start time, or created as the application executes. Obviously, seems kind of a heavy solution to me.</li><li>Use a special container class that all other classes inherit from. This method is suitable mostly when attempting to unserialize objects (when unserializing objects, PHP must have the class definition to recover the object). This special container will automatically know the class file for the contained object. Then, the only class file that needs to be located is the container&#8217;s class file. (Autoloading in the <a
href="http://efiquest.org/?p=6">context of object un/serialization is a special case of class loading</a>.)</li><li>Use a naming convention. The idea here is to name your classes in a way that corresponds to your file system. For example, Zend names their classes where underscores represent directories in the project. So the <code>Zend_Auth_Storage_Session</code> class is defined in the file <code>Zend/Auth/Storage/Session.php</code>. Autoload simply needs to replace the underscores for the system directory separator char and give it to <code>require()</code>.</li></ol><p>At work, we use #4, naming conventions. Why? Well, why not? The other methods are heavier or more complicated, and I don&#8217;t see any gain. By using a simple naming convention, never again will we need to call <code>require()</code> or <code>include()</code> in our app. As a bonus side-effect, the code is organized in a consistent matter. Provided we follow the naming convention that Zend (and PEAR) use, our <code>__autoload()</code> function looks like:</p><div
class="wp_syntax"><div
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// by using PEAR-type naming conventions, autoload will always know where to</span>
<span style="color: #666666; font-style: italic;">// find class definitions</span>
<span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">DIRECTORY_SEPARATOR</span><span style="color: #339933;">,</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2009/05/06/autoload-magic/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
