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

Proxy

design patterns | php
Intent
Provide a surrogate or placeholder for another object to control access to it
Motivation
We want to provide a wrapper around a set of classes to unify their access protocols
Implementation
  • Provide a wrap method
  • intercept calls to the wraper object and forward them accordingly, may change the protocol
Example
class Fluenter { private $obj; function __construct($obj) { $this->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->obj, $method), $args); if (is_null($result)) return $this; else if (is_object($result) and ($result instanceof $fluent)) return $result; else throw new RuntimeException( "Fluent::__call called method $method ". "and expected a null return or a non-fluent object, ". "got (".gettype($return).") $return instead."); } }
notes
  • This code is an unaltered copy from Making Fluent Interfaces Readable in PHP, I hope Jonnay doesn't mind that
  • You might want to check out fluent interfaces( method chaining) and accumulator passing
‹ Protocol methodupSingleton ›
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 » Proxy

dikini.net

spreading confusion by accident since 1970