Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113509 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 96596 invoked from network); 13 Mar 2021 23:11:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Mar 2021 23:11:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3DD351804C0 for ; Sat, 13 Mar 2021 15:04:09 -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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) (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 15:04:08 -0800 (PST) Received: by mail-ot1-f42.google.com with SMTP id w21-20020a9d63950000b02901ce7b8c45b4so3328907otk.5 for ; Sat, 13 Mar 2021 15:04:08 -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; bh=UmXVVHJUeg32o3JxhyrbIl184Ti546I6rg/MwyCXmoE=; b=UY4nI3YToXouLzJ5PgAH2UMdVGualW8QORER6tRUHluifz4WGHU3voW/TEpPu0hq9W KNPCeeY6TzkrjwjqDhWFqmMX7VSNW7FzWm59C38v2p+cZB0qLx3os5/GAFrFSXYDkycW v63fx+U6LkKEyTswFjCON0Sk89yA5k18Fxc4cFaCw1hhdksYohT9ii8s4sSGpBSyFm3p ebbtvwPcl0/vPByo30lBU06QmnoVXn/2WMmpGju4P0NMbZp7Wjkqs/1HV6Hu82tKui+y G2KT5jzxtXoQCdYXsLR4oVsIJC0UnymOK5Bbwmc+rshBNMX3g/MGY0jRgt1BY42A3ZrS Vu3A== 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; bh=UmXVVHJUeg32o3JxhyrbIl184Ti546I6rg/MwyCXmoE=; b=V7ZzcV8XQb1IeurN4u+rRevEoldySg4pUqX/v8oWL6HRmhJnSSAhkMp8lWrJjNZJho a/tKlNGXM5QM2KdwddUqAx8G35F4ayKqUvOMc5SXkFW8C7NIHArbaMjwTemhXvBWJlKf k+d/vs1icQXx9SMH4cy+CBIg9IomHSfYAEI5RJKv06++JTal91MDsyYlBgQShn1kesbB N5HJ3pbyPXRDBsznIREXqNJ2ftZQhGnqDQLTT0tzaoq4hfISWkIvnQogvxM5zqjdIz/d 8zCdp4JkvQTvlsjKObAc6EzSZKLg1X1GcsSYTcqVQa2ugzjyxR2TA82qwXX8LZXR1VU9 e+wQ== X-Gm-Message-State: AOAM533n8ZWAfVrTGKkkgWf1ZjClmsG7ENypFww/F1xBCF2dbveXKlpI NNYM1TKKO2SdU3qeGrl//qXb8Xw/9Y6K/PMir/LICBOy+NBPLw== X-Google-Smtp-Source: ABdhPJz0I1i8OdAfapmVfQFmWDD1ZMGg9lvCTf/I1EVTO4Hy0t3bEnW5HROmtOE6RwniEJDvZO+SEmwUm+NU2kXqNNY= X-Received: by 2002:a9d:5508:: with SMTP id l8mr5965144oth.233.1615676644955; Sat, 13 Mar 2021 15:04:04 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 13 Mar 2021 23:03:54 +0000 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000007e99b705bd7308d5" Subject: Re: [PHP-DEV] Built-in decorator attribute? From: davidgebler@gmail.com (David Gebler) --0000000000007e99b705bd7308d5 Content-Type: text/plain; charset="UTF-8" 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 >> > --0000000000007e99b705bd7308d5--