<?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 - Tag - oop</title>
  <link>http://mirmodynamics.com/</link>
  <atom:link href="http://mirmodynamics.com/feed/tag/oop/rss2" rel="self" type="application/rss+xml"/>
  <description>Rien de grand ne se fit jamais sans enthousiasme.</description>
  <language>en</language>
  <pubDate>Tue, 18 Nov 2008 12:26:30 +0100</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/atom/comments/1072</wfw:commentRss>
      </item>
    
  <item>
    <title>Stripping the logic: the Transfer Object</title>
    <link>http://mirmodynamics.com/post/2007/08/11/Stripping-the-logic%3A-the-Transfert-Object</link>
    <guid isPermaLink="false">urn:md5:fb705efad8749c9d2c20f85702147c29</guid>
    <pubDate>Sat, 11 Aug 2007 11:56:00 +0200</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Coding</category>
        <category>j2ee</category><category>oop</category><category>p of eaa</category><category>pattern</category><category>php</category><category>transfer object</category><category>zend framework</category>    
    <description>    &lt;p&gt;Sometimes you have to pass an object data to another object, or to another layer of your application (who said controller/view ?), while ensuring that the receiving entity will not be able to run business code encapsulated in your class. In the Zend Framework, several objects provide a &lt;code&gt;toArray&lt;/code&gt; method, but that is not always sufficient as sometimes you'd like to keep with the &lt;code&gt;$object-&amp;gt;varname&lt;/code&gt; syntax.&lt;/p&gt;


&lt;p&gt;That is where the Transfer Object arrives. While the preceding definition is not exact (that's not the real purpose of the Transfert Object in the J2EE spirit), This is the most common use that PHP Developers can make of it nowadays I think. So I came up with a &lt;a href=&quot;http://svn.riskle.com/library/Riskle/Pattern/TransferObject.php&quot;&gt;very light implementation of a concept&lt;/a&gt; which I hope can prove useful for any folks getting by there.&lt;/p&gt;


&lt;p&gt;See also:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Martin Fowler's &lt;a href=&quot;http://www.martinfowler.com/eaaCatalog/valueObject.html&quot;&gt;Value Object&lt;/a&gt; and &lt;a href=&quot;http://www.martinfowler.com/eaaCatalog/dataTransferObject.html&quot;&gt;Data Transfer Object&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html&quot;&gt;The Transfert Object as a Core J2EE Pattern&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2007/08/11/Stripping-the-logic%3A-the-Transfert-Object#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2007/08/11/Stripping-the-logic%3A-the-Transfert-Object#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/atom/comments/925</wfw:commentRss>
      </item>
    
  <item>
    <title>Interfaces et Classes Abstraites</title>
    <link>http://mirmodynamics.com/post/2007/01/31/Interfaces-et-Classes-Abstraites</link>
    <guid isPermaLink="false">urn:md5:7eff991a33b1612a701afab5a0ed3233</guid>
    <pubDate>Wed, 31 Jan 2007 09:08:00 +0100</pubDate>
    <dc:creator>Geoffrey</dc:creator>
        <category>Coding</category>
        <category>bonnes pratiques</category><category>concepts</category><category>doc</category><category>oop</category><category>opinions</category><category>php</category>    
    <description>&lt;p&gt;Les concepts de classes abstraites et d'interfaces sont souvents assez flous quand on débute dans la programmation objet, et on se fourvoie assez souvent sur leur utilisation et leur but. Voila une petite explication qui j'espère sera 1) juste et 2) assez claire pour tout le monde. Si vous pensez que je me fourvoie sur ces 2 concepts de base de la &lt;acronym&gt;POO&lt;/acronym&gt;, n'hésitez pas à me le faire savoir !&lt;/p&gt;    &lt;p&gt;Commençons par le commencement: l'Interface. Une Interface définit une &lt;acronym&gt;API&lt;/acronym&gt; (Application Programming Interface), dont le seul et unique but est de décrire les méthodes disponibles pour un objet donné. Une Interface ne contient pas de code fonctionnel, seulement des synopsis de méthodes et d'attributs. Par exemple, l'utilisation du mot clé &amp;quot;instanceof&amp;quot; permet de déterminer si un objet donné implémente ou non une interface donnée, et d'adapter le comportement de l'application selon la réponse. Le &amp;quot;type hinting&amp;quot; permet également ce genre de choses:&lt;/p&gt;

