On Monday 19 November 2007, Edward Z. Yang wrote:
> Larry Garfield wrote:
> > It sounds like you want to be using decorators instead.
>
> The decorator pattern is inappropriate for this case, because Sam wants
> to extend the interface, not change the behavior of an existing one.

class AbstractField {
// ...
}

class InputField {

  protected $field;

  function __construct($field) {
    $this->$field = $field;
  }

  function __call($method, $args) {
    return call_user_func_array(array($this->field, $method), $args);
  }
  // Other methods.
}

class DBField {

  protected $field;

  function __construct($field) {
    $this->$field = $field;
  }

  function __call($method, $args) {
    return call_user_func_array(array($this->field, $method), $args);
  }
  // Other methods.
}

$myfield = new InputField(new DBField(new AbstractField(...)));

This is a case where you don't want type-hinting, actually. :-)  You could do 
the same thing with an AbstractField interface that DBField and InputField 
also implement and pass through, which might be faster because you eliminate 
the _call()/call_user_func_array() choke point, but it's more code to write.  
Pick your poison.

-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to