Re: Multiple class inheritance
| From: | Larry Garfield | Date: | Mon, 19 Nov 2007 06:44:25 +0000 |
| Subject: | Re: Multiple class inheritance | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-33288@lists.php.net to get a copy of this message | ||
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
larry@garfieldtech.com 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