Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120396 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 88704 invoked from network); 25 May 2023 15:21:18 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 May 2023 15:21:18 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9D6F4180384 for ; Thu, 25 May 2023 08:21:16 -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.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,MISSING_HEADERS, 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-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) (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 ; Thu, 25 May 2023 08:21:16 -0700 (PDT) Received: by mail-ot1-f53.google.com with SMTP id 46e09a7af769-6af6f4a0e11so1164348a34.0 for ; Thu, 25 May 2023 08:21:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685028075; x=1687620075; h=content-transfer-encoding:cc:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=abQ2u3loy9jqt0KgjqcA17xMmeIDtkx3XvDIYR8MuBM=; b=ZvYOC7CZG2sRMLwMZ/jk1zcg0rkZhXd0vHSOkCCO8VtNWtpSfANwAij/RCb3ndKLwF zmDBGHpcCZqyTWRuZlLBE9yj3/VnQLFRxgGf/3OAeD+FBdW8neBnC9UdE5O87BlAV1Dg gqU4tff7CcE1BoyThtrOtoeGR84H6XFpm0G6hVH8136Ezer905JpGKosldUuys5VYYNa v89uyja4SW2Uxuw9GxlO1owz+tiM8oLQu1ap3lAXiCdEHnqZ/mjfL8cCKtgxl03QNu66 akz8Rl4zG4SsD1Ms/ag5kKomHnjd61DOnO06lCxAaCA4QmJ14cKtoxS8bJXg6J7EGC6R vTiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685028075; x=1687620075; h=content-transfer-encoding:cc: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=abQ2u3loy9jqt0KgjqcA17xMmeIDtkx3XvDIYR8MuBM=; b=cKYG/jdQs1kjLz1Sd3Xi425ihIofYsKibb7rj/7AM7u9BovcuRgPmW/tYzSzzOuByh 7eFj/288G4Pe4LGgomei2jUQUkuTq0qg+qneBybz2lBI2qtkTaraZsRH7JJUnZY9dOfu KqHQzk30f4JE6gN/DamyeOZiXN7kMMBk3o4XuxBxXx3ocMkrmcuLOpWmJrLrVruK2EFa tWGeyZgM4fkLDSFsJYnBmTXYrRMBncZpssw27fTajVN4gWNCIGW528SCzeSlPdHZmAZj yuVx7Jx3AX2VxO4rBGp2KYMJTiPiMvxPHnQX3oDv3DjUV0y4eWqXZNAAW8S0sxpCvYGy 2pGg== X-Gm-Message-State: AC+VfDyeCNtvdWCr0X2tq15Dyo2LVFoYUAGfsQXI4ZCcIDnYpSAuAmAj nmRVKxdYmYu0awnN4ZvPmZf/To7SIC8YW6KKf+lRaTTFyltjWA== X-Google-Smtp-Source: ACHHUZ49I96betpo6qNiXKTOxkl+OmGJVHGmwlGhjcMwpDstKdsrBp+46V+6u+BprKb13Lz8G40vTNw9O0Cz/HLuZg0= X-Received: by 2002:a05:6830:12d0:b0:6aa:d278:24e5 with SMTP id a16-20020a05683012d000b006aad27824e5mr11865898otq.14.1685028075284; Thu, 25 May 2023 08:21:15 -0700 (PDT) MIME-Version: 1.0 References: <5e35344e-e885-4b65-8f86-2cb43330af46@app.fastmail.com> In-Reply-To: <5e35344e-e885-4b65-8f86-2cb43330af46@app.fastmail.com> Date: Thu, 25 May 2023 17:21:03 +0200 Message-ID: Cc: php 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 Tue, May 16, 2023 at 11:56=E2=80=AFPM Larry Garfield wrote: > > On Sat, May 13, 2023, at 7:27 AM, Robert Landers wrote: > > 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 > > Some concrete use cases that I know I run into, and would thus be what I'= d hope an RFC like this would resolve: > > Router::addRoute('\my\space\my_action_function`); > > Right now that has to be a string with a full namespace. You cannot use = a FCC here, because you want to use this data to compile the router somehow= . So in this case we want the full namespace. (Ignore function autoloadin= g for now.) > > Router::addRoute(MyClass::actionMethod); > > Various frameworks have different custom syntaxes for this case. It's al= so not clear if this refers to a static method, or a "instantiate this out = of the container first and then call this method" approach. In these cases= , we would want the full class name, and the method name on its own, as sep= arate strings. > > In my FP library, I have code like this: > > function prop(string $prop): \Closure > { > return static fn (object $o): mixed =3D> $o->$prop; > } > > function method(string $method, ...$args): \Closure > { > return static fn (object $o): mixed =3D> $o->$method(...$args); > } > > Which you can then use in a pipe, like so: > > pipe($someObject, method('foo'), prop('bar')); > > Or, more realistically, you'd use method() and prop() in a map or filter = call within the pipe. > > In this case, you want just the method/property name on its own, without = a namespace, because it will be used in the context of an object to be name= d later. > > How would nameof() handle each of these cases? > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > Hey Larry, I did not ignore you. I somehow missed your question while researching something after reading the first half of your email last week -- I, apparently, never made it to the second half until today. I'll get to that in a sec. > pipe($someObject, method('foo'), prop('bar')); > > Or, more realistically, you'd use method() and prop() in a map or filter = call within the pipe. > > In this case, you want just the method/property name on its own, without = a namespace, because it will be used in the context of an object to be name= d later. So, for example, you could replace your pipe with: pipe($someObject, method(nameof($someObject->foo(...))), prop(nameof($someObject->bar))) It looks a bit wordy, but PHP differentiates between properties and methods by using a parenthesis/bracket (hence if you want to call a property that is actually a closure, you have to write ($this->prop)() otherwise it will look for a method called prop and fail to find it). At runtime, you would receive a warning on this line if $someObject is missing a method called foo or a property called bar (if the error-detecting version is chosen). This would result likely result in an actual error in your method() and prop() functions. However, it should make debugging from logs a bit easier since you'll (hopefully) notice the warning. Now, why I didn't answer you and only made it half way through your email..= . Much of the earlier discussion was about functions/consts being fully-qualified names. I was mostly against it, because I was offering 'both' qualified and unqualified. However, your initial examples really hit home for me and I did a bit of spelunking in various popular PHP codebases for class::name (since that is really the only similar thing) and for stringified names of things. I'm now convinced that FQL's should be returned for functions, and constants (but not methods and properties). Thus, your example: Router::addRoute('\my\space\my_action_function`); Router::addRoute(MyClass::actionMethod); could be rewritten as use function \my\space\my_action_function; Router::addRoute(nameof(my_action_function)); Router::addRoute(nameof(MyClass::actionMethod(...))); I'll have to think about how to word this in RFC, but I'll do so as soon as I figure it out. Cheers, Robert Landers