Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115619 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99657 invoked from network); 1 Aug 2021 13:40:25 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Aug 2021 13:40:25 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 72A621804B3 for ; Sun, 1 Aug 2021 07:08:39 -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=-0.8 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (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 ; Sun, 1 Aug 2021 07:08:38 -0700 (PDT) Received: by mail-wm1-f50.google.com with SMTP id a192-20020a1c7fc90000b0290253b32e8796so9744971wmd.0 for ; Sun, 01 Aug 2021 07:08:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=nFUXPhsLMAI4iTJqOApiMh6fa9uagip/lrBax9GQeTk=; b=e1Gc0wdgL2nmcr+Naf0w/XEGl+8Z3RZuREdagm30rZCzpGXD6HeQs/j9uChH6kz8ns qxmToqJvX1E43zKqfKPyd5gialSB+y55wIGEGprokm3LFelFqXhs9G7PvWtt26NHaJB3 9hx1qgE/iSRLp6pKCSDiNhvNpibs37ulrbagN16qNFLv5brwRmfat1jKDXPvm/lGJ+kE f7WxqAB7i/Md5aWmbSezrKlctZ0Ue0BF8+3GngRQ+LXJqk1voB+rkjBcqXKdwUYOp2j+ cZi0giqQ6vJjYwnPTzI47fxTfXgohuDvjCVa87G1qg8uk4+G97aejtrIQ/wyvGXk2v5i lHPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=nFUXPhsLMAI4iTJqOApiMh6fa9uagip/lrBax9GQeTk=; b=j1U54fyDKCeHnt4JQodSyGhJyPWrRHRdO6k80BCtjikh+VzD81wi/52nCC1Yrx0Za7 e9aeP/BwGga3wVgCVMTw6ikwdiEy+Q695tLlQTgSRq1aMEG2yhWNJ+ijSFYThiL/7lQ+ +4atGOoui6zwetW2dz5eU4Zj5tmzwL/plfrq1UCpufeKcu/xi9uSj4iMq9XtgD3f54xa 8vrOCtjEboBlZaZVRgJ9aTnWEGs7AnBbxVOwQXGTfT0EkdNolLD+8XOHFQIOCj/x+/y3 5kqD6Z5LcuJjKasYCmQbJXYFYe9vgY0JGqH2oN0kUEBHnymW9hizWSNeFRkUKfFreRIL 4Sgg== X-Gm-Message-State: AOAM532rFNy3VSAoroqDVrAkaJ/o3VfLW88cLt9r6otSP/0dQ2MUeCV7 AGlz/E9pCUG8WwP0dGsSB/aPnxx4xL4= X-Google-Smtp-Source: ABdhPJyqd/Uw1VoJJdqt3JzvGC3cYTOKWzN77tWAlrnMhQDTrgax1mdho2KDWR6+988wSeqSLIox9A== X-Received: by 2002:a7b:cbcd:: with SMTP id n13mr11154227wmi.105.1627826915281; Sun, 01 Aug 2021 07:08:35 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id z137sm7483573wmc.14.2021.08.01.07.08.34 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 01 Aug 2021 07:08:34 -0700 (PDT) To: internals@lists.php.net References: <22EF92EC-C5A3-498E-9EFD-DE2B5B204BB7@gmail.com> Message-ID: <6e668a49-d69c-7b6d-f9c4-1d3fe2058d27@gmail.com> Date: Sun, 1 Aug 2021 15:08:29 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: <22EF92EC-C5A3-498E-9EFD-DE2B5B204BB7@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-GB Subject: Re: [PHP-DEV] RFC: autoconst From: rowan.collins@gmail.com (Rowan Tommins) On 01/08/2021 12:21, Serhii Smirnov wrote: > instead of defining constants like: > const FOO = 'FOO'; > > they could be defined like: > autoconst FOO; // defines a constant FOO with the value 'FOO' While I've certainly seen constants like this in the past, I think they mostly fall into two categories: A) Constants where the value doesn't actually matter, and you just need something to compare input against. As of PHP 8.1, we will have enums, where you can just write "case FOO;" and not assign any value at all, which can probably be used in most of these situations. B) Constants where the value matters, but the constant name isn't actually conveying anything. Given your suggestion of built-in transforms for lower-case etc, these seem like what you're interested in. These often arise from a misunderstanding of the dogma to "avoid magic numbers and hard-coded values", and result in code that looks like it's SHOUTING without conveying any extra information. For instance, people start with this: echo "Hello, " . $name ?? "World"; And think that they can "avoid hard-coding" like this: private const HELLO='Hello, '; private const WORLD='World'; // ... echo self::HELLO . $name ?? self::WORLD; But these constants aren't actually doing anything useful - the code is harder to read not easier, and it would be very odd to later change the value of "const WORLD" without also changing its name. In order to actually be useful, the name of the constant needs to convey something other than its value, like this: private const MESSAGE_GREETING='Hello, '; private const MESSAGE_DEFAULT_NAME='World'; // ... echo self::MESSAGE_GREETING . $name ?? self::MESSAGE_DEFAULT_NAME; I think if you eliminate constants which can now be enums, and fix constants which are badly named, there won't be enough uses left for this new feature to justify its inclusion in the language. Regards, -- Rowan Tommins [IMSoP]