Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128237 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 68BBE1A00BC for ; Sat, 26 Jul 2025 11:42:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753530024; bh=Bgu0zJE0VC84nQCMbC2xyb5BvqGhsN3e2LQFC1ikDKE=; h=Date:From:To:In-Reply-To:References:Subject:From; b=AjZGjKrKjui1DpxX2ss72pjOFbSQ5LuAvdPfIKceJBgaqhCwdHbFpFI1F9/reCd24 jti69uvEtpfa52YNC1IRuxpSxmm7R+yBWjKbwjgMTn/+0AeZbRDn7+Q1v1c8nmGkmv W5TSFf5K9lVYcWwogmMkvj7edH1zQB+YHrQpfBc9o7PwLulRP7gEfzQZGfymNefdZD aUmZGdirjtnH5oAATfyi38wn+3PVUVMecWnBs7E5KmfGm7wZ8QUZsxUvmyiYPHt8DN vGzBmSCAJpsbWdKB+x5UiHdYQivpOGIxnj/u/UEzU/d10wvkZ1EuZ9HSyOFnMe4ix/ 3O3liN8uGqOWA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 63096180057 for ; Sat, 26 Jul 2025 11:40:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fout-a6-smtp.messagingengine.com (fout-a6-smtp.messagingengine.com [103.168.172.149]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 26 Jul 2025 11:40:23 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id 84871EC0226; Sat, 26 Jul 2025 07:42:06 -0400 (EDT) Received: from phl-imap-05 ([10.202.2.95]) by phl-compute-05.internal (MEProxy); Sat, 26 Jul 2025 07:42:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1753530126; x=1753616526; bh=Bgu0zJE0VC 84nQCMbC2xyb5BvqGhsN3e2LQFC1ikDKE=; b=dMv3gC2VWd0zPFtOi9ajxU+JNA WBl3tAwbYS86rbLIK8PJV8aT0OdFE4+cJlch8ND7ibgXpJijUMxH89KBNqiTbzp0 N2clv+I9mtUWQbNe6vX4rQZl352zyckSVWcTeYxABSEg5bSFrDW6aBH/ZpHMe1iI j64whLZ/wafRAfBPxVRZZ87FU0WvfCfb1jaWZC4848gSUKvk6BzPBVC4gjVLVEyv fAKb5vlS2ppUxCIOBmStIzRDZQIsAx6x3G3VwpjbKuBDkQ6oC7sIFKbGylH6Lyib 6k8ekVNdAhdZ2A5cuFR4PdpHfX9OdiDQk/ZDJxK0FtJPLVtw5uZDG+lATL9A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1753530126; x=1753616526; bh=Bgu0zJE0VC84nQCMbC2xyb5BvqGhsN3e2LQ FC1ikDKE=; b=MaQC/FqpKbRoxsffdWrnwnREY9K2TuHRHiz5MDuLHi7Y8e3/IfF rS9xg8bHJXQ/T0za0PTZjpAeyBBc4hkqQ3GQMemqamezyut4QydNWW8/Hj620crs ufG6Djco29RP4DpnoO/konPuM3cFAF4V4Rh8UO+tec+yIRp0QLCaSnTNmTFpzOxw o7Z8eLzUYbnL0T534LT2VLhv3tF/Bssxe94iUmNe+MpTl+KeTT6cOJliZtpsKNXK fBcrG5+WR1jbdpaDXtXhJhOzr/YH8TeAe1IhdHX3FSJHxRcMFdZZZhFfft2sQ6TK 5ve9U8E6zgfOw3lFzSCYlwd7W10HvsBU3TQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdekieefvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefoggffhffvkfgjfhfutgesrgdtreerredtjeenucfhrhhomhepfdftohgsucfnrghn uggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtffrrghtthgvrh hnpeekleelgeejhffhgfevleekuddtkeduveduveeutdetheeghfdtkeffvdejlefgteen ucffohhmrghinhepphhhphdrnhgvthdpfehvgehlrdhorhhgnecuvehluhhsthgvrhfuih iivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothhtlhgvugdrtgho uggvshdpnhgspghrtghpthhtohepfedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoh epjhhurhhishesghhlrghivhgvrdhprhhopdhrtghpthhtohepuggrnhhivghlrdgvrdhs tghhvghriigvrhesghhmrghilhdrtghomhdprhgtphhtthhopehinhhtvghrnhgrlhhsse hlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 43A811820074; Sat, 26 Jul 2025 07:42:06 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: T52980399d72dd973 Date: Sat, 26 Jul 2025 13:41:45 +0200 To: "Juris Evertovskis" , "Daniel Scherzer" , "'php internals'" Message-ID: <3224caf5-51b3-4b3f-9a55-8130eca16c9a@app.fastmail.com> In-Reply-To: <010c01dbfe20$8ddad8e0$a9908aa0$@glaive.pro> References: <010c01dbfe20$8ddad8e0$a9908aa0$@glaive.pro> Subject: Re: [PHP-DEV] Validating #[\Attribute] targets Content-Type: multipart/alternative; boundary=65fd87fd0829400d96bec4cb1cf3ba21 From: rob@bottled.codes ("Rob Landers") --65fd87fd0829400d96bec4cb1cf3ba21 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Sat, Jul 26, 2025, at 13:29, Juris Evertovskis wrote: > Hey, > =20 > > if you are not going to call ReflectionAttribute::newInstance(), the= n you don't > > really need to mark the trait/interface/enum/abstract class as an at= tribute. > =20 > Personally I never knew the `#[\Attribute]` is only required to do `->= newInstance()`. > =20 > The docs make it seem like it=E2=80=99s a boilerplate to have an attri= bute at all: https://www.php.net/manual/en/language.attributes.overview.= php > And the documented example does not call `newInstance` although the at= tribute is defined with `#[Attribute]`. > =20 > I=E2=80=99m also confused as the example created by Ilija https://3v4l= .org/8Lih3#v8.4.10 that would supposedly break does not include `#[Attri= bute]` at all. But it includes an enum being used as attribute without i= t using the label itself. So what would actually break? Using enums as a= ttributes or declaring them as such? > =20 > Btw while reviewing the docs https://www.php.net/manual/en/language.at= tributes.classes.php > I noticed that > =20 > > It is recommended to define a separate class for each attribute. > =20 > Recommended as opposed to what other options? I don=E2=80=99t think I = can use a random string as an attribute without defining it, right? So w= hat is this recommendation steering me away from? > * * > BR, > Juris Hi Juris, You can use any identifier you=E2=80=99d like as an attribute, so long a= s you never try to get an instance of them. Since you are already reflec= ting on them, instead of getting an instance, you can just get the argum= ents and do whatever you want. We actually ended up doing this on an int= ernal project at my last job. It was pretty powerful. =E2=80=94 Rob --65fd87fd0829400d96bec4cb1cf3ba21 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
<= br>

On Sat, Jul 26, 2025, at 13:29, Juris Evert= ovskis wrote:

Hey,

 = ;

> if you are not going to call ReflectionAttribute::newI= nstance(), then you don't
> really need to mark the trait/interfac= e/enum/abstract class as an attribute.

 

Pers= onally I never knew the `#[\Attribute]` is only required to do `->new= Instance()`.

 

The docs make it seem like it=E2= =80=99s a boilerplate to have an attribute at all: https://www.php.ne= t/manual/en/language.attributes.overview.php

And the docu= mented example does not call `newInstance` although the attribute is def= ined with `#[Attribute]`.

=  

I=E2=80= =99m also confused as the example created by Ilija https://3v4l.org/8Lih3#v8.4.10 that would supp= osedly break does not include `#[Attribute]` at all. But it includes an = enum being used as attribute without it using the label itself. So what = would actually break? Using enums as attributes or declaring them as suc= h?

 

Btw while reviewing the docs https://w= ww.php.net/manual/en/language.attributes.classes.php

I no= ticed that

 

> It is recommended to define= a separate class for each attribute.

 

Recom= mended as opposed to what other options? I don=E2=80=99t think I can use= a random string as an attribute without defining it, right? So what is = this recommendation steering me away from?

 

BR,

Juris


Hi Juris,

You can use any ident= ifier you=E2=80=99d like as an attribute, so long as you never try to ge= t an instance of them. Since you are already reflecting on them, instead= of getting an instance, you can just get the arguments and do whatever = you want. We actually ended up doing this on an internal project at my l= ast job. It was pretty powerful.

=E2=80=94 Rob
--65fd87fd0829400d96bec4cb1cf3ba21--