Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115623 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 47073 invoked from network); 2 Aug 2021 07:08:29 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Aug 2021 07:08:29 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4E8FE1804B3 for ; Mon, 2 Aug 2021 00:36: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=-1.5 required=5.0 tests=BAYES_00,KHOP_HELO_FCRDNS, SPF_HELO_NONE,SPF_NONE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS16276 94.23.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from processus.org (ns366368.ip-94-23-14.eu [94.23.14.201]) (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 ; Mon, 2 Aug 2021 00:36:54 -0700 (PDT) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by processus.org (Postfix) with ESMTPA id ECA385101324; Mon, 2 Aug 2021 07:36:47 +0000 (UTC) To: Serhii Smirnov , Rowan Tommins Cc: internals@lists.php.net References: <22EF92EC-C5A3-498E-9EFD-DE2B5B204BB7@gmail.com> <6e668a49-d69c-7b6d-f9c4-1d3fe2058d27@gmail.com> <7FAE3AE2-F8C1-4774-9B84-721DE8FF49D4@gmail.com> Message-ID: <410fc4de-17d9-1268-1995-25ab0b2505de@processus.org> Date: Mon, 2 Aug 2021 09:36:46 +0200 MIME-Version: 1.0 In-Reply-To: <7FAE3AE2-F8C1-4774-9B84-721DE8FF49D4@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US Authentication-Results: processus.org; auth=pass smtp.auth=pierre-php@processus.org smtp.mailfrom=pierre-php@processus.org X-Spamd-Bar: / Subject: Re: [PHP-DEV] RFC: autoconst From: pierre-php@processus.org (Pierre) Le 01/08/2021 à 17:59, Serhii Smirnov a écrit > I was more interested in autoconst keyword that reduces copy/pasting constant name as it's value. camelcase and snakecase is an addition. A real example why I wanted to use autoconst: on the past project, we had class that holds constants with the feature flag names. Something like: > > class Features > { > const FEATURE_FLAG_WITH_SOME_LONG_NAME = 'feature_flag_with_some_long_name'; > const FEATURE_SOME_ANOTHER_COOL_FEATURE = 'feature_some_another_cool_feature'; > } > > later, in the code we had to check if certain feature is enabled in the system: > > if ($featureManager->isEnabled(Features::FEATURE_FLAG_WITH_SOME_LONG_NAME)) { > // some feature related code goes here > } > > The class Features could be written as: > > class Features > { > autoconst lower FEATURE_FLAG_WITH_SOME_LONG_NAME, FEATURE_SOME_ANOTHER_COOL_FEATURE; > } Hello, Having an implicit by-convention name reducing algorithm from constant string seems very obfuscated and dangerous, especially for a preference name. This means that if you fix a typo anywhere, it'll implicitly change the underlying acceptable value for anywhere your configuration comes from (env var, database, configuration file, ...). This means that will create a maintenance burden for anyone using such autoconst this way. The right way to handle those situation is not by writing less code, but by writing more, you should explicitly map your preference names over in-code constants, any attempt in doing otherwise will cause you trouble at some point later in time. So I don't think this is a good example, in my opinion this is more an school use-case example of what you should never do ! Regards, -- Pierre