<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://mirmodynamics.com/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Mirmo Dynamics - dry</title>
  <link>http://mirmodynamics.com/</link>
  <atom:link href="http://mirmodynamics.com/feed/tag/dry/rss2" rel="self" type="application/rss+xml"/>
  <description>Rien de grand ne se fit jamais sans enthousiasme.</description>
  <language>en</language>
  <pubDate>Wed, 15 Oct 2008 21:10:32 +0200</pubDate>
  <copyright>2003-2008 &amp;copy; Geoffrey Bachelet</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Extending Zend_Controller_Router_Route: the singleton problem.</title>
    <link>http://mirmodynamics.com/post/2007/11/05/Extending-Zend_Controller_Router_Route%3A-the-singleton-problem</link>
    <guid isPermaLink="false">urn:md5:639fef9d7c01edc0e63ee3f2c2a5c823</guid>
    <pubDate>Mon, 05 Nov 2007 14:37:00 +0100</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Coding</category>
        <category>annoying</category><category>dry</category><category>oop</category><category>pattern</category><category>php5</category><category>route</category><category>self</category><category>singleton</category><category>zend framework</category>    
    <description>    &lt;p&gt;Today I ran into an issue while extending &lt;code&gt;Zend_Controller_Router_Route&lt;/code&gt;. I wanted to add a little path pre/post processing in the &lt;code&gt;match()&lt;/code&gt; and &lt;code&gt;assemble()&lt;/code&gt; methods, so I just extended the Route class to add my tiny bits of code into the methods. Except it did not work at all. After a few debuging, it turned out that the Router uses &lt;code&gt;Zend_Controller_Router_Route::getInstance()&lt;/code&gt; to retrieve a route object, which uses a &lt;code&gt;new self();&lt;/code&gt; statement to instantiate the route object. Problem is that &lt;code&gt;self&lt;/code&gt; always refers to the current class definition we're in, if the method is called from a child class, without being overloaded, self will refer to the wrong class.&lt;/p&gt;


&lt;p&gt;Example:&lt;/p&gt;
&lt;code class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; Foo &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; public &lt;a href=&quot;http://www.php.net/static&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; getInstance&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #b1b100;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; self;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; Bar extends Foo &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.php.net/var_dump&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;var_dump&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Bar::&lt;span style=&quot;color: #006600;&quot;&gt;getClass&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/code&gt;


&lt;p&gt;echoes something like:&lt;/p&gt;

&lt;pre&gt;
object(Foo)#1 (0) {
}
&lt;/pre&gt;


&lt;p&gt;Which is fscking wrong IMHO. A quick workaround is to overload the &lt;code&gt;getInstance&lt;/code&gt; method, which is what I call pretty annoying as it does not follow the DRY principle.&lt;/p&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2007/11/05/Extending-Zend_Controller_Router_Route%3A-the-singleton-problem#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2007/11/05/Extending-Zend_Controller_Router_Route%3A-the-singleton-problem#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/rss2/comments/1072</wfw:commentRss>
      </item>
    
</channel>
</rss>