<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://dikini.net" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>dikini.net - php</title>
 <link>http://dikini.net/taxonomy/term/99/0</link>
 <description></description>
 <language>en</language>
<item>
 <title>php closures and lambda functions rfc</title>
 <link>http://dikini.net/20.06.2008/php_closures_and_lambda_functions_rfc</link>
 <description>&lt;p&gt;There is a &lt;a href=&quot;http://aspn.activestate.com/ASPN/Mail/Message/php-dev/3640369&quot;&gt;discussion&lt;/a&gt; going on in &lt;a href=&quot;http://wiki.php.net/rfc/closures&quot;&gt;php&lt;/a&gt; land about introducing closures and lambda functions, there was even a &lt;a href=&quot;http://www.haskell.org/pipermail/haskell-cafe/2008-June/044379.html&quot;&gt;discussion on haskell-cafe&lt;/a&gt; about it (chx strikes again ;). About time, I would say. Having this functionality is a bonus. Having it implemented badly or half-arsed is going to do more damage than help. This is a short summary of what do I understand from the rfc and what do I think about it.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/20.06.2008/php_closures_and_lambda_functions_rfc&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/20.06.2008/php_closures_and_lambda_functions_rfc#comment</comments>
 <category domain="http://dikini.net/tags/closures">closures</category>
 <category domain="http://dikini.net/tags/drupal">drupal</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Fri, 20 Jun 2008 06:45:44 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">299 at http://dikini.net</guid>
</item>
<item>
 <title>drupal 5.0 beta</title>
 <link>http://dikini.net/01.11.2006/drupal_5_0_beta</link>
 <description>&lt;p&gt;Sweet. Really. There are not too many big changes, but funnily enough they are felt more than previous betas/pre-releases.&lt;/p&gt;
&lt;p&gt;First you are struck with the really nice, simple, stylish, cute, add your own epithet default theme - Garland. You can modify the colour scheme online. So &lt;a href=&quot;http://acko.net/garland/?q=node/16&quot; rel=&quot;nofollow&quot;&gt;palette builders enjoy and share&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Kudos to the reorganised admin pages and everyone involved in that. At first glance it is allright.&lt;/p&gt;
&lt;p&gt;The infamous cck and views leave a lot of traces in core.&lt;/p&gt;
&lt;p&gt;All I can say it took me between 5 and 10 minutes to setup a basic group blog site. That includes database creation magic, (php) file uploads, user registration ...&lt;/p&gt;
&lt;p&gt;Time for theming, upgrading and code porting I suppose. Just where could I get it from?&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/01.11.2006/drupal_5_0_beta&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/01.11.2006/drupal_5_0_beta#comment</comments>
 <category domain="http://dikini.net/tags/drupal">drupal</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 01 Nov 2006 08:40:44 -0500</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">233 at http://dikini.net</guid>
</item>
<item>
 <title>Models, records, databases, friends and foes</title>
 <link>http://dikini.net/models_records_databases_friends_and_foes</link>
 <description>&lt;p&gt;the beginnings of a &lt;em&gt;long&lt;/em&gt; on DB related pattern discussion.&lt;/p&gt;
</description>
 <comments>http://dikini.net/models_records_databases_friends_and_foes#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <pubDate>Fri, 27 Oct 2006 11:01:12 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">230 at http://dikini.net</guid>
</item>
<item>
 <title>First sketches</title>
 <link>http://dikini.net/first_sketches</link>
 <description>&lt;p&gt;Ok, Let&#039;s recap:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Relational database (SQL) tables are records. We can view them (in our heads) as record type definitions.&lt;/li&gt;
&lt;li&gt;JOINS define (some kind of) derived union types.&lt;/li&gt;
&lt;li&gt;SQL CROSS JOINS are full cartesian products, i.e. union types where no name folding/aliasing is done by the imaginary type system&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;How can we model that in php?&lt;/h3&gt;
&lt;p&gt;Classes (and objects) are records. Arrays are records too. Both are candidates for doing the job. We need to be able to somehow represent this (meta) type information and manipulate it. The aim is to have a &lt;strong&gt;natural&lt;/strong&gt; feeling/looking abstraction of the database in php. It should be flexible enough, for us to modify the relations at runtime, as we need. It should allow us in the long run to have a near optimal speed and not too much complications in the end code. That is a tough cookie.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/first_sketches&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/first_sketches#comment</comments>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/sql">sql</category>
 <pubDate>Fri, 27 Oct 2006 10:57:32 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">229 at http://dikini.net</guid>
</item>
<item>
 <title>Problems of active record and friends</title>
 <link>http://dikini.net/problems_of_active_record_and_friends</link>
 <description>&lt;p&gt;After a lot of silence caused by lot&#039;s of work (good, my bills are happy) and a continuous diversion into scheme, haskell, dylan and other interesting languages, I&#039;m back into php speaking land. Funny feeling that. So here comes the beginning of something I&#039;ve been continuously rubbing my (leftovers of) brains against.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;copout&lt;/strong&gt; This is not php specific, but since it discusses frequent problems occuring in php apps, I&#039;ve labeled it with as php related as well. Makes it easier maintaining the site you see.&lt;/p&gt;
