Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120266 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 7316 invoked from network); 13 May 2023 22:42:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 May 2023 22:42:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 91E3F1804D0 for ; Sat, 13 May 2023 15:42:55 -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-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.52]) (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 15:42:54 -0700 (PDT) Received: by mail-ot1-f52.google.com with SMTP id 46e09a7af769-6ab1942ea59so4006043a34.0 for ; Sat, 13 May 2023 15:42:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684017774; x=1686609774; 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=2qCTu5p5zvxL4aPmv43c0JICOO2i/JUHLOOvgFhgBwM=; b=bO6nXHXVnNAJRvoIdrxRaWQNRIAYP8llHcwPvUoeakpczV6vU4/0updSV1fkY17jaK j/6OGfyY0pBobbwkCWXTa1Geg2Zyh7suji4miRRN/Ygp4X8t/PdM3W6R60fE8+FIoSiW CHxeIR9A8/WO0vv9bEyVi5Hd5NytTBOHS808kYqViuANt3tSsuMLovclZehMcIoTghCL sehTVF4prlxTQbD0eoOKPgDVlD3VpuXcvzUKSIwNnMZkZsQR2H2oKMw5E96A0m4tZnwh ON4c71TBX6sUhSvedPaTIy8qKqjOx4/HKUSTFLVe6WUZcTVC6mHJINaIF+zzMC1RunpJ J0iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684017774; x=1686609774; 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=2qCTu5p5zvxL4aPmv43c0JICOO2i/JUHLOOvgFhgBwM=; b=lRePEXO+37IL5Q4FIsuaIpWSGcs5Sj1VEC+5nvGkdwOxjSoASNug6uOfPftZe9oDi1 ni5gPp/nPW2lMMzbt1PtCAVTscwk+uETvuDMdF7BBgIY+fuNcYZuVT3ZNas4zIlNT39a QDDyhZsWqio4gxujE3HaDano5hM1iW7A6OGZVy26G9Ustqo3yTFsMAmMRHsmr+Sep5Io /N+HGfdijAYODjkyRSzPNupEnk0QsylWfLBCv2+Qx7qL8rQtaQFHp0x1ZceE87kPglY6 mthtZFHwOSoLr2gVJjS1AOnPB6kWpHEvu5N7l39XZXOdJr7N4dt5Yv50MtEzylwrV6kI /MtQ== X-Gm-Message-State: AC+VfDxVAEvc9g6Z08A2ZZOQT7lRtU4MRw1g6/clZuOADrwBDhwhOWHF sXYlRks+Lb7wAWB/bmXZMBeXL6h3wsw0IlkeD0g= X-Google-Smtp-Source: ACHHUZ766BXTynBg4pc+bIeKRl7Dzrt+FtH1A79C+1hQhhsq35fAy8GCXb5tlyCiBeFfO5XyP6swKlgqUzpQYHsuGe8= X-Received: by 2002:aca:a888:0:b0:395:f73c:ce64 with SMTP id r130-20020acaa888000000b00395f73cce64mr290692oie.3.1684017773834; Sat, 13 May 2023 15:42:53 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 14 May 2023 00:42:41 +0200 Message-ID: To: Lydia de Jongh 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) On Sat, May 13, 2023 at 11:05=E2=80=AFAM Lydia de Jongh wrote: > > Wow! Yess please. My mail yesterday crossed yours, I guess (thanks Claude > for answering) > asking for almost the same: > > myClass::$normalProperty::name > > So I would like to add my thoughts to your proposal. > > I prefer a magic constant like `::name` instead of a function call as it > can be used in a wider scope; for example as a parameter in an Attribute. > > My arguments from yesterday: > > > 'Directly' accessing properties and methods of the class as a parameter > > in an Attribute is not really possible. > > You can do that for a class with: \path\to\MyClass::class, but for > > properties or methods you have to use strings, without recognition in > > editors. > > > > It would be so nice if more items in php could be accessed with a > > magic constant as you can do with ::class. > > With magic constant an editor could recognize it and it limits typos > > etc. and it provides the developer with a list of the properties/method= s in > > scope. > > > > > Some examples from my mail yesterday for using in attributes: > > abstract class SaveFactoryAbstract { > > use ObjectBuilderTrait; > > > #[ResetValue] > > protected bool $isSaved =3D false; > > > #[InitObject] // tells the builder to initialize this property with the > latest specs provided by (other) attributes > > #[ResetObject] > > protected ?SaveInterface $oSave =3D null; > > > > public function __construct(){ > > $this->buildMe(); // provided by the trait > > } > > } > > > With a magic constant you could do: > > #[UseClass(static::$oSave::name, ImageSave::class)] // using static for > normal property? > > #[ResetValue($this->isSaved::name, default: true)] // or using $this ?? > class ImageSaveFactory extends SaveFactoryAbstract { > > // negative side-effect: empty classes..... > > } > > > From my mail yesterday: > This could also extend possibilities with/for variable variables.... > > Like: > $this->myProp::name > myClass::$staticProp::name > > A potential difficulty is: how to access normal properties/methods.... > Maybe just as if they are static? > Like parent::normalMethod() is working.... > A normal property or method cannot have the same name as its static partn= er > (sadly) so internally I hope it is not a problem. > > outside scope: > myClass::$staticProperty::name > myClass::$normalProperty::name > myClass::normalMethod::name > > inside scope: > static::$normalProperty::name > self::$normalProperty::name // I do not know if 'self::' adds anything fo= r > this purpose, except for private properties/methods maybe > > inside class/object: > $this->normalProperty::name > $this->normalMethod::name > $this::$staticProperty::name > > > Greetz, Lydia > P.S. I hope the spacing in de code-example is working now. > > > Op za 13 mei 2023 om 09:27 schreef Robert Landers >: > > > > Hello Internals, > > > > It is with much trepidation and excitement that I'd like to announce > > the `nameof` RFC (https://wiki.php.net/rfc/nameof). It has changed > > quite a bit in the last couple of days, so if you haven't seen the > > latest draft, please check it out. > > > > Essentially, it allows using `nameof()` anywhere a string can be used, > > even in static contexts. From a developer's perspective, it is a > > string and from the engine's perspective, it is also mostly a string > > (depending on how deep we want to go on error checking -- see the > > RFC). > > > > If anything is unclear or if I missed something, please let me know. > > > > Robert Landers > > Software Engineer > > Utrecht NL > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: https://www.php.net/unsub.php > > Hi Lydia, Please remember to bottom-post responses. But, to respond: I think the downside of using ::name would be: 'what if I already have a const called "name"?' So, we'd have to come up with a different name. Largely, as far as the compiler would be concerned, we've both got the same idea. > A potential difficulty is: how to access normal properties/methods.... > Maybe just as if they are static? > Like parent::normalMethod() is working.... > A normal property or method cannot have the same name as its static partn= er > (sadly) so internally I hope it is not a problem. I find this particular point an interesting side discussion, as it was something that bothered me for a while. Ultimately, I ended up with two solutions: 1. Do no error checking at all with nameof. This lets the community come up with something that makes sense. Perhaps someday in the future the community will settle on something that works well and an RFC will be put forward to add proper error checking. For example, any of these could work, and all resolve to the string "normalMethod": - nameof(ClassName::normalMethod) - nameof(ClassName::normalMethod(...)) - nameof($_->normalMethod) - nameof(ClassName->normalMethod(...)) <-- I kinda like this one - nameof(normalMethod) 2. Emit a warning This is the one I suspect will be chosen (and I'm starting to like more and more, after writing out those examples). In this implementation, we'd emit a warning if you used a name that doesn't exist. However, PHP has a well-known silence operator (@) that could be used to silence the warning if you wanted to do something weird (like one of the examples above). It would still allow an RFC for referencing instanced properties/methods at a later date, if there is demand for it. Until then, there is the silence operator. If there was an RFC for referencing instanced properties or methods, it could be interesting. Right now, the only way to reference instanced properties is via reflection, and having this capability would open the door for having a generic 'declared_type()' (not to be confused with gettype() that returns the actual type of a variable and not the declared type) that could instantly return the reflected type. I can think of a number of applications for something like that... and all the code I could delete... It would be magical to write something like: var_dump(declared_type(ClassName->property)); instead of $class =3D new ReflectionClass(ClassName::class); $property =3D $class->getProperty('property'); $type =3D $property->getType(); var_dump($type); Something to think about...