Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107982 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2314 invoked from network); 3 Jan 2020 21:48:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jan 2020 21:48:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E8457180569 for ; Fri, 3 Jan 2020 11:53:05 -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 out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (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 ; Fri, 3 Jan 2020 11:53:05 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 920AB21B82 for ; Fri, 3 Jan 2020 14:53:04 -0500 (EST) Received: from imap26 ([10.202.2.76]) by compute7.internal (MEProxy); Fri, 03 Jan 2020 14:53:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=Uw0EmM oNCb0J2+03KIt29VXarnL21DkG0ksmswV8O58=; b=U5ZnY8ggZzm0aDVnc/b9eb dUjFVfr92by3q/3k7rDvYtX5OBWsVzpK9sF2THHLhRFkjLD5ILYmcNUsNRthrmK3 UwIpaxxLCU8oGs/oQCzt/KTSOuaHPcll6mLo1s1XMHka2WG3yU1drmEa0eu5wZah t4PlEtZn/ZxD0cxJymahprF8SQzKxFP1d/4UXZkCPMjOXLU7rkFY+2VPp1LcbKo1 0UO73YEdskEFQe3F82T6bWNxcJAIrK3LI+/ius0T02RqQut9Yco9yuWOnap219iq B54gkhMyl5cXV9ah2uiRgTY4/T/oyW7Nf/MXw9+p9qGOvpJ7N1ZmNGsZWT0JB+9A == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvdegfedgudeftdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlught vggthhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 0AA2714200A2; Fri, 3 Jan 2020 14:53:04 -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: <70cbf5ab-2445-4003-987e-cb26301d64de@www.fastmail.com> In-Reply-To: References: <5e0d723f.1c69fb81.e2ae8.24e2SMTPIN_ADDED_MISSING@mx.google.com> <74F2DBFC-E63C-428C-A37F-2D0CEE15AD0F@newclarity.net> Date: Fri, 03 Jan 2020 13:52:43 -0600 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Initializing constants once, with code? From: larry@garfieldtech.com ("Larry Garfield") On Fri, Jan 3, 2020, at 12:55 PM, Mike Schinkel wrote: > Hi Larry, > > Thanks for replying. > > > It seems to me it would be much cleaner to just... build that into a static method call with the derivation-and-caching logic behind the scenes. It's not complicated code. > > That is exactly what I have been doing, and what I am finding suboptimal. > > The problem is that, most of the time and especially when refactoring > poorly written code, we just want a constant. One line in the class > file, hard coded. > > We ideally don't want to have to write a full functionr. So for five > data elements, 5 easy to read lines of code with constants vs. 20 > harder to read lines with functions, or at least 40 lines if we include > PHPDoc for those functions, which is part of our standard for writing > functions. > > When we *later* want to be able to refactor to initialize with code we > want to do so w/o having to modify the code that is already using the > constants. But we many never actually need to do so. What I am asking > for is the option. > > Basically, in my view, a programming language is better if it can > support refactoring a reusable unit of code to add additional > functionality W/O requiring the code that uses the reusable element to > be modified. Using your proposed approach we cannot start using > constants and later move to functions unless we modify all code that > uses those constants. That is Bad JuJu(tm) in my book. > > OTOH, if PHP 8.x would allow functions w/o parameters to be called w/o > parenthesis then your approach satisfy the use-case: > > class Stuff { > protected $val; > > public static function VAL() { > self::$val ??= computeStuff(); > return self::$val; > } > } > echo Stuff::VAL; //<== if this could work then using functions would > address the use-case. > > > I agree with the use case, but not with the solution of piggybacking them onto constants. > > Let me put it more concretely. What I am asking for is this: > > > "Transitionally of reusable code when using constants that does not require modifying code that already uses the constant." > > So using functions does not actually address the use-case I am identifying. > > Is there some technical reason why you object to extending constants, > or is it your objection more stylistic? > > -Mike There's two broad reasons I think this is a bad idea. 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. 2) The approach you describe (of starting with constants everywhere and refactoring to method calls later)... I would never do and do not endorse. 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 from the language entirely I would". Frankly, the use of constants for configuration is an anti-pattern to begin with; they should be used only for things that are truly constant. Honestly, I cannot recall the last time I used constants for anything other than giving some other compile-time value a nicer name. (Eg, DEFAULT_THING_VALUE or giving nice names to bit flags or something like that.) For configuration, my answer is frankly "put your configuration behind 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 conceivable way to semi-mutable globals passing themselves off as pseudo-constants. 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 point is it pretending to be a compile time value when it isn't. --Larry Garfield