Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120443 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 86523 invoked from network); 30 May 2023 00:48:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 May 2023 00:48:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3040E1804DF for ; Mon, 29 May 2023 17:48:41 -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,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-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) (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 ; Mon, 29 May 2023 17:48:40 -0700 (PDT) Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-565bd368e19so43813627b3.1 for ; Mon, 29 May 2023 17:48:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20221208.gappssmtp.com; s=20221208; t=1685407720; x=1687999720; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=EzprHnWLBs4yANw4e1aTAua6Dki7kzyqb7vlxj7674k=; b=BUJsYUiVIQyyvNXznrQGBtXzwTC/zOZCgMKue76nLtInOxcs85ZdmkFcZYPkU2DI3f uUaOnOS6KtD1sH3YIPC7G8+KpbVBJ1n/0VotUe83708PuboHxcNjp0pxOqhm3qgvBZeO pNlql8CNjTRRcWJiMb2ku05LubW3rT4tqTkqcOkxOrCtHBNbvnM8re/6sJ0L3+2/Ytlu ExQ5fl1E+IGO7IFI7ybXHvZus1iD/xDLZRH/0f824/OCLJHJARh7fntQBZ1uvUiS11ZP QREPUztSeG4QdYj6zhlGav4Ay4dPbUq5B6FMssYCBn73QvYMUd6Ue8JgZRvW6hWusjCB W5jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685407720; x=1687999720; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=EzprHnWLBs4yANw4e1aTAua6Dki7kzyqb7vlxj7674k=; b=gmYY/Urd2hssaa0BCFP+lFpv/23qlB2Ky+vPCsaXY9zEOVVJlLHn0/lGKO5eq/Qfyd 2eoxvA5fAIOWC7UP3NoUyuzQaDnLc6AV7Ivf22qiZxl1hWhkc+x6/6Py6gAtUo/duieP nzYw6AH7q7hA9lGrmGc2FMINWsjlozsDqV6BcsZPqsgPZVWZPKs4nVHBn9mbnJOkj4sR VMvUPo7p7/lgpE1ZH7I4PkAHJJcLpAn3fdiMFBuSX6uuj6pDgwwyWpdIlr4JGVv1FGgo JqBXDnAL9oBN3i5rlRmKf8Tx0VAAZz+uMaRjQp3kc9MT2sZeyQYc/FMoIrfYN8hUU0u3 oXDA== X-Gm-Message-State: AC+VfDyIdQoaRgwpBdUz/0hWaPNAQp6bVwyXJHe2ZOoCscn9ZoEnbVdo 53bB4NTGYVAueoN/d3W173hLWaRZUiAx6+JexxVpAlG1FcYJfDMO2nY= X-Google-Smtp-Source: ACHHUZ4z91Q0iFdogY/pfdPh/gQu64VebVeThe2Wz3umiNnVHkTH1dDMj2tiaa71U9lC0qwYG6p7X1lBUh6o3K1ZF4M= X-Received: by 2002:a0d:eac4:0:b0:559:d3a0:4270 with SMTP id t187-20020a0deac4000000b00559d3a04270mr559249ywe.34.1685407719499; Mon, 29 May 2023 17:48:39 -0700 (PDT) MIME-Version: 1.0 Date: Tue, 30 May 2023 02:48:28 +0200 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Declaration-aware attributes From: andreas@dqxtech.net (Andreas Hennings) Hello internals, I am picking up an idea that was mentioned by Benjamin Eberlei in the past. 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 = (new ReflectionClass(C::class))->getAttributes()[0]->newInstance(); assert($a instanceof A); assert($a->x === 'x'); assert($a->class->getName() === 'C'); assert($a->y === '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 = (new ReflectionMethod(CC::class, 'f))->getAttributes()[0]->newInstance(); assert($a->class->getName() === 'CC'); assert($a->method->getName() === '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. ---- Thoughts? -- Andreas