- 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
Proxy
by vlado on Thu, 2006-07-27 15:20- Printer-friendly version
- Login to post comments