Re: [Pre-RFC] Support for decorator patterns
| From: | Rowan Tommins | Date: | Fri, 14 Feb 2020 11:55:40 +0000 |
| Subject: | Re: [Pre-RFC] Support for decorator patterns | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108570@lists.php.net to get a copy of this message | ||
On Fri, 14 Feb 2020 at 11:09, Rowan Tommins <rowan.collins@gmail.com> wrote:
>
> That would also cover the fluent interface case:
>
> after delegate setFoo {
> $this->delegated = $return;
> return $this;
> }
>
I just realised that this could be easily extended to share an
implementation across multiple methods, making a really succinct decorator
definition:
class FluentThingDecorator implements FluentThing {
private int $setterCount=0;
private int $getterCount =0;
private delegate FluentThing $delegated;
public function __construct(FluentThing $delegate) {
$this->delegated = $delegate;
}
public function getSetterCount() {
return $this->setterCount;
}
public function getGetterCount() {
return $this->getterCount;
}
after delegate getFoo, getBar, getBaz {
$this->getterCount++;
return $return;
}
after delegate setFoo, setBar, setBaz {
$this->setterCount++;
$this->delegated = $return;
return $this;
}
}
It might be useful to have access to the name of the function actually
called, e.g. for an audit log decorator; maybe the de-sugaring could happen
early enough that __METHOD__ took on the right value for each case.
Regards,
--
Rowan Tommins
[IMSoP]