Topics

programming

php drupal scheme scheming macros design patterns da la

design

design css

random thoughts

scribbles

alter ego

other me 'em that link us my space me linked in

Collections

Programmable web
PHP design patterns

guild

Closure

closures | design patterns | php
Intent
Create first class behaviours capable of storing state between use
Motivation
We want to maintain the history of computation, an maybe save and reuse the computation at a particular point in time
Implementation
The basic implementation is invisible in php - static variables in functions (but that doesn'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't copy them), objects (they get us there, albeit more verbously). The examples below are borrowed from io-reader: php closures. Read the post and the full code to understand the full implementation.
Example
// fixed-point combinator function Y(Lambda &$le) { return lambda(get_defined_vars(), 'Lambda $f', ' return $f->call($f); ')->call(lambda(get_defined_vars(), 'Lambda $f', ' return $le->call(lambda(get_defined_vars(), \'$x\', \' return $f->call($f)->call($x); \')); ')); }
Note
Closures and objects are dual, compare behaviour with state and state with behaviours
Yet another note:
Alternatively have a look at this implementation or my emulating closures in php blurb
Final note:
All this is better avoided in php - too many underwater rocks
‹ Accumulator passingupControl abstraction ›
add new comment

Reply

Please solve the math problem above and type in the result. e.g. for 1+1, type 2
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <br /> <br> <div> <a> <em> <strong> <cite> <pre> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <img> <blockquote> <q> <strike> <small> <h4> <h5> <h6>
  • Link to content with [[some text]], where "some text" is the title of existing content or the title of a new piece of content to create. You can also link text to a different title by using [[link to this title|show this text]]. Link to outside URLs with [[http://www.example.com|some text]], or even [[http://www.example.com]].
  • Lines and paragraphs break automatically.
More information about formatting options
Home » Closure

dikini.net

spreading confusion by accident since 1970