Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120488 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68567 invoked from network); 31 May 2023 19:27:32 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 May 2023 19:27:32 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D501A180511 for ; Wed, 31 May 2023 12:27:31 -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=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 31 May 2023 12:27:31 -0700 (PDT) Received: by mail-yb1-f180.google.com with SMTP id 3f1490d57ef6-bad97da58adso6908380276.1 for ; Wed, 31 May 2023 12:27:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=beberlei-de.20221208.gappssmtp.com; s=20221208; t=1685561250; x=1688153250; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=/hveODUITcBIewBYZjHbpx1weuqGyVm6iKQNlJQ8r44=; b=3DLhUPsErM9xINc9v1zUVgNPZ5tUU0rnY6Co11qK7ehfXiuxAMVDtxsG6+l8XmAuhO UbxlTPOrNg/bJSAnQb9HvBcU377tRyU7qL3/shaNR3eF3F0cITTrIv5DTrGTTLVBVmF1 Xt7ljt0i5JRKWzCgqpbdpllcVh6vrgEn+/jT8Qj2+p1NjLI3DU8Bk5SFYx8HmQLltWfh V/hOqQrNpASiF/KxIgkJ1gixAj64GD0xZ2PrQA82iIfzr/vKkFakTXby8GQO/9fEhTSL u5oVyCZvo4YbzR9baHPbR9XFA3gnH1GnMTol/5mlyaqfHExOluOQKxyqUuH1lg8GlFJO JzrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685561250; x=1688153250; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/hveODUITcBIewBYZjHbpx1weuqGyVm6iKQNlJQ8r44=; b=Ek+eRf8sYBonyo8VF8o/KR7BD4moCFSYMa9s72u3q205eLEN+hDgKwS0MalpO5YDGx PK4KD7qa+kUIXnmme4Gz1qydmwlEhHHpOaYo1ElmWVPtQhrUfCH3AZtx66Bu0rSEQA7I IkSECio9mDBieMvF+dzscDh+qt+9eptY7pZ4Q901Ccw6YVE/iaf1yLmcOQ70qg8viInE /Re9WhLCl/RNTLdafAPFB/I2FaagVSXxbsWwy2j1332IfHzOx8HMYbLeCksIifbc9fhc EzROXIIsiDjnmrk/MZXVNbc7d3fWVbddLNTv2++Q1OThM1NUmlYdzfFHhOuyJty2hmQI Ooeg== X-Gm-Message-State: AC+VfDwGxUmykK+fslOJZgjBAVIAGXm+NFL5r69cv/RXY9fHIRW0sHO2 p66yMATzV2lplgk6ckMPpfEui71KZJ6H2KZWfYt4Vw== X-Google-Smtp-Source: ACHHUZ5/Yy6FfjqqlDydMHggtPOS4s30k9vz98nhspTHd+WUsC3XFVgpWarxg8A7gM1SbXTeAOCYsRE3D5EhzM7DOvo= X-Received: by 2002:a25:264b:0:b0:ba8:4859:3eb1 with SMTP id m72-20020a25264b000000b00ba848593eb1mr7583407ybm.42.1685561250425; Wed, 31 May 2023 12:27:30 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 31 May 2023 21:27:19 +0200 Message-ID: To: Andreas Hennings Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000094443305fd024f6f" Subject: Re: [PHP-DEV] Declaration-aware attributes From: kontakt@beberlei.de (=?UTF-8?Q?Benjamin_Au=C3=9Fenhofer?=) --00000000000094443305fd024f6f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, May 30, 2023 at 2:49=E2=80=AFAM Andreas Hennings wrote: > Hello internals, > I am picking up an idea that was mentioned by Benjamin Eberlei in the pas= t. > https://externals.io/message/110217#110395 > (we probably had the idea independently, but Benjamin's is the first > post where I see it mentioned in the list) > > Quite often I found myself writing attribute classes that need to fill > some default values or do some validation based on the symbol the > attribute is attached to. > E.g. a parameter attribute might require a specific type on that > parameter, or it might fill a default value based on the parameter > name. > > Currently I see two ways to do this: > 1. Do the logic in the code that reads the attribute, instead of the > attribute class. This works ok for one-off attribute classes, but it > becomes quite unflexible with attribute interfaces, where 3rd parties > can provide their own attribute class implementations. > 2. Add additional methods to the attribute class that take the symbol > reflector as a parameter, like "setReflectionMethod()", or > "setReflectionClass()". Or the method in the attribute class that > returns the values can have a reflector as a parameter. > > Both of these are somewhat limited and unpleasant. > > I want to propose a new way to do this. > Get some feedback first, then maybe an RFC. > > The idea is to mark constructor parameters of the attribute class with > a special parameter attribute, to receive the reflector. > The other arguments are then shifted to skip the "special" parameter. > > #[Attribute] > class A { > public function __construct( > public readonly string $x, > #[AttributeContextClass] > public readonly \ReflectionClass $class, > public readonly string $y, > ) {} > } > > $a =3D (new ReflectionClass(C::class))->getAttributes()[0]->newInstance()= ; > assert($a instanceof A); > assert($a->x =3D=3D=3D 'x'); > assert($a->class->getName() =3D=3D=3D 'C'); > assert($a->y =3D=3D=3D 'y'); > > Note that for methods, we typically need to know the method reflector > _and_ the class reflector, because the method could be defined in a > base class. > > #[Attribute] > class AA { > public function __construct( > #[AttributeContextClass] > public readonly \ReflectionClass $class, > #[AttributeContextMethod] > public readonly ReflectionMethod $method, > ) {} > } > > class B { > #[AA] > public function f(): void {} > } > > class CC extends B {} > > $aa =3D (new ReflectionMethod(CC::class, > 'f))->getAttributes()[0]->newInstance(); > assert($a->class->getName() =3D=3D=3D 'CC'); > assert($a->method->getName() =3D=3D=3D 'f'); > > --- > > Notice that the original proposal by Benjamin would use an interface > and a setter method, ReflectorAwareAttribute::setReflector(). > > I prefer to use constructor parameters, because I generally prefer if > a constructor creates a complete and immutable object. > Thank you bringing this up, the more I work with attributes the more often this comes up. I think when we designed the attributes there was just so little concrete exprimentation that we didn't pick this up as a serious missing gap. As for implementation, reviewing the whole e-mail thread, i like both: 1, ReflectionAttribute::getReflectionTarget() - this we should add no matter what and is a no brainer 2. An argument attribute that instructs newInstance() to inject the reflector or the ReflectionAttribute, for example #[AttributeContext] or #[AttributeTargetReflector] > > ---- > > Thoughts? > > -- Andreas > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --00000000000094443305fd024f6f--