Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113527 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 88914 invoked from network); 15 Mar 2021 01:41:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Mar 2021 01:41:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4F8451804B8 for ; Sun, 14 Mar 2021 18:34:38 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f52.google.com (mail-lf1-f52.google.com [209.85.167.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 14 Mar 2021 18:34:37 -0700 (PDT) Received: by mail-lf1-f52.google.com with SMTP id r3so46167517lfc.13 for ; Sun, 14 Mar 2021 18:34:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wmb0clNimnb+SxOSZs6fR5mlj3wyDwo+NOfhPxf0YdI=; b=MNiKONy9SC+FeY2VAQl954w6DT00jDmTRdDa5g0dMv/GDPgtNMxV2Ml1VpzJRrIFhq a0a++WTCjL0ElxSD87EkEcTblicE+Cv8jiZGjRV+4835qWe9gYNITB75PAtbslAAbqI1 q9upWqCqkxfMxLMev8JQKr5M3o3kor7TeJYNt+i+C/vH6YicK5vxlpxbDBcGBlSAT/0z UN0ZVatIC4B8k1OdELUxqDDLI08w7VdAsG57Ptad3J2eoLpWmw5LYYSIVGQgx8tDeJzi QCRvlVclJKk4KTMqGEMNeUCzWPqmlpIUDHeAVPQRtgoG6D7yllrh59b/eyI8vkTOzzqt 45Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wmb0clNimnb+SxOSZs6fR5mlj3wyDwo+NOfhPxf0YdI=; b=AEg/9YUxuwXvCsYOfI1f9EKdHBxdtY0dMjelNgj3o4qOvrKL4kQUIR1UAhqDSeQTWy kwfUXA957/Dk2e/LFS2vQwf49aRlCsPbaW+xbHfft2vjVMBXK2Qykoqd4uYiHbStcBcu KqLVdGWM99j1VS25KYOv9vDA2mq5slPVblzDeQ0AdDTb/fCmDrquVQCpN+kkAGADI1Et pdpxIKhjzkGG8KArvYZMOocmX5U5nt8gRbi7FEnyUvFx9UW8/6KqUIhHharSQW5DpUcC TthXiYHoFQbqjeFPWa0F7Gsjoj8pGVnJgh5kW+QbfbBWEIgz9CxQBpmoSe7H9Ci8Gn5o TYxA== X-Gm-Message-State: AOAM533gVH5h+xMwjpKdNppJCYCsSdFwv8rgiLWIcgFrX1bOyue2yYLb joBcVh3p63wRIsB66KCRFLNOZhqsaBADrECX8pE= X-Google-Smtp-Source: ABdhPJwLjKZEVaWDVlLyP5TbRKtY83yaGWnlqVc6KDeMu7NxWVnHwlCoOMEfkwHKjcaNho9MLh5IJn87MkWEw97zmnI= X-Received: by 2002:a19:b47:: with SMTP id 68mr6148234lfl.343.1615772074003; Sun, 14 Mar 2021 18:34:34 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 14 Mar 2021 18:34:22 -0700 Message-ID: To: David Gebler Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000824f2c05bd894004" Subject: Re: [PHP-DEV] Built-in decorator attribute? From: sarkedev@gmail.com (Peter Stalman) --000000000000824f2c05bd894004 Content-Type: text/plain; charset="UTF-8" On Sun, Mar 14, 2021 at 5:34 AM David Gebler 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 --000000000000824f2c05bd894004--