Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117408 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 60819 invoked from network); 23 Mar 2022 21:29:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Mar 2022 21:29:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5A0751804AF for ; Wed, 23 Mar 2022 15:55:51 -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,HTML_MESSAGE, 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-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 23 Mar 2022 15:55:50 -0700 (PDT) Received: by mail-ej1-f53.google.com with SMTP id j15so5748539eje.9 for ; Wed, 23 Mar 2022 15:55:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wntsUzT7k/c0ykYcvpmistJVHNqpBVf0CG2HVmqXRsY=; b=HlqvXc9Qe7URHKyRv2FLqSp/JjXqJ43shDq32RfDxtC/pzhXA43tQB/Sot0ZPSgiwH Gylcu6+Kydwt78yT/IjIRIju9m9FtcyuBRnupRBlqSXQS5TKneJR05g5/qZNYaT5OTh6 1OpVrQAjnmATtJGBZFYrPUTweoSSaS4Nz4QnjDbHbQVMX8BIgSwjg0t7uWwzPBnmgO8V vencqXPj0ZKJLyGEvtlFof+hW63mFjUxZR0axpjPkJHMQj1jDX0n+Ha26VYX11LrfKt5 cxV03JVeAzgtKNUzeyYJrXc9ysMukmMrzGlQNFN363L3jKQkSoLFN6/bzwGKoTgQyaIc k+1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wntsUzT7k/c0ykYcvpmistJVHNqpBVf0CG2HVmqXRsY=; b=1TmfPCWoSF9iKndMY/r55D3/Fqj2rqR2cS+RWzUpVvkK4lcyNDbVg5ZBFyF30xru+w dv/JgC3WFbXDmZmgfm113O+9Z8J2AKTrIAZKef3OAoXj45oHMrVJBEMflWrNAYYsmjRK UevfnCEzbXLikdbJqA3x5GGd0rTJpThueQuJ6CVR3UDFR04fmiEcrhIQa/LY98QbQg4M OB+YNeWi0xWEZAM88utFJvRfCmyWdFU0HR6AO36+Vwd9v4QVpXwJeeZ5ZFZx2AhCnQhp KufNXv9JzlgaJmhThV+i3wASanzx9Z1obQJpxLQweujZ/aqJ8veRpKJQdV+yGNOWRN++ NbRg== X-Gm-Message-State: AOAM533IMjY0yKfw6JkrxQaGpiiXMBwlsqVT51XBuLlvOU9stCVxSyf+ DdBg4tKITGiDnDyodVrNn/A+rwpAoqn7+X9RraBF5RPFroU= X-Google-Smtp-Source: ABdhPJwZFKuDNL+8WwI+rYdwQzIhq7htltci2AoNMhVFUijWsRLdcd072ZI8Vss0DUqPSMFt5v1I3nVY6KHjk72IE0M= X-Received: by 2002:a17:907:7ea7:b0:6df:fb36:e8af with SMTP id qb39-20020a1709077ea700b006dffb36e8afmr2696309ejc.356.1648076148924; Wed, 23 Mar 2022 15:55:48 -0700 (PDT) MIME-Version: 1.0 References: <76c399cb-fb29-4583-a212-8eb69740c96b@www.fastmail.com> In-Reply-To: <76c399cb-fb29-4583-a212-8eb69740c96b@www.fastmail.com> Date: Wed, 23 Mar 2022 16:55:37 -0600 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="0000000000006b661b05daeaa1e5" Subject: Re: [PHP-DEV] Typed constants revisited From: mbniebergall@gmail.com (Mark Niebergall) --0000000000006b661b05daeaa1e5 Content-Type: text/plain; charset="UTF-8" On Wed, Mar 23, 2022 at 4:10 PM Larry Garfield wrote: > On Wed, Mar 23, 2022, at 1:54 PM, Mark Niebergall wrote: > > Hello All, > > > > In June 2020, Benas Seliuginas emailed this list ( > > https://externals.io/message/110755#110755) to gauge interest in typed > > constants. An RFC (https://wiki.php.net/rfc/typed_class_constants) was > > started and had an accompanying PR ( > https://github.com/php/php-src/pull/5815). > > At the time, Nikita Popov supported it for overall language consistency, > > and Sebastian Bergmann +1'd it for overall language consistency as well. > > The PR has since been closed without being merged, and the idea hasn't > > progressed since. > > > > I'd like to revisit the concept, gauge current interest, and pick the > > concept and work back up. Any more discussion items for or against the > idea? > > > > I'm willing to do the work to pick it up where it was left off and get an > > RFC proposed through the normal process, and a working PR. This is my > first > > foray into the PHP RFC process, and C code is not my forte, but I'll take > > it on. If anyone would be willing to pair/mentor/co-author with me, > please > > let me know. Either way, I'll plan to work on it. > > > > This is the first of several concepts I have been tinkering with and > seeing > > practical uses for. The next phase of work with different RFC would take > > this further with const inheritance, where const type must match. > > > > - Mark > > Is there a benefit to it other than "well everything else has types now, > so..."? Even if it's esoteric, like in reflection-based meta programming? > (I've been dabbling too much in that lately. :-) ) "Everything else does" > isn't a compelling argument for me, but there may be others I'm not > thinking of. > > --Larry Garfield > Larry and Internals, To the benefits! On its own, typing class constants does help align them with other things like typed class properties, typed arguments, and method return types. There will come additional benefits though, and this would be phase 1. Phase 2 would be to introduce typed constants to be set by extending or implementing classes. Combining the two features would lead to better defined and usable class constants. For example, an interface could be: ``` interface DbTable { public const string TABLE_NAME; } ``` Which would then be implemented as: ``` class UserTable implements DbTable { public const string TABLE_NAME = 'user'; } ``` Thus ensuring `TABLE_NAME` is always set, and always returns a `string` type. See https://docs.laminas.dev/laminas-db/table-gateway/ for a real-world framework example where this feature _could_ be used. Another example I often see in my projects could be used with a similar example: ``` abstract class Bird { public const bool CAN_FLY; public const string FAMILY; public function canFly(): bool { return self::CAN_FLY; } } final class EmperorPenguin extends Bird { public const bool CAN_FLY = false; public const string FAMILY = 'penguin'; } ``` In this case, one could instead use typed class properties, but it feels improper since the values are static and unchanging. Immutability is a work around to this when implemented properly. This would simplify the solution while ensuring values remain unchanged. - Mark > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --0000000000006b661b05daeaa1e5--