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

Hooks

design patterns | php
Intent
Build a function/method from multiple components
Motivation
Very often we need to decouple or delay definitions until run time
Implementation
Implement a methods that allows executing a set of functions at predefined times
Example
class hooks { private $cache = array(); function run( $func, $arg ) { foreach(array('before','at','after') as $hook) foreach($this->cache[$func][$hook] as $f) { $f($arg); } } function hook($hook, $func, $cb) { $this->cache[$func][$hook][]=$cb } function before($func, $cb) { $this->cache[$func]['before'][]=$cb } function at($func, $cb) { $this->cache[$func]['at'][]=$cb } function after($func, $cb) { $this->cache[$func]['after'][]=$cb } function around($hook, $func, $cb) { $this->cache[$func]['around'][]=$cb $this->cache[$func]['after'][]=$cb } } //and usage $hs = new hooks(); $hs->hook('before','label','before_a_func'); $hs->hook('at','label','core'); $hs->run(); //or using the individual methods $hs = new combinator(); $hs->before('label','before_a_func'); $hs->around('label','tracer'); $hs->run();
‹ Generic functionupInterpreter ›
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