<?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>Notes Log &#187; Zend Framework</title>
	<atom:link href="http://noteslog.com/category/zend-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://noteslog.com</link>
	<description></description>
	<lastBuildDate>Sat, 15 May 2010 13:31:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to autoload Table and Row classes in ZF</title>
		<link>http://noteslog.com/post/how-to-autoload-table-and-row-classes-in-zf/</link>
		<comments>http://noteslog.com/post/how-to-autoload-table-and-row-classes-in-zf/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 20:09:33 +0000</pubDate>
		<dc:creator>Andrea Ercolino</dc:creator>
				<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://noteslog.com/?p=258</guid>
		<description><![CDATA[This article describes how to use a little hack into the Zend Framework library, for having it autoload an ExampleModel.php file whenever an ExampleTable or ExampleRow class is instantiated. In my Zend Framework projects I use to split my models into a Table and a Row class, and I put both into the same file, [...]]]></description>
			<content:encoded><![CDATA[<p>This article describes how to use a little hack into the <a href="http://framework.zend.com/" target="_blank">Zend Framework</a> library, for having it autoload an <span style="color: #333399;">ExampleModel.php</span> file whenever an <span style="color: #333399;">ExampleTable</span> or <span style="color: #333399;">ExampleRow</span> class is instantiated.</p>
<p><span id="more-258"></span>In my Zend Framework projects I use to split my models into a Table and a Row class, and I put both into the same file, one after the other.</p>
<p>For example, my <span style="color: #333399;">PersonModel.php</span> holds <span style="color: #333399;">PersonTable</span> and <span style="color: #333399;">PersonRow</span> classes. I like to have collection methods like <span style="color: #333399;">findByFullname</span> into the <span style="color: #333399;">PersonTable</span> class and instance methods like <span style="color: #333399;">getGroups</span> into the <span style="color: #333399;">PersonRow</span> class. And it&#8217;s very helpful to have these classes into the same file, due to their coupling.</p>
<p>Zend Framework supports autoloading, and it&#8217;s possible to replace the default autoloader with your own, but the autoloading replacement feature is compromised by the use of direct calls to the default loader throughout the library (it occurs 50+ times in ZF 1.6.x).</p>
<h4>1. Patch Zend_Loader</h4>
<p><span style="color: #333399;">Patched Zend_Loader</span> differs from <span style="color: #333399;">Zend_Loader</span> only for the added lines 7-10. The patch is a contribution of Tomáš Procházka who described it in the <a href="http://framework.zend.com/issues/browse/ZF-2533" target="_blank">issue ZF-2533</a>.</p>
<p><pre class="ln-"><code class="php">    public static function loadClass($class, $dirs = null)
    {
        if (class_exists($class, false) || interface_exists($class, false)) {
            return;
        }

        if (spl_autoload_functions()!==false) {
          spl_autoload_call($class);
          if (class_exists($class, false) || interface_exists($class, false))  return;
        }

        if ((null !== $dirs) &amp;&amp; !is_string($dirs) &amp;&amp; !is_array($dirs)) {
...</code></pre></p>
<h4>2. Add My_Loader</h4>
<p><span style="color: #333399;">My_Loader</span> differs from <span style="color: #333399;">Zend_Loader</span> only for the added line 14.</p>
<p><pre class="ln-"><code class="php">    public static function loadClass($class, $dirs = null)
    {
        if (class_exists($class, false) || interface_exists($class, false)) {
            return;
        }

        if ((null !== $dirs) &amp;&amp; !is_string($dirs) &amp;&amp; !is_array($dirs)) {
            require_once 'Zend/Exception.php';
            throw new Zend_Exception('Directory argument must be a string or an array');
        }

        // autodiscover the path from the class name
        $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
        $file = preg_replace('/^(?!Zend)(.*?)(?:Table|Row)\.php$/', '$1Model.php', $file);
        if (!empty($dirs)) {
...</code></pre></p>
<h4>3. Register My_Loader as a replacement for Zend_Loader</h4>
<p>Finally, at the beginning of my bootstrap file I register <span style="color: #333399;">My_loader</span> as the real loader.</p>
<p><pre><code class="php">...
	require_once 'Zend/Loader.php';
	Zend_Loader::registerAutoload( 'My_Loader' );
...</code></pre></p>
<h4>Final thoughts</h4>
<p>The <a href="http://framework.zend.com/issues/browse/ZF-2533" target="_blank">issue ZF-2533</a> is very old, the proposed patch is simple and effective, still it does not find its way into the trunk. Why?</p>
]]></content:encoded>
			<wfw:commentRss>http://noteslog.com/post/how-to-autoload-table-and-row-classes-in-zf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
