terminal-auspicious

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

PHP Design Patterns

  • A catalogue of php design pattern shorts
    • Accumulator passing
    • Closure
    • Control abstraction
    • Coroutines (generator based)
    • Dynamic dispatch
    • Dynamic loading
    • Facade
    • Factory and Abstract Factory
    • Fluent Interfaces/ Method chaining
    • Generator (iterator protocol based)
    • Generic function
    • Hooks
    • Interpreter
    • Iterator (based on protocol method)
    • Lazy evaluation
    • Memoization
    • Nested scope
    • Observer
    • Partial Evaluation
    • Protocol method
    • Proxy
    • Singleton
    • State
    • Strategy Pattern
  • Models, records, databases, friends and foes

Similar things

  • Learning lessons from Lisp or patterns and languages in PHP
  • Emulating closures in PHP
  • Some ways to use saved state with closures in php
  • Scoped php functions or more ways to abuse php
  • PHP Design Patterns
  • Strategy Pattern
  • Memoization
  • Singleton
  • Factory and Abstract Factory
  • Facade

Home » PHP Design Patterns » A catalogue of php design pattern shorts

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 ›
printer-friendly version | add new comment
Home » PHP Design Patterns » A catalogue of php design pattern shorts

dikini.net

spreading confusion by accident since 1970