&lt;p&gt;I have a problem with active record as a pattern. It is a logical one. Essentially it turn a (SQL) database row into an object. The class of the object represents the SQL table or view. Of course you can add behaviour to those classes and objects, i.e. activating those records.&lt;/p&gt;
&lt;p&gt;All that is nice and dandy, but when you start talking about relations and constructing &lt;em&gt;dynamically&lt;/em&gt; queries and corresponding objects, we start hitting the limitations of what I will call from now on &lt;em&gt;the  naive active record&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The problem is not trivial at all. It boils down to how to make peace between the host language type system (for example classes) and the SQL&#039;s dynamic compound types - the relations expressed as SELECT variants for example. It gets even harder when we decide to reverse the direction, so that we actulally want to update the database. Yuk! As though somebody actually does that!&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/problems_of_active_record_and_friends&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/problems_of_active_record_and_friends#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/series">series</category>
 <pubDate>Fri, 27 Oct 2006 06:25:45 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">228 at http://dikini.net</guid>
</item>
<item>
 <title>A catalogue of php design pattern shorts</title>
 <link>http://dikini.net/a_catalogue_of_php_design_pattern_shorts</link>
 <description>&lt;dl&gt;
&lt;dt&gt;What can you find on these pages?&lt;/dt&gt;
&lt;dd&gt;A pattern catalogue with php pseudo-codish examples. For working implementations you can always check &lt;a href=&quot;http://www.fluffycat.com/PHP-Design-Patterns&quot;&gt;what the kitty thinks of that&lt;/a&gt;. I might have problems with over implementation, but those are my problems, it does not mean those pages are notr worth as a reference.&lt;/dd&gt;
&lt;dt&gt;Why?&lt;/dt&gt;
&lt;dd&gt;A reminder and a tantrum space&lt;/dd&gt;
&lt;dt&gt;Why have you moved the catalogue to this page?&lt;/dt&gt;
&lt;dd&gt;Because I wanted to separate this catalogue from longer descriptions I plan to put, and all of them should live under design patterns&lt;/dd&gt;
&lt;dt&gt;Warnings and notices&lt;/dt&gt;
&lt;dd&gt;&lt;p&gt;&lt;strong&gt;warning&lt;/strong&gt; I realised, late enough not to correct it, that I&#039;m using a particular case of wishful thinking - using any php callable in &lt;code&gt;$callable(...)&lt;/code&gt; expressions. The reality is abit uglier. We need to use either
&lt;code&gt;call_user_func($callable,...); call_user_func_array($callable,...)&lt;/code&gt; How I wish php could do that out of the box, especially when it actually does it, just not in the short syntax. Anyone happy to hold my hand to do a php patch for that feature? I&#039;m very unfamiliar with the internals, sorry.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;notice&lt;/strong&gt; As Jared notes in his comment &lt;a href=&quot;http://dikini.net/26.07.2006/updated_design_pattern_collection#comment-16415&quot;&gt;calling functions/methods with $&lt;/a&gt; in a lot of cases you can use &lt;code&gt;$obj-&gt;$meth(...)&lt;/code&gt; instead of &lt;code&gt;call_user_func($callable,...);&lt;/code&gt;. Indeed you can use the following code as part of the remedy
&lt;code&gt;list($object,$method) = some_call()
$object-&gt;$method(...)&lt;/code&gt;
unfortunately this discriminates against functions, which are lighter to use than their method brethren&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;and a mention&lt;/strong&gt; The above two used to live on the top pattern page until I moved them here&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/a_catalogue_of_php_design_pattern_shorts&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/a_catalogue_of_php_design_pattern_shorts#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Fri, 27 Oct 2006 05:57:36 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">227 at http://dikini.net</guid>
</item>
<item>
 <title>macro processor - initial notes</title>
 <link>http://dikini.net/30.08.2006/macro_processor_initial_notes</link>
 <description>&lt;p&gt;Most of my work on this is on paper, and some code sketeches. There quite a few non-trivial issues, and hard decisions to be made. Since I have the knack of loosing or recycling the various paper bits lying around, I decided to keep at least some of them in my diary, aka this blog.&lt;/p&gt;
&lt;h3&gt;initial requirements&lt;/h3&gt;
&lt;dl&gt;
&lt;dt&gt;convenient syntax and semantics&lt;/dt&gt;
&lt;dd&gt;convenience matters. The syntax should be obvious. The meaning of the syntax should be easy to grasp.&lt;/dd&gt;
&lt;dt&gt;minimal core&lt;/dt&gt;
&lt;dd&gt;the core of the macro processor should be minimal. This will help managing the code quality. The minimal core should be enriched using macros.&lt;/dd&gt;
&lt;dt&gt;retargetable/multi-targets&lt;/dt&gt;
&lt;dd&gt;using one and the same &#039;surface syntax&#039; to produce different target code. It should be possible to have a one to many code generation. Example application - one definition producing javascript, php and sql code. Alternative example: one definition - three code targets - debug, deployment + a set of autogenerated tests&lt;/dd&gt;
&lt;dt&gt;target and syntax agnostic&lt;/dt&gt;
&lt;dd&gt;no assumptions should be made (at least at top level) about the target or syntax. We should be able to accomodate any theoretically parsable syntax, within reason, of course. Subject to parsing strategy selection, etc...&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/30.08.2006/macro_processor_initial_notes&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/30.08.2006/macro_processor_initial_notes#comment</comments>
 <category domain="http://dikini.net/tags/dylan">dylan</category>
 <category domain="http://dikini.net/tags/lisp">lisp</category>
 <category domain="http://dikini.net/tags/macros">macros</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/projects">projects</category>
 <category domain="http://dikini.net/tags/scheme">scheme</category>
 <category domain="http://dikini.net/tags/work_in_progress">work in progress</category>
 <pubDate>Wed, 30 Aug 2006 05:46:40 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">217 at http://dikini.net</guid>
