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
  • Factory and Abstract Factory
  • Facade
  • Partial Evaluation

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

Singleton

design patterns | php
Intent
enforce the existance of a single object of a specified type
Motivation
Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.
Implementation
  • use the memoize pattern to implement a singleton
Examples
with functions
function &get_instance() { static $instance; if(empty($instance)) { $instance = new SingletonClass(); } return $instance; }
with objects
class Singleton { private static $instance = null; private static function __construct() { } public function get_instance() { if(empty(self::$instance) { self::$instance = new Singleton(); } return self::$instance; } }
Complications
The function based version implements the code equivalent of security by obscurity. It doesn't really enforce a single instance of a class, but a revised version can be used to abstract out singleton creation
‹ ProxyupState ›
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