&lt;pre&gt;
&amp;lt;?php

interface Bar_Interface {
      public function doSomething();
}

class Bar implements Bar_Interface {
      public function doSomething() {
            echo &amp;quot;something was done.&amp;quot;;
      }
}

class Foo {
      public function doSomething(Bar_Interface $bar) {
            $bar-&amp;gt;doSomething();
      }
}

?&amp;gt;
&lt;/pre&gt;


&lt;p&gt;Dans cet exemple, quand Foo::doSomething() est executée, on est certain que l'objet $bar dispose de la méthode doSomething car il implémente l'Interface Bar_Interface.&lt;/p&gt;


&lt;p&gt;Vient ensuite la classe abstraite. La classe abstraite, &amp;quot;abstract class&amp;quot; en V.O., se comporte comme un squelette d'objet. Elle est utile dans le cas où on utilise une série d'objets d'une même famille qui partagent du code commun, mais qui comportent tous leurs particularités propres. Dans ce cas de figure, on commence par implémenter le code commun dans la classe abstraite, dont hériteront les autres objets, dans lesquels le code spécifique pourra à son tour être implémenté. C'est très utile pour certains Design Pattern, comme par exemple &amp;quot;Adapter&amp;quot; ou &amp;quot;Proxy&amp;quot;, qui ont tous deux pour but de permettre des stratégies différentes pour une même situation, situation qui présente le plus souvent des points similaires quelque soit la stratégie adoptée.&lt;/p&gt;



&lt;p&gt;En règle générale, une classe abstraite implémente une interface (et un exemple d'Adapter en cadeau bonux):&lt;/p&gt;

&lt;pre&gt;
&amp;lt;?php

interface Bar_Adapter_Interface {
      public function doSomething();
}

class Bar {
      public function __construct(Bar_Adapter_Interface $adapter) {
            $this-&amp;gt;_adapter = $adapter;
      }

      public function doSomething() {
            return $this-&amp;gt;_adapter-&amp;gt;doSomething();
      }

      static public function factory($adapter) {
            $className = &amp;quot;Bar_Adapter_&amp;quot;.$adapter;
            if (!class_exists($className)) {
                  throw new Exception('Adapter not available: '.$adapter);
            } else {
                  return new Bar(new $className);
            }
      }
}

abstract class Bar_Adapter_Abstract implements Bar_Adapter_Interface {
      protected function getSomething() {
            return &amp;quot;la méthode pour accéder a ce truc est la même pour tout le monde&amp;quot;;
      }
}

class Bar_Adapter_Explode extends Bar_Adapter_Abstract {
      public function doSomething() {
            return explode(' ', $this-&amp;gt;getSomething());
      }
}

class Bar_Adapter_PregSplit extends Bar_Adapter_Abstract {
      public function doSomething() {
            return preg_split('/ /', $this-&amp;gt;getSomething());
      }
}

$bar = Bar::factory('Explode');
print_r($bar-&amp;gt;doSomething());

?&amp;gt;
&lt;/pre&gt;


&lt;p&gt;Ici, l'objet Bar offre la méthode doSomething, qui présentement retourne une chaine explosée, et on propose deux adapteurs pour se faire, l'un utilisant explode(), l'autre preg_split(). Bien sur, cet exemple est complètement trivial :-)&lt;/p&gt;</description>
    
    
    
          <comments>http://mirmodynamics.com/post/2007/01/31/Interfaces-et-Classes-Abstraites#comment-form</comments>
      <wfw:comment>http://mirmodynamics.com/post/2007/01/31/Interfaces-et-Classes-Abstraites#comment-form</wfw:comment>
      <wfw:commentRss>http://mirmodynamics.com/feed/atom/comments/739</wfw:commentRss>
      </item>
    
</channel>
</rss>