</item>
<item>
 <title>macros, higher-order functions, datatypes and design patterns</title>
 <link>http://dikini.net/29.08.2006/macros_higher_order_functions_datatypes_and_design_patterns</link>
 <description>&lt;p&gt;While reading, researching and experimenting for my macros project the terms in the title get mixed a lot. I&#039;ll try to summarise what is my understanding of a lot of the above. To be fair, most of these words are heavily overloaded with sometimes contradictory and or ovelapping meanings, so this short is only about my personal summary, no pretence for a general study or lit review.&lt;/p&gt;
&lt;p&gt;Generic programming intuitevely corresponds to design patterns, or a tool to implement them. There should be a distinction between the functional or algorithmic side of the term and the structural or data (type) genericity.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/29.08.2006/macros_higher_order_functions_datatypes_and_design_patterns&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/29.08.2006/macros_higher_order_functions_datatypes_and_design_patterns#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/dylan">dylan</category>
 <category domain="http://dikini.net/tags/haskell">haskell</category>
 <category domain="http://dikini.net/tags/macros">macros</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/scheme">scheme</category>
 <pubDate>Tue, 29 Aug 2006 10:09:21 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">215 at http://dikini.net</guid>
</item>
<item>
 <title>SOC: Collaborative Editor, pimping for tests and comments</title>
 <link>http://dikini.net/17.08.2006/soc_collaborative_editor_pimping_for_tests_and_comments</link>
 <description>&lt;p&gt;Ernest has done quite a lot of experiments with his collaborative editor. Early on I&#039;ve advised him early on to focus on the communication/collision detetection/... part as opposed to a new really fancy editor. In the ideal world his code should be mixed in with and editor of your choice, but that is a utopian fantasy.&lt;/p&gt;
&lt;p&gt;At the moment the demo code is focused around a special node, but Ernest is working on removing the specialisation to eventually handle any form.&lt;/p&gt;
&lt;p&gt;If you are reading this please try it (links follow) and give him comments, ideas, etc... Don&#039;t kill him though - the learning curve of the drupal apis is steepish. Help with ideas and bug reports works better.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://groups.drupal.org/soc-collaborative-editor&quot;&gt;the drupal groups page&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://groups.drupal.org/node/1216&quot;&gt;how to test the module page&lt;/a&gt; a bit outdated, but the major points still valid (hint: look for the usernames)&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/17.08.2006/soc_collaborative_editor_pimping_for_tests_and_comments&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/17.08.2006/soc_collaborative_editor_pimping_for_tests_and_comments#comment</comments>
 <category domain="http://dikini.net/tags/drupal">drupal</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/soc_2006">soc 2006</category>
 <pubDate>Thu, 17 Aug 2006 10:56:58 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">212 at http://dikini.net</guid>
</item>
<item>
 <title>Rewriting macros - the peculiar case of php</title>
 <link>http://dikini.net/16.08.2006/rewriting_macros_the_peculiar_case_of_php</link>
 <description>&lt;p&gt;Without going into theoretical details, some of which are quite alien to me, I&#039;ll try to describe some of the challenges that pattern patching rewriting macros might pose for a language like php. After brief explanation what kind of a beast is this, I try to explore some of the finer points, which might cause problems. The intent of this post is to sketch a design and highlight some of the possible issues.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/16.08.2006/rewriting_macros_the_peculiar_case_of_php&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/16.08.2006/rewriting_macros_the_peculiar_case_of_php#comment</comments>
 <category domain="http://dikini.net/tags/ideas">ideas</category>
 <category domain="http://dikini.net/tags/macros">macros</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/projects">projects</category>
 <category domain="http://dikini.net/tags/work_in_progress">work in progress</category>
 <pubDate>Wed, 16 Aug 2006 08:29:03 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">211 at http://dikini.net</guid>
</item>
<item>
 <title>nested classes and other beasts</title>
 <link>http://dikini.net/11.08.2006/nested_classes_and_other_beasts</link>
 <description>&lt;p&gt;It seems I&#039;m not alone in not wanting added complexity in php. &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;So what? I would rather the core developers spend more time implementing Namespaces than some feature no one in their right mind would use. People would use Namespaces and people advocating subclasses can use Namespaces for such a feature.&lt;br /&gt;
&lt;a href=&quot;http://www.santosj.name/php/dont-advocate-subclasses/&quot; rel=&quot;nofollow&quot;&gt;Don’t Advocate Subclasses (Inner Classes) - SantosJ&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;From a &#039;theoretical&#039; perspective php is weird. Really strange. It has some low(ish) level features, for example the semantics of a &lt;em&gt;function&lt;/em&gt;, the lack of nested functions and closures, as well as high level ones - the runtime symbol resolution, .i.e. variable variables and the $$func() syntax.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/11.08.2006/nested_classes_and_other_beasts&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/11.08.2006/nested_classes_and_other_beasts#comment</comments>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/rants">rants</category>
 <pubDate>Fri, 11 Aug 2006 05:35:25 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">210 at http://dikini.net</guid>
</item>
<item>
 <title>out of the (php) loop</title>
 <link>http://dikini.net/09.08.2006/out_of_the_php_loop</link>
 <description>&lt;p&gt;&lt;em&gt;&lt;a href=&quot;http://www.regdeveloper.co.uk/2006/08/08/cplusplus_loops/&quot; title=&quot;Out of the (C++) loop&quot; rel=&quot;nofollow&quot;&gt;inspired by Verity&lt;/a&gt;&lt;/em&gt; , my urge to display my writing talent and random buzz on the internet&lt;/p&gt;
