Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107989 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 70322 invoked from network); 5 Jan 2020 18:58:36 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Jan 2020 18:58:36 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2A0651804A8 for ; Sun, 5 Jan 2020 09:03:18 -0800 (PST) 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.2 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS11403 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 5 Jan 2020 09:03:17 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 274AB213F4 for ; Sun, 5 Jan 2020 12:03:16 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Sun, 05 Jan 2020 12:03:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=AO/Zl4wnc191Wl92TQjL6xMScNYQN2bibF6c2JKTb wk=; b=vW4+6Krb3heSqw/KS1HXbh8Q4nASXpnVxaj7S9ULV5jCquA+Q8LpzUlI8 5Rj6SyQBYwwHSFQ1n7QpTvwHKkIJeNoGSTtYTODhq1+b1T52unpuLBpfkRCLotqC lRMRf/RQ6CDkgvjov5glta0IsykBFU2/MSOjk1ZdG15dNidF46wtDQIPO8p2ZewU KUms8YoJN1h1/OJdnCeQXiplxuKkt4TIGKznBmoT2+aMsXMeOP+mMmQ2Eskvp2nK R/cPQLtkz29/p9U1GyjD5a6nwtxRYHhO0nD1LYbhEg8cGsgYEuAia8Y3PU6SXluF AZvgUDQxog8B2T4jCQbW7wxgvNMsw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvdegkedgleeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlught vggthhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id DD41B14200A2; Sun, 5 Jan 2020 12:03:15 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.7-694-gd5bab98-fmstable-20191218v1 Mime-Version: 1.0 Message-ID: <28c08b74-6c5a-429f-bd3d-1dbea2faebf2@www.fastmail.com> In-Reply-To: <21FF0232-D305-4E8F-9117-41D007E339D0@newclarity.net> References: <5e0d723f.1c69fb81.e2ae8.24e2SMTPIN_ADDED_MISSING@mx.google.com> <74F2DBFC-E63C-428C-A37F-2D0CEE15AD0F@newclarity.net> <70cbf5ab-2445-4003-987e-cb26301d64de@www.fastmail.com> <7A150D33-2434-401B-B687-E74B6E0F0AF8@newclarity.net> <21FF0232-D305-4E8F-9117-41D007E339D0@newclarity.net> Date: Sun, 05 Jan 2020 11:02:22 -0600 To: "php internals" Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Initializing constants once, with code? From: larry@garfieldtech.com ("Larry Garfield") On Sat, Jan 4, 2020, at 3:25 PM, Mike Schinkel wrote: > > On Jan 3, 2020, at 2:52 PM, Larry Garfield = wrote: > >=20 > > There's two broad reasons I think this is a bad idea. > >=20 > > 1) Constants are one thing. Function calls are another. They serve= different purposes. Trying to mutate them into one thing can only lead= to confusion and lack of understanding about what is actually going on.= > >=20 > > 2) The approach you describe (of starting with constants everywhere = and refactoring to method calls later)... I would never do and do not en= dorse. What you describe is basically "make globals nicer to work with"= , whereas I am 100% firmly in the camp of "if I could remove globals fro= m the language entirely I would". Frankly, the use of constants for con= figuration is an anti-pattern to begin with; they should be used only fo= r things that are truly constant. Honestly, I cannot recall the last ti= me I used constants for anything other than giving some other compile-ti= me value a nicer name. (Eg, DEFAULT_THING_VALUE or giving nice names to= bit flags or something like that.) > >=20 > > For configuration, my answer is frankly "put your configuration behi= nd a nice configuration object from the very beginning and then you won'= t have to refactor it later; Problem solved." You can use env vars for = configuration, and wrap those into a nice object, possibly using one of = the many DotEnv implementations that already exist to make them nicer to= work with in development. That is superior in basically every conceiva= ble way to semi-mutable globals passing themselves off as pseudo-constan= ts. > >=20 > > I *would* love to see property accessors come back, which would have= a side effect of making what you describe a little easier, but at no po= int is it pretending to be a compile time value when it isn't. > >=20 > > --Larry Garfield >=20 > So much to unpack here, but I will just hit the most important= points. *snip* > Symfony =E2=80=94 which many PHP developers hold in high regard includ= ing=20 > Drupal developers =E2=80=94 uses constants: Just an FYI: I assume you mention Drupal specifically here because of my= history with the project. What Drupal developers hold in high regard m= atters not even 0% for me at this point; "Drupal does it" is not an argu= ment that will carry any weight with me. *snip* > I think this analysis proves many PHP developers do use constants in=20= > ways you claim you would never. IOW, the use-cases I am trying to=20 > address exist to a significant degree in the wild. >=20 > Further I believe I have made the case that there would be significant= =20 > value in allowing developers who previously used constants because of=20= > prior lack of skill or too-tight timelines to be able to evolve their=20= > code to initialize their constants with code instead of forcing their= =20 > users to change their code or worse. >=20 > -Mike I think you are overstating my case. I am not arguing that one should n= ever use constants. In fact, a majority of the examples you cite I woul= d say are reasonable uses of constants, at least in concept. (I cannot = speak to the particulars of the individual code bases.) I am saying that the behavior of a constant is that it is... constant. = A proper constant is evaluated at compile time and appears as a literal = in the final runtime code, with no dereferencing. AIUI, `const` in PHP = does exactly that, in contrast to `define`. That is fundamentally conce= ptually incompatible with a value that is derived at runtime and happens= to use constant-style syntax. I am firmly opposed in principle to havi= ng a language construct that looks like a constant and makes you think i= t's a constant but is actually a method call that does who knows what. There's really two different use cases here, I think, that we're conflat= ing. ("We" meaning I think we're all doing it to an extent.) 1) I want an approximately global value that is auto-memoized on first u= sage, so I can access it from anywhere without having to recompute it or= do the work of memoizing it myself. 2) I have a code base with lots of constants, and I've changed my mind a= bout the API (for whatever reason) and want them to no longer be constan= t but runtime defined. For point 1, I'd love it if we had some way to auto-memoize a result. T= hat would be sweet. There's a couple of ways we could go about it... bu= t I firmly believe that sticking it behind a const-like API is the wrong= way to do it for the reasons above. (I wouldn't want to limit it to gl= obal-ish things, either, because global-ish things are, as stated, bad i= n every way and should be avoided whenever possible. Yes, I know there'= s a lot of bad code out there already with it; that doesn't mean encoura= ging it.) For point 2... if you want to change your API, then your API is going to= change. That's what it means to change an API. It's no different than= deciding to rename a method or class. It may or may not be hard depend= ing on the API and the install base, but that it's a constant doesn't ch= ange matters. Making code easier to refactor is all well and good, but = not if it violates all expectations around what a constant is. For derive-once global constants, you may be looking for `define`. That= 's a runtime operation and effectively boils down to a set-once global. = It doesn't help with constants defined in a class, but for an already-g= lobal constant `define` already gives you a one-time set operation, just= not on-demand. That's probably the better avenue to pursue for option = 2, though again it's not one that I find worth compromising the language= design for. --Larry Garfield