On Sun, Mar 14, 2021 at 5:34 AM David Gebler <davidgeb...@gmail.com> wrote:
> Before and after hooks are a similar but slightly different implementation, > semantically, to what I was originally suggesting but may be the preferred > option. I think to be useful, you probably still need some way to choose > not to call the original, decorated function - but I guess you could just > throw an exception in your before hook and that might be sufficient control > in practice. > I could be as simple as `return false;` as well, since https://wiki.php.net/rfc/make_ctor_ret_void was declined. As you mentioned one issue is getting the return value of the decorated function, which would be easier for a wrapper type of implementation. Before/after works well for timing and profiling, but wrapping works better for things like caching. Would it make sense to have both options? Some things to consider: 1) Wrapper and/or before/after pattern? As discussed. 2) Can Decorators take arguments? I would think this should be included, i.e. `#[Timer(1)]`. How would it be conveniently available and separate from the wrapped function arguments? 3) Can Decorators be used on Decorator functions? ``` #[Decorator] class MyDecorator { #[MyDecorator] public function wrapper(callable $callable, $args = []) { /* */ } } ``` 4) Can Decorators be used on Generator functions? ``` #[Decorator] class MyDecorator { public function wrapper(callable $callable, $args = []) { yield 1; yield 2; return call_user_func_array($callable, $args); } } ``` 5) Can Decorators be used recursively? ``` #[Decorator] class MyDecorator { public function wrapper(callable $callable, $args = []) { if (true or false) return $this->wrapper($callable, $args); return call_user_func_array($callable, $args); } } ``` Thanks, Peter