Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113503 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78386 invoked from network); 13 Mar 2021 19:20:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Mar 2021 19:20:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DACD21804B4 for ; Sat, 13 Mar 2021 11:13:12 -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-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) (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 11:13:12 -0800 (PST) Received: by mail-oi1-f180.google.com with SMTP id w65so30441677oie.7 for ; Sat, 13 Mar 2021 11:13:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=/XcOWB6mYSLnkh7elgrtkesyczon/vmCROxORjU19PQ=; b=hGd1XdPXP/pN5S0EGC2KW6H4YBKIMqKU+vyxMoOXb52/3g1bMIaJDgexubc2+aFA4j ONmHenvBPNkyyemFb0daKBi2ITyUHa0ORIXyxKNZmASIQ/65VApdIYxhKVAs559gV5BZ aB0dy2U7DEOrFVlYGTgmcHOyQquMoDU8n/kg0j8LPCcHK0UNr6u9qFqUPa8bD6rdJh0R mTB1Mpv3OAwfcXusO8+2dm6QZXruIsIcNTUPi1j40WKMTu3XbB/IceOZE+gjpk+T0Xwm LvCnYhyoi6fWMFlFr7Qq8wD107Kr4DHSos8fE6tjVzJ4G0H369zBSb2hLC4h7LNJvU0b kZBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=/XcOWB6mYSLnkh7elgrtkesyczon/vmCROxORjU19PQ=; b=b17dEpA6f9HwW6EaH5i2jBxwbt7TVtWbJJGkr6la44vGQ7Lup5F1WBZO2Zg8HLCIuq QrLKu1Rx7hsnd7LWkF9I17mbZ5K+tK704ABM3q30bJZgTKZmMsoVRGuQA/z5huzSfKJR KwgiyXmpn2dhMlvtUvrmZLVp9XrQodkqUe82sHJeWWWRxSFvqRWM+gNORWKGrdLUcl2p PVn7oWjKq8ItwHYvIIgtGb7JBuBtHAclSowo5yL2K2yf8SXruHOdlXUon050megsJQ35 idPHUkjSrhOJMgf1B/uUOYh0LwqhsUsbrD93j9Gpn1Dlm5HUPIq2TwoxaZkoRdp2uxBR +43A== X-Gm-Message-State: AOAM5323vBUl8orxOWmwSM0PkESq2wiIxWVkw1W8TSPCD1YAGSTDElmS 8Nv3jbgT5RbbJ0DDwp4Ib4kmx2fRVttt3i31nnRooH0GRuc= X-Google-Smtp-Source: ABdhPJz5Jy9X0pHnU7z7xK+uskWAKo0YZKVSym4OyNrtKyz3A/x0MA0+/1aMW0A1tpo8HcXDJxWjhMvTduSsFgxxa2Y= X-Received: by 2002:aca:59d7:: with SMTP id n206mr13690428oib.7.1615662790957; Sat, 13 Mar 2021 11:13:10 -0800 (PST) MIME-Version: 1.0 Date: Sat, 13 Mar 2021 19:13:00 +0000 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000bb5bfb05bd6fcee9" Subject: [PHP-DEV] Re: Built-in decorator attribute? From: davidgebler@gmail.com (David Gebler) --000000000000bb5bfb05bd6fcee9 Content-Type: text/plain; charset="UTF-8" Oops, didn't tag the subject. On Sat, Mar 13, 2021 at 4:51 PM 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 > --000000000000bb5bfb05bd6fcee9--