Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113511 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27864 invoked from network); 14 Mar 2021 10:35:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Mar 2021 10:35:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 95B9B1804B4 for ; Sun, 14 Mar 2021 03:28:36 -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-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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 03:28:36 -0700 (PDT) Received: by mail-lf1-f50.google.com with SMTP id r3so44192424lfc.13 for ; Sun, 14 Mar 2021 03:28:36 -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=+/g7Gd2PEZqVTZEcMd/YwKCZRjsJRs93K4N3Y9ce9zQ=; b=sqoGtNWy1foa/gUxU5IjibN9bGKG3JsCh4GNyPk8a5IA2+5a9s3Tgk6X0V1iVdmkSZ FmWQGCXVA58OZxYbzF9DyeidhLSbzkBG3bqNRpLrb6ydl1LLsiNZIeqDxLl+VuwinBon 7DGSFPMhJpw9dJpMY3HmvmTsCIJ/Hqk818VRm1vPTZLzaYN1LNKpoSZmJ10kwkEbMv4p Pd4WirkdzCtefePfOi+5UO4yEh7OEGpnsyGneq8IxVYeH/NizoR7Og2NyFSL96hCJmMH yrJmlHB6v2chcOPt5LVmopOohFQBPVui5fLmos85jEoW4mJ71FDaHNoxpeRlij0qmX4R ttdw== 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=+/g7Gd2PEZqVTZEcMd/YwKCZRjsJRs93K4N3Y9ce9zQ=; b=qu9AJLgXuR0pTboeGEW1uj6V+hwuXKdXRNeiD96iXHQaL1myNXA3cSx/OGUVK2lcRg nPc/7hx8oL1bJZeyTxK/UB1BmHNkbMNIroifjLYSCCuZTVc5CB8ZddpMlbHJ/CfLb2rZ q3bx1q1N/BB3oo3iguCzrQ42rl3V+1h9RVnD4JOHradGWCf1kv1PjKs6QXM/ZObiN1xz SMBHigy1l18IgF+hJpjyOS4eu7QFEW0QsjvtDQBI5jhnJAGGfFCKbr7aiNZUVASWE52G QywAQv5gykKqDnJpAB93fXf0siaR2ytEBBOLNnGn8T9WauwGX3Bmd4vpnjKCMa2LzfgU WR8g== X-Gm-Message-State: AOAM5311vuzrPP9CKELJiZrb2X2BP5BJOGhgV/KEHoK5+n4z2EYeHs4T UQVA0PCdIrhHV+VYOmyiKAaq3MKG2ttxikB6e3w= X-Google-Smtp-Source: ABdhPJzo4GkLqIigD6qTMNhbjMfHWEkYBCxSMsZrLJMDWgq9FWvzbF9wRaIgIIhv24Dpo8Jf6Qm1TpMh0i/Xe2PrZSg= X-Received: by 2002:a19:6b13:: with SMTP id d19mr4765608lfa.291.1615717713561; Sun, 14 Mar 2021 03:28:33 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 14 Mar 2021 03:28:20 -0700 Message-ID: To: David Gebler Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000005fcc2205bd7c9829" Subject: Re: [PHP-DEV] Built-in decorator attribute? From: sarkedev@gmail.com (Peter Stalman) --0000000000005fcc2205bd7c9829 Content-Type: text/plain; charset="UTF-8" Hi again David, I've been thinking about this some more, it could be quite useful. I'd like to propose a bit more of a Attributes-y syntax: ``` start = microtime(true); // these could be made available $foo = $this->getObject(); $args = $this->getArguments(); // [$a, $b] $ref = $this->getReflection(); } public function __destruct() { $end = microtime(true); $total = $end - $start; echo "Executed {$this->name} in $total second(s)\n"; } } class Foo { #[Timer] public function bar($a, $b) { ... } #[Timer('Bob')] public function baz($a, $b) { ... } } ``` Thanks, Peter On Sat., Mar. 13, 2021, 15:04 David Gebler, wrote: > Decorators are a way of bringing aspect oriented programming into PHP core, > yes, among other uses. Go AOP is a fairly bulky framework which could be > easily replaced by a Decorator attribute for the purposes of cross-cutting > changes to function behaviour. > > Regards, > David > > On Sat, Mar 13, 2021 at 10:51 PM Peter Stalman wrote: > > > Hi David, > > > > This sounds a lot like Asect Oriented Programming. Have you looked into > > that? > > > > PHP framework: > > https://github.com/goaop/framework > > > > PECL extension: > > https://aop-php.github.io/ > > > > Thanks, > > Peter > > > > > > > > On Sat., Mar. 13, 2021, 08:51 David Gebler, > wrote: > > > >> With the introduction of attributes in PHP 8, this new behaviour is > still > >> quite sparsely documented. Some of the articles I've seen out there, > >> though, liken PHP's attributes to similar constructs in other languages > >> including decorators in Python. > >> > >> Attributes are not the same thing as (Python's concept of) decorators > and > >> they shouldn't be confused; a decorator is a function which wraps > another > >> function and is automatically called in place of the wrapped function. > >> > >> This isn't currently possible in PHP. Using frameworks like Symfony, we > >> can > >> start to build things like this: > >> > >> class UserProfileController { > >> #[LoginRequired] > >> public function editProfile(...) { } > >> } > >> > >> but the logic of enforcing our "require the user to be logged in" > >> decorator > >> relies on the surrounding framework controlling the flow of execution, > >> reading the attribute and deciding whether to call the decorated method > >> editProfile() at all. > >> > >> What we *can't* do is something like this: > >> > >> class Foo { > >> private function timer(callable $wrapped) > >> { > >> $start = microtime(true); > >> $wrapped(); > >> $end = microtime(true); > >> $total = $end - $start; > >> echo "Executed function in $total second(s)\n"; > >> } > >> > >> #[timer] > >> public function bar($a, $b) { ... } > >> > >> #[timer] > >> public function baz($a, $b) { ... } > >> } > >> > >> What I'm wondering is whether there's a desire / interest for a built-in > >> attribute to provide this kind of behaviour modification. > >> > >> I'm thinking something like > >> > >> class Foo { > >> private function timer(callable $wrapped) { ... } > >> > >> #[Decorator([self::class, 'timer'])] > >> public function bar() { > >> echo "Bar"; > >> } > >> } > >> > >> Where this would result in any call to $foo->bar() being equivalent to > as > >> if the above were defined as: > >> > >> class Foo { > >> private function timer(callable $wrapped) { ... } > >> > >> public function __bar() { > >> echo "Bar"; > >> } > >> > >> public function bar() { > >> return $this->timer([$this, '__bar']); > >> } > >> } > >> > >> I'm not saying I have the skills to implement this attribute (though I'd > >> happily try), I'm not even in a position to propose a draft RFC at this > >> stage, just throwing the idea out there to get a feel for what people > >> think > >> of the concept? > >> > >> Regards, > >> Dave > >> > > > --0000000000005fcc2205bd7c9829--