Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120262 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 75426 invoked from network); 13 May 2023 15:40:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 May 2023 15:40:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CF6C31804C6 for ; Sat, 13 May 2023 08:40:43 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,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-oa1-f51.google.com (mail-oa1-f51.google.com [209.85.160.51]) (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 ; Sat, 13 May 2023 08:40:43 -0700 (PDT) Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-1960539df06so6204264fac.1 for ; Sat, 13 May 2023 08:40:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683992442; x=1686584442; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=J9hAUEj6El9d4gaQYwyrKwQJS+CotAIGpU0sMWv0nv0=; b=suHmt6bJcbkKeBBblHbvDcr929348sMMKixN8QEWXrDMtNImwUZdHq15TOCs/bsHsO +QNd0IEsUBQHcruHXxqgQk2DcxA/vDhxKHgoY9x+R1+KeviSBwOxA0AXQtf8wOCYw+2T np5BDclSOlxTUZhkOwhvLXGGktLszQYE+ZaXDGns97wcEf6gUF/lt2ytoc9cleKlUjLO 8aUFlQN8ZcfssmsveY3zt4nR0yPddNabRba0cYFhBgdGguBJavwzJM95lN4WaXmHb39m A4NLu4jYG+51EaTyf//2bIUVwI9X8nloBwOIvdCtQPy+8ty9Y2C2r9CAtAY35MAEYfcq Um8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683992442; x=1686584442; h=content-transfer-encoding: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=J9hAUEj6El9d4gaQYwyrKwQJS+CotAIGpU0sMWv0nv0=; b=kMf9+wAXoEOs2NHCT2nU6gCUKFsP4erS2f7nScSc53wlGalWs6oNougunhwe0RVcNi FKA91gnTL6KlL0K6DBvjBkEgQv5h7lhHjoimy1LCQ3ZKWUoRvcVI/vorJqkhr+lu8xqB bCZga/oh1p779fdez5gVN4AQTPFb7/erG8wPGwDtEArgqgma1ravhgzIJDGXWxQX0f8i aaoiSJXpIWSx4BPpoiHx2V5sEx6L66EOA3c84w3nHlVkBaypnYFqFlKZX/c85v9fayPj GmGtJXWJ2Ana5L+xNAgEyTelSBqxtUYhrMV70G+ZBkr8GmbqStLSj8VU9AUmWrA3R33r 9lwg== X-Gm-Message-State: AC+VfDw3y1f4YpaDkrETQIXlt+hwQ2NCxBWT0s68qIWpg4kqCAdswSpN 7TpeCvc1v26xS1d+Hs9uvb4oFXFJDXP5lQ80O6Q= X-Google-Smtp-Source: ACHHUZ6xdsAEOmuZU9uLGs/oBJuT8927RgnnncJilJ8JAr/Ikg/CjRZFQZLeBaLJB0jyXO4nE6gP3x01cQDW/kxPba4= X-Received: by 2002:aca:db45:0:b0:395:eb6c:6351 with SMTP id s66-20020acadb45000000b00395eb6c6351mr587964oig.55.1683992442019; Sat, 13 May 2023 08:40:42 -0700 (PDT) MIME-Version: 1.0 References: <436378BB-FDFA-43BD-A633-C030C347E683@gmail.com> In-Reply-To: <436378BB-FDFA-43BD-A633-C030C347E683@gmail.com> Date: Sat, 13 May 2023 17:40:27 +0200 Message-ID: To: Rowan Tommins Cc: internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] [Discussion] nameof From: landers.robert@gmail.com (Robert Landers) > The one part of the RFC that surprised me was this: > > > When getting the name of constants and functions, the name will NOT be = the full name, but the lexical name. This means that if the name is use'd, = it will be that name. However, if the full name is used in the nameof(), th= e full name is returned. > > No reason is given *why* it works this way, and expanding the namespaces = on a name is one of the most useful features of "::class". Importantly, it = means that the string created can be passed to other contexts, and still re= fer to the same thing. For instance: I'll update the RFC for the reasoning later tonight, but it's actually a pretty simple one. There are other languages that have something similar (C# and Scala, from personal experience) and usually, they provide an unqualified name. This can be annoying, for exactly the reason you describe: when you want to pass the value to another context. Most of the time, you want the unqualified name, but then there are times you want the qualified name, and getting it can be a complex process. By allowing developers to provide a fully qualified name, if that is what they want, developers have more control over their experience. This is especially important when using traits and aliases: trait A { public function example() {} } trait B { public function example() {} } class C { use A, B { A::example insteadof B; B::example as exampleB; } } $a =3D new C(); echo nameof($a->exampleB(...)); // outputs: "exampleB" The actual 'qualified name' is "example" or maybe "B::example" but that isn't very helpful if you want to call $a->B::example instead of $a->A::example later on. Or consider if you have an aliased const: namespace A { const A =3D 1; } namespace B { use const A\A as TEST; echo nameof(TEST); } In this case, if you want to use nameof(TEST) in an error message or something else, it would be surprising to get \A\A instead of TEST. So, for your example, we can instead call nameof(\Acme\bar(...)) instead of the aliased name when passing to another context: use function Acme\bar as foo; ... #[SomeAttribute(callback: nameof(\Acme\bar(...))] ... I hope this helps! > Similarly, I'm not wholly clear why nameof(MyClass::method(...)) should e= valuate to "method" rather than "MyClass::method" or indeed "Full\Namespace= Of\MyClass::method". Usually, you want the name of the 'last' thing". For example, if you want the name of a property called "myProperty", using nameof($this->myProperty), you probably just want "myProperty" and not "{some_internal_instance_id}->myProperty" that you have to parse to really use. Instead, we can compse ::class and/or nameof() to achieve the same thing if we want to.