Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113508 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 94750 invoked from network); 13 Mar 2021 22:58:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Mar 2021 22:58:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A457E1804DF for ; Sat, 13 Mar 2021 14:51:49 -0800 (PST) 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-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (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 ; Sat, 13 Mar 2021 14:51:49 -0800 (PST) Received: by mail-lj1-f182.google.com with SMTP id u10so386541lju.7 for ; Sat, 13 Mar 2021 14:51:49 -0800 (PST) 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=8756ruo3IqrnFeE+/RhIeyuIQho/8rrQ8syDNj8Hm0Y=; b=Ftu4OIg3J7Tz8xBcleBIZxWeouVwe8ig39yf1WBBWTTkhSPxOw/pRtX+rFKWp1RjJD v7dVtQAeqnMvq2QXFQ0rhSP82GtM/vRHRLhBPpUGpAMrI6CXp8RiJ7EaIrJ26wuEOcY+ Ut4okdWZBJdr2SN49Ns/I8Toub+ziyRau/dfVDPmTjw19KC4U3GhtnFlcwa/SV0hy30U VpdzkPqpIyZDGrZFobRCvo3DnpwVzFDB4PQv5zHvDMxvS0Xx3RHqglGAnh28VmPi99PQ EQ/H0WlRk/mg9oEax104GrF73RYSL8qGzjjsYh27SEe2TpZXOnFHP5AH0X+BuyJ9vSJn Flrw== 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=8756ruo3IqrnFeE+/RhIeyuIQho/8rrQ8syDNj8Hm0Y=; b=qaYR7LDGjKLsyyAlmP1gtwiyYWwQLVgN66zERJHzOmyR4Mvk5KrOzANZofyUTzDJPN k6kUJVBOTsZ4KZST+b91FcFMQyYDvX6aYWsAkYOJZg6N6I/NGSHQ+A+SQYCqxEprZfm5 0skncoaD1luGEyOGIOnxRh2z96dL1a51LkC2BLwVZtSinbgys0Pqn4ITDIJm0y8xaiTQ ULrbiUtJ3bB6ACfXdQctII5RMR8UiRtNMaQQLwF1y5kHqSTa6WW/XEhXFTpnMFNZzYPe Aq4uPE+21wwlErXODk8aPka1Or2FT5t4NltYo56gM2LZURZGv+0YCEDogxVW3EyiXWF6 lXng== X-Gm-Message-State: AOAM531sE/POHs3973DfJOf3iy2DQz0aQNw9wQIdzj0KWMsEHNu0gZYy W6m+FpXAYNkQbvPAW5cb5kLph8YIhb26Sk3OwJg= X-Google-Smtp-Source: ABdhPJxd+sRfktnAdgMRR3+0IWdlsIcHIOYEGj0tmoAhWs7GBAHrjFf7cgNoToH6Tj6VzL64wenvpfrBfBQFAOc0FMU= X-Received: by 2002:a2e:8984:: with SMTP id c4mr6056648lji.456.1615675907566; Sat, 13 Mar 2021 14:51:47 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 13 Mar 2021 14:51:34 -0800 Message-ID: To: David Gebler Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000008af24d05bd72dc0f" Subject: Re: [PHP-DEV] Built-in decorator attribute? From: sarkedev@gmail.com (Peter Stalman) --0000000000008af24d05bd72dc0f Content-Type: text/plain; charset="UTF-8" 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 > --0000000000008af24d05bd72dc0f--