&lt;p&gt;This conversation was (allegedly) overheard in a nursery, which won&#039;t be disclosed to avoid litigation &lt;/p&gt;
&lt;p&gt;&lt;em&gt;Write a standard php loop on the nursery blackboard&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Come on, can&#039;t be bothered. Are you joking?&lt;/strong&gt;&lt;br /&gt;
&lt;code&gt;foreach( $array as $key =&amp;gt; $value ) echo &quot;$key $value&quot;;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Wrong! Don&#039;t you ever learn proper language usage&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What have I done NOW?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/09.08.2006/out_of_the_php_loop&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/09.08.2006/out_of_the_php_loop#comment</comments>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/scribbles">scribbles</category>
 <pubDate>Wed, 09 Aug 2006 12:32:03 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">209 at http://dikini.net</guid>
</item>
<item>
 <title>Poor man&#039;s macro programming in php (revisited)</title>
 <link>http://dikini.net/01.08.2006/poor_mans_macro_programming_in_php_revisited</link>
 <description>&lt;em&gt;How exactly can you do macros in php now?&lt;/em&gt;

&lt;p&gt;Macros have (let&#039;s say) two main responsibilities - adding syntactic sugar and abstraction of common patterns. The syntactic sugar bit is something which probably should happen at compile time, at least in a weakly typed language like php. So I&#039;ll skip them. The abstraction of computation, well this is wat codified design patterns are. This is what function, classes and the rest of the things we read about and use usually do. To abstract a function in php there are at least two different strategies, probably more, but these two are effective in diametrically different situations. The first one is a combination of the ideas of &lt;a href=&quot;http://dikini.net/generic_function&quot;&gt;generic functions (higher order functions)&lt;/a&gt; and &lt;a href=&quot;http://dikini.net/partial_evaluation&quot;&gt;partial evaluation&lt;/a&gt;. In code it can look something like:&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://dikini.net/01.08.2006/poor_mans_macro_programming_in_php_revisited&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/01.08.2006/poor_mans_macro_programming_in_php_revisited#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/ideas">ideas</category>
 <category domain="http://dikini.net/tags/macros">macros</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <pubDate>Tue, 01 Aug 2006 04:31:59 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">207 at http://dikini.net</guid>
</item>
<item>
 <title>Proxy</title>
 <link>http://dikini.net/proxy</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Provide a surrogate or placeholder for another object to control access to it&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to provide a wrapper around a set of classes to unify their access protocols&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;&lt;ul&gt;&lt;li&gt;Provide a wrap method&lt;/li&gt;
&lt;li&gt;intercept calls to the wraper object and forward them accordingly, may change the protocol&lt;/li&gt;
&lt;/ul&gt;&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
class Fluenter
{
        private $obj;

        function __construct($obj)
        {
                $this-&gt;obj = $obj;
        }
        static function MakeFluent($obj)
        {
                if ($obj instanceof fluent)
                        return $obj;
                else
                        return new fluenter($obj);
        }
        function __call($method, $args)
        {
                $result = call_user_func_array(array($this-&gt;obj, $method), $args);
                if (is_null($result))
                        return $this;
                else if (is_object($result) and ($result instanceof $fluent))
                        return $result;
                else
                        throw new RuntimeException(
                                                &quot;Fluent::__call called method $method &quot;.
                                                &quot;and expected a null return or a non-fluent object, &quot;.
                                                &quot;got (&quot;.gettype($return).&quot;) $return instead.&quot;);
        }
}
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;notes&lt;/dt&gt;
&lt;dd&gt;&lt;ul&gt;&lt;li&gt;This code is an unaltered copy from &lt;a href=&quot;http://blog.jonnay.net/archives/599-Making-Fluent-Interfaces-Readable-in-PHP.html&quot;&gt;Making Fluent Interfaces Readable in PHP&lt;/a&gt;, I hope Jonnay doesn&#039;t mind that&lt;/li&gt;
&lt;li&gt;You might want to check out &lt;a href=&quot;http://dikini.net/fluent_interfaces_method_chaining&quot;&gt;fluent interfaces( method chaining)&lt;/a&gt; and &lt;a href=&quot;http://dikini.net/accumulator_passing&quot;&gt;accumulator passing&lt;/a&gt;&lt;/li&gt;&lt;/dd&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/proxy&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/proxy#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Thu, 27 Jul 2006 11:20:04 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">206 at http://dikini.net</guid>
</item>
<item>
 <title>Accumulator passing</title>
 <link>http://dikini.net/accumulator_passing</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Use of an accumulator variable to allow the finer control over the assembly of the final result of an algorithm &lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to use &lt;a href=&quot;http://dikini.net/fluent_interfaces_method_chaining&quot;&gt;method-chaining&lt;/a&gt;, but we need to return values from some of the methods&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Use an accumulator variable&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;  a simple IFS( iterative function system ) &lt;br /&gt;
  x(t+1) = x(t) + f[random(1..i)]( 1/(t+1) ) &lt;br /&gt;
  note: the implementation is quite naive and not foolproof at all.
&lt;pre&gt;&lt;code&gt;&amp;lt;php
class example {
  private $ifs = array( &#039;sin&#039;, &#039;cos&#039; );  

  function x( &amp;$acc ) {
    $acc[&#039;t&#039;] += 1;
    $acc[&#039;x&#039;] = $acc[&#039;x&#039;] + $this-&gt;ifs[ rand(0,1) ]( 1/ $acc[&#039;t&#039;] );
    return $this;
  }
}

$ex = new example();

$something = array(&#039;t&#039;=&gt;0, &#039;x&#039; =&gt; 0 );
$ex-&gt;x($something) -&gt; x($something) -&gt; x($something);
print &#039;t: &#039; . $something[&#039;t&#039;] . &#039; x: &#039; . $something[&#039;x&#039;] . &quot;\n&quot;;
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
and when you run it:
&lt;pre&gt;&lt;code&gt;vlado@cow:~/php design patterns$ php  php accumulator_passing.php
t: 3 x:1.34692254127
vlado@cow:~/php design patterns$ php accumulator_passing.php
t: 3 x: 1.64809122021
vlado@cow:~/php design patterns$ php accumulator_passing.php
t: 3 x: 2.66401049301
&lt;/code&gt;&lt;/pre&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/accumulator_passing&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/accumulator_passing#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Thu, 27 Jul 2006 10:59:21 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">205 at http://dikini.net</guid>
</item>
<item>
 <title>Fluent Interfaces/ Method chaining</title>
 <link>http://dikini.net/fluent_interfaces_method_chaining</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;return the next context of computation, enable simple and flexible sequencing of computation&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to provide a small domain specific language to improve the readability of the code&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;return the next expected object (state)&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
class fluent_class {
  var $state_var;
  function self_next( $arg ) {
    ....
    return $this;
  }
  function other_next( $arg ) {
    ....
    return $this-&gt;another;
  }
}

$obj = new fluent_class();
$obj-&gt;self_next(1)
    -&gt;self_next(2)
    -&gt;other_next(3)
    -&gt;something_else(4);

&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Note&lt;/dt&gt;
&lt;dd&gt;This is an awkward idea to explain in words. Essentially you are delegating the desision what is the next object in the chain to the executable methods. It probably should be combined with weaving the values, which we would otherwise return, using accumulator style passing, or some other store method.&lt;/dd&gt;
&lt;dd&gt;Have a look at &lt;a href=&quot;http://blog.jonnay.net/archives/599-Making-Fluent-Interfaces-Readable-in-PHP.html&quot;&gt; Jonnay&#039;s post&lt;/a&gt; for better and more sensible code examples and the Fluenter class.&lt;/dd&gt;
&lt;dd&gt;I sometimes look at this as the imperative &quot;version&quot; of continuation passing, but let&#039;s not even get there.&lt;/dd&gt;&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/fluent_interfaces_method_chaining&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/fluent_interfaces_method_chaining#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Thu, 27 Jul 2006 07:33:18 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">204 at http://dikini.net</guid>
</item>
<item>
 <title>Poor man&#039;s macro programming in php</title>
 <link>http://dikini.net/26.07.2006/poor_mans_macro_programming_in_php</link>
 <description>Jonnay&#039;s post I &lt;a href=&quot;http://dikini.net/26.07.2006/googles_summer_of_yawn&quot;&gt;mentioned before&lt;/a&gt; started me thinking - what do you need to have macros in php. What is the closest we can get to that without actually changing anything in php (poor man&#039;s version? What minimal sugar does php need to make it comfy? What is the natural syntax for macros in php?

I definitely don&#039;t know the answers to these questions but let&#039;s try:

&lt;code&gt;
  macro delay() {
     match { delay( $func, ... ) } : 
       { $varname = $func . random();
         $$varname = array(new promise( $func, ... ), &#039;evaluate&#039;); 
         return $varname;
       }
     match { $func( ... ) } :
       { return call_user_func($$varname, ... }; }
  }
&lt;/code&gt;
&lt;p&gt;This looks kind of allright, phpish. It has problems, but the above dream code demonstrates the idea enough - match the left-hand-side code and substitute it with the right hand side. The difference from C macros is that this macro is a program fragment/function/..., the result of which is substituted in the AST, as opposed to simple string pattern matching + substitution&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;http://dikini.net/26.07.2006/poor_mans_macro_programming_in_php&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/26.07.2006/poor_mans_macro_programming_in_php#comment</comments>
 <category domain="http://dikini.net/tags/macros">macros</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <category domain="http://dikini.net/tags/scheme">scheme</category>
 <pubDate>Wed, 26 Jul 2006 11:43:44 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">203 at http://dikini.net</guid>
</item>
<item>
 <title>Googles Summer of yawn.</title>
 <link>http://dikini.net/26.07.2006/googles_summer_of_yawn</link>
 <description>&lt;blockquote&gt;&lt;p&gt;&lt;a href=&quot;http://blog.jonnay.net/archives/739-Googles-Summer-of-yawn..html&quot; rel=&quot;nofollow&quot;&gt;Googles Summer of yawn.&lt;/a&gt; - &lt;em&gt;Google&#039;s &amp;quot;Summer of Code&amp;quot; has started and they have announced a few PHP projects. Some of them look interesting and useful.  However, I am going to pick apart one in particular, that to be honest, looks neither interesting or useful: &lt;a href=&quot;http://code.google.com/soc/php/appinfo.html?csaid=3752FBA8CFFCD528&quot; rel=&quot;nofollow&quot;&gt;The PHP Macro Preprocessor&lt;/a&gt;.&lt;br /&gt;
....&lt;br /&gt;
&lt;br /&gt;
Why does the PHP Preprocessor need to be stuck in a world of #ifdefs and #includes?  Instead of blindly copying what the C preprocessor does, why not focus on the languages strengths and deficiencies?  The object system of PHP needs to be looked at, and see if it can be improved upon through a method of code transformation.&lt;br /&gt;
&lt;br /&gt;&lt;/em&gt; [&lt;a href=&quot;http://blog.jonnay.net/&quot; rel=&quot;nofollow&quot;&gt;sacrificial rabbit&lt;/a&gt;]
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;I sooo totally agree with Jonnay on this. In fact it is simply like reading my own thoughts, just put it better english, , ok, nearly. But what to do about it? I don&#039;t really know, I&#039;m afraid I chicken out from digging into the guts of php. When I last looked there it was really scary.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/26.07.2006/googles_summer_of_yawn&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/26.07.2006/googles_summer_of_yawn#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/rants">rants</category>
 <category domain="http://dikini.net/tags/scheme">scheme</category>
 <pubDate>Wed, 26 Jul 2006 09:07:08 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">202 at http://dikini.net</guid>
</item>
<item>
 <title>Updated design pattern collection</title>
 <link>http://dikini.net/26.07.2006/updated_design_pattern_collection</link>
 <description>&lt;p&gt;I guess I&#039;m on the down form my design pattern tantrum hit. I updated the collection so  it now has about 20 of them, not all can be considered &quot;officially&quot; design patterns, but to be honest I don&#039;t really care. I&#039;m very well aware that I ignore the existance of boundaries between design patterns, idioms, techniques, ... That is intentional. I like my rainbow coloured world and I want to keep it that way. If you disagree - just read the small script. My tanrum energy is over and I need to do work work. So the combing over the &lt;a href=&quot;http://dikini.net/php_design_patterns&quot;&gt;php design pattern collection&lt;/a&gt; will come later. For now enjoy, and have anger fits of your own.&lt;/p&gt;
&lt;p&gt;Oh yes. I&#039;m using a particular case of wishful thinking - using any php callable in $callable(...) expressions. The reality is a bit uglier. We need to use either call_user_func($callable,...); call_user_func_array($callable,...) How I wish php could do that out of the box, especially when it actually does it, just not in the short syntax. Anyone happy to hold my hand to do a php patch for that feature? I&#039;m very unfamiliar with the internals, sorry.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/26.07.2006/updated_design_pattern_collection&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/26.07.2006/updated_design_pattern_collection#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/ideas">ideas</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <category domain="http://dikini.net/tags/programming">programming</category>
 <pubDate>Wed, 26 Jul 2006 06:22:18 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">201 at http://dikini.net</guid>
</item>
<item>
 <title>Coroutines (generator based)</title>
 <link>http://dikini.net/coroutines_generator_based</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Abstract out naturally parrallel evaluation&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;Merge two functional streams&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;The coroutines can be implemented using generators. The example shows an example application&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;//if gen and gen2 are generators
function merge_data() {
  try {
    do {
      $data[] = 10*gen() + 5 + 4*gen2();
      ...
    } while( some_condition( ... ) );
  } catch ( $e ) { .... }
  return $data;
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/coroutines_generator_based&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/coroutines_generator_based#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:50:22 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">200 at http://dikini.net</guid>
</item>
<item>
 <title>Generator (iterator protocol based)</title>
 <link>http://dikini.net/generator_iterator_protocol_based</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Define a (stream like) value producer&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to be able do define functional streams&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Any object implementing an iteration protocol (interface) can be a base for a generator.&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
//a generator implemented with iterators
function generator($it=null, $cb=null) {
  static $itor;
  static $callback;

  if(!isset($itor)) ($itor = $it; $callback=$cb; }

  if($valid()) { $out = $callback($current); $next(); return $out; }

  //or if you are not sure about the type of the above callables ($valid, $next,..)
  if( call_user_func($valid) ) { 
    $out = call_user_func( $callback, $current); 
    call_user_func( $next ); 
    return $out; 
  }

  else throw( new some_exception );
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/generator_iterator_protocol_based&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/generator_iterator_protocol_based#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:46:42 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">199 at http://dikini.net</guid>
</item>
<item>
 <title>Iterator (based on protocol method)</title>
 <link>http://dikini.net/iterator_based_on_protocol_method</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to be able to postpone the effects of a computation, for example delay printing from a function to webserver output until whole web-page is assembled&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;This is an alternative implementation based on &lt;a href=&quot;http://dikini.net/protocol_method/protocol method&quot;&gt;protocol method&lt;/a&gt;, rather than interfaces&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
class alt_iterator {
  private $st = array();

  function next() { ... }
  function current() { ... }
  function valid() { ... }
  //... put the rest here

  function protocol() {
    return array(
             array($this,&#039;next&#039;),
             array($this,&#039;current&#039;),
             array($this,&#039;valid&#039;),
	     .....
	     );
  }
  
}

//an example use
function do_them($itor, $callback ) {
  list($next, $current, $valid, ...) = $itor-&gt;protocol();
  while($valid) {
    $callback( $current );
    $next();
  }
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/iterator_based_on_protocol_method&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/iterator_based_on_protocol_method#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:31:04 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">198 at http://dikini.net</guid>
</item>
<item>
 <title>Dynamic dispatch</title>
 <link>http://dikini.net/dynamic_dispatch</link>
 <description>&lt;dl&gt;
&lt;dt&gt;Intent&lt;/dt&gt;
&lt;dd&gt;dispatch the control based on the environment, but delay enumerating the cases&lt;/dd&gt;
&lt;dt&gt;Motivation&lt;/dt&gt;
&lt;dd&gt;we don&#039;t know at the time of writing the code what are the different cases to dispatch on&lt;/dd&gt;
&lt;dt&gt;Implementation&lt;/dt&gt;
&lt;dd&gt;(nearly) invisible in php&lt;/dd&gt;
&lt;dt&gt;Examples&lt;/dt&gt;
&lt;dd&gt;&lt;code&gt;$case($arg);&lt;br /&gt;
$object-&amp;gt;$method($arg);&lt;br /&gt;
call_user_func($calleable, $arg1, $arg2);&lt;br /&gt;
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Note&lt;/dt&gt;
&lt;dd&gt;A more elaborate form of dynamic dispatch is &lt;a href=&quot;http://dikini.net/fluent_interfaces_method_chaining&quot;&gt;Method Chaining&lt;/a&gt;&lt;/dt&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/dynamic_dispatch&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/dynamic_dispatch#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:23:19 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">197 at http://dikini.net</guid>
</item>
<item>
 <title>Lazy evaluation</title>
 <link>http://dikini.net/lazy_evaluation</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;A function/method call results in a promise for future computation&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to be able to postpone the effects of a computation, for example delay printing from a function to webserver output until whole web-page is assembled&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Instead of evaluating a function, we deliver a promise - a callable with no arguments, which when forced is executed&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
//for simplicity handle only functions with one argument 
class promise { 
  private $args = null;
  private $func = null;
  function __construct($func, $args) { 
    $this-&gt;func = $func; 
    $this-&gt;args = $args;
  } 
  function evaluate() { 
    call_user_func($this-&gt;func,$this-&gt;args);
  } 
} 

function delay( $func, $arg ) {
  return array(new promise($func, $arg), &#039;evaluate&#039;); 
}

//example use
...
$chunks[] = delay(&#039;a_printer&#039;,$a_variable);
...
foreach($chunks as $chunk ) {
  if(is_callable($chunk)) {
    $chunk(); //force - this does it&#039;s own printing in this example
  } else {
    print $chunk;
  }
}
&lt;/code&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/lazy_evaluation&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/lazy_evaluation#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 05:10:57 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">196 at http://dikini.net</guid>
</item>
<item>
 <title>Nested scope</title>
 <link>http://dikini.net/nested_scope</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Create a composable runtime hierarchy of data and behaviour&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want dynamic means of modifying complex runtime structures&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Use php &lt;em&gt;magic&lt;/em&gt; to dynamically dispatch requests to parent methods&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
class scope {
  var $parent;
  function __call( $name ) {
    if(isset($parent[$name]) { return $parent[$name](); }
    throw( new someException() );
  }

  function let( $name, $value ) {
    if(is_object($value)) { 
      //hygiene
      if(isset($this-&gt;$name)) { $this-&gt;$name-&gt;parent = null; }
      $this-&gt;$name = $value;
      $this-&gt;$name-&gt;parent = $this;
  }
}

//alternative let
function let($parent, $name, $class) {
  $parent-&gt;$name = new $class();
  $parent-&gt;$name-&gt;parent = array($parent,$name);
}

?&gt;

&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Notes&lt;/dt&gt;
&lt;dd&gt;&lt;ul&gt;&lt;li&gt;A concept related to and very often mistaken (mixed) (by me) with (for) &lt;a href=&quot;http://dikini.net/closure&quot;&gt;closures&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;the let method can be folded into &lt;em&gt;__set&lt;/em&gt; if we want this behaviour to the default for assignments to object fields&lt;/li&gt;&lt;/ul&gt;&lt;/dd&gt;&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/nested_scope&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/nested_scope#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 04:46:50 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">195 at http://dikini.net</guid>
</item>
<item>
 <title>Closure</title>
 <link>http://dikini.net/closure</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Create first class behaviours capable of storing state between use&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;We want to maintain the history of computation, an maybe save and reuse the computation at a particular point in time&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;The basic implementation is invisible in php - &lt;em&gt;static variables in functions&lt;/em&gt; (but that doesn&#039;t allow us to have the usual features of a language with closures because functions in php are not first class objects, i.e we can&#039;t copy them), &lt;em&gt;objects&lt;/em&gt; (they get us there, albeit more verbously). 
The examples below are borrowed from &lt;a href=&quot;http://ioreader.com/2007/05/03/php-closures/&quot;&gt;io-reader: php closures&lt;/a&gt;. Read the post and the full code to understand the full implementation.&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;// fixed-point combinator
function Y(Lambda &amp;$le) {
	return lambda(get_defined_vars(), &#039;Lambda $f&#039;, &#039;
		return $f-&gt;call($f);
	&#039;)-&gt;call(lambda(get_defined_vars(), &#039;Lambda $f&#039;, &#039;
		return $le-&gt;call(lambda(get_defined_vars(), \&#039;$x\&#039;, \&#039;
			return $f-&gt;call($f)-&gt;call($x);
		\&#039;));
	&#039;));
}
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Note&lt;/dt&gt;
&lt;dd&gt;Closures and objects are dual, compare &lt;em&gt;behaviour with state&lt;/em&gt; and &lt;em&gt;state with behaviours&lt;/em&gt;&lt;/dd&gt;
&lt;dt&gt;Yet another note:&lt;/dt&gt;
&lt;dd&gt;Alternatively have a look at &lt;a href=&quot;http://www.steike.com/code/php-closures/&quot;&gt;this implementation&lt;/a&gt; or &lt;a href=&quot;http://dikini.net/24.01.2006/emulating_closures_in_php&quot;&gt;my emulating closures in php blurb&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;Final note:&lt;/dt&gt;
&lt;dd&gt;All this is better avoided in php - too many underwater rocks&lt;/dd&gt;&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/closure&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/closure#comment</comments>
 <category domain="http://dikini.net/tags/closures">closures</category>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Wed, 26 Jul 2006 04:29:49 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">194 at http://dikini.net</guid>
</item>
<item>
 <title>State</title>
 <link>http://dikini.net/state</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Allow an object to alter its behavior when its internal state changes. &lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;An article in a typical editorial workflow exibits different behaviours in different publication stages. We want an object that hides the complex logic.&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Similar to the strategy pattern. Need to handle state change and behaviour change.&lt;/dd&gt;
 &lt;dt&gt;Examples&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
$new_state=&quot;edit&quot;;
$new_protocol=&quot;edit&quot;;

//change the behaviour
list($view,$update,$get, $set) = $object-&gt;protocol($new_protocol);
&lt;/code&gt;&lt;/dd&gt;
 &lt;dt&gt;Related patterns&lt;/dt&gt;
 &lt;dd&gt;&lt;a href=&quot;http://dikini.net/protocol_method&quot;&gt;Protocol Method&lt;/a&gt;, Builder, &lt;a href=&quot;http://dikini.net/strategy_pattern&quot;&gt;Strategy&lt;/a&gt;&lt;/dd&gt;
 &lt;dt&gt;Note&lt;/dt&gt;
 &lt;dd&gt;Too many different implementations exists. This means that there is probably a need to clean up the intent.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;&lt;a href=&quot;http://dikini.net/state&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/state#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 12:41:07 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">193 at http://dikini.net</guid>
</item>
<item>
 <title>Observer</title>
 <link>http://dikini.net/observer</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;When an object changes, notify all interested parties&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;events, synchronisation, ...&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Use &lt;a href=&quot;http://dikini.net/hooks&quot;&gt;hooks pattern&lt;/a&gt; &lt;em&gt;at&lt;/em&gt; and &lt;em&gt;after&lt;/em&gt; to implement the observer&lt;/dd&gt;
 &lt;dd&gt;No implementation needed in the Subject class&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;
//initialisation
$hs = new hooks();
$observer = new interested_party();
$change_method = array($object, &#039;setter&#039;);

//setup change point(can be done externally from $change method)
$hs-&gt;at(&#039;change&#039;, $change_method);

//express interest
$hs-&gt;after(&#039;change&#039;, array($obesrver, &#039;callback&#039;));

$hs-&gt;run($arg);
&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Note&lt;/dt&gt;
&lt;dd&gt;The precise implementation of the hooks may differ, depending on functionality, abstraction, sugar, ... Have a look at the drupal hooks as well.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/observer&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/observer#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 07:54:56 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">192 at http://dikini.net</guid>
</item>
<item>
 <title>Hooks</title>
 <link>http://dikini.net/hooks</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Build a function/method from multiple components&lt;/dd&gt;
 &lt;dt&gt;Motivation&lt;/dt&gt;
 &lt;dd&gt;Very often we need to decouple or delay definitions until run time&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;Implement a methods that allows executing a set of functions at predefined times&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;class hooks {
  private $cache = array();

  function run( $func, $arg ) {
    foreach(array(&#039;before&#039;,&#039;at&#039;,&#039;after&#039;) as $hook)
      foreach($this-&gt;cache[$func][$hook] as $f) { $f($arg);  }
  }

  function hook($hook, $func, $cb) {
    $this-&gt;cache[$func][$hook][]=$cb
  }

  function before($func, $cb) {
    $this-&gt;cache[$func][&#039;before&#039;][]=$cb
  }
  
  function at($func, $cb) {
    $this-&gt;cache[$func][&#039;at&#039;][]=$cb
  }
  
  function after($func, $cb) {
    $this-&gt;cache[$func][&#039;after&#039;][]=$cb
  }
  
  function around($hook, $func, $cb) {
    $this-&gt;cache[$func][&#039;around&#039;][]=$cb
    $this-&gt;cache[$func][&#039;after&#039;][]=$cb
  }

}

//and usage
$hs = new hooks();
$hs-&gt;hook(&#039;before&#039;,&#039;label&#039;,&#039;before_a_func&#039;);
$hs-&gt;hook(&#039;at&#039;,&#039;label&#039;,&#039;core&#039;);
$hs-&gt;run();

//or using the individual methods
$hs = new combinator();
$hs-&gt;before(&#039;label&#039;,&#039;before_a_func&#039;);
$hs-&gt;around(&#039;label&#039;,&#039;tracer&#039;);
$hs-&gt;run();
&lt;/code&gt;&lt;/dd&gt;&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/hooks&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/hooks#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 07:41:04 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">191 at http://dikini.net</guid>
</item>
<item>
 <title>Dynamic loading</title>
 <link>http://dikini.net/dynamic_loading</link>
 <description>&lt;dl&gt;
 &lt;dt&gt;Intent&lt;/dt&gt;
 &lt;dd&gt;Keep working set small and mainatin fast start time by loading features as required&lt;/dd&gt;
 &lt;dt&gt;Implementation&lt;/dt&gt;
 &lt;dd&gt;use &lt;em&gt;__autoload()&lt;/em&gt;&lt;/dd&gt;
 &lt;dt&gt;Example&lt;/dt&gt;
 &lt;dd&gt;&lt;code&gt;function autoload( $classname) {
  list($path,$file) = loadpath( $classname );
  require_once &quot;$path/$file&quot;;
}&lt;/code&gt;&lt;/dd&gt;
&lt;dt&gt;Notes&lt;/dt&gt;
&lt;dd&gt;&lt;ul&gt;
&lt;li&gt;we are loading a file, based on some conventions interpreted in loadpath&lt;/li&gt;
&lt;li&gt;This can be used to implement module-like system, but the lack of subclasses or similar tools makes the &#039;module&#039; bindings always transparent&lt;/li&gt;
&lt;/ul&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;&lt;a href=&quot;http://dikini.net/dynamic_loading&quot;&gt;read more&lt;/a&gt;&lt;/p&gt;</description>
 <comments>http://dikini.net/dynamic_loading#comment</comments>
 <category domain="http://dikini.net/tags/design_patterns">design patterns</category>
 <category domain="http://dikini.net/tags/php">php</category>
 <pubDate>Tue, 25 Jul 2006 07:21:47 -0400</pubDate>
 <dc:creator>vlado</dc:creator>
 <guid isPermaLink="false">190 at http://dikini.net</guid>
</item>
</channel>
</rss>
