Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107987 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 41264 invoked from network); 5 Jan 2020 00:36:38 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Jan 2020 00:36:38 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 989BB1804F3 for ; Sat, 4 Jan 2020 14:41: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.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_NONE 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-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (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 ; Sat, 4 Jan 2020 14:41:04 -0800 (PST) Received: by mail-yb1-f180.google.com with SMTP id l7so12515268ybp.1 for ; Sat, 04 Jan 2020 14:41:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=7rRe2C92Sr+XfNrR9AJgW7ATfMLAiInP1TZV6brG53w=; b=ITF9OcOTQTNwRjMQCY0VnZXxgNQxnuwZdg/vUpwBXppEBFabxAOzv97USjone4mX3o O9YpWuprDikS34WzI2eHItxndcVvKbkDTuN0EHYRueUjoxtiNwKdKcarV25hv9ltCR+i LlZzfvkIQ9F7IS6kItB6kdr0O2rb7805SOuLQWB9KAeRvcacMYEZ2CsmwfoppHtG2NmE 6qYp5DF5pQBABGXsajjUQDycKcY8+xPbxjfsLRWj6BiuLBGP9mkw6PgHicyHZOnVYUqN ipeBaUbBnwhVoG11j9GK1HDGQybFfPmEYIj4smRiTnNTOVsXvYikP68uE2rqU2CXgYjj MVzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=7rRe2C92Sr+XfNrR9AJgW7ATfMLAiInP1TZV6brG53w=; b=myay7hW3j8qN8W+sMMlTOE6lzBlJnXkeDdzMmzt52QvoW1GvQ2XPsfIUtsVuLoa3t6 1ff2nlHoVDNfTbfoIlw368RJ2DbkYt98NEjfK3Zhd8X9xO6W4BgtZYiPjJ2pm++6cVeA ngMeb5KoWWiZBAqgnidO/GF28tXV3dqTMniaROAfgRMRXZUDOzXFibWzvWJYRuKBpueH eNYGpy5054mNlbJxWFhGp0wCb89eHSVLFCo+SI55ybjixKNQCtws/PY/wZyETPXmLd9s dNa2Rcr53ExwZFs2gObSn3j9bKesffx/rIpX3s3GtArHVGlGF4tGw1l92X6pdfJbLOP9 Tlmw== X-Gm-Message-State: APjAAAU97G7/z0Jw005/g4vwMmFeiVW9SK7HkEDyYJEw2HYmyz/Jt6Rf 1FAZyoFTW1IKJG0TEeo7r8/nWQ== X-Google-Smtp-Source: APXvYqzDQMcQyagjzgpof7dQ9CEN6uA/5RriRINO61QVZzVFhZUkkwjKCBUY1Mnn5Dtbt2fatKqSmA== X-Received: by 2002:a25:447:: with SMTP id 68mr64250374ybe.432.1578177660299; Sat, 04 Jan 2020 14:41:00 -0800 (PST) Received: from ?IPv6:2601:c0:c680:5cc0:1c3b:f16f:3708:29d8? ([2601:c0:c680:5cc0:1c3b:f16f:3708:29d8]) by smtp.gmail.com with ESMTPSA id g190sm25314058ywf.41.2020.01.04.14.40.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 04 Jan 2020 14:40:59 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) In-Reply-To: Date: Sat, 4 Jan 2020 17:40:58 -0500 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: References: <5e0d723f.1c69fb81.e2ae8.24e2SMTPIN_ADDED_MISSING@mx.google.com> <74F2DBFC-E63C-428C-A37F-2D0CEE15AD0F@newclarity.net> To: Rowan Tommins X-Mailer: Apple Mail (2.3445.104.11) Subject: Re: [PHP-DEV] Initializing constants once, with code? From: mike@newclarity.net (Mike Schinkel) > On Jan 4, 2020, at 10:49 AM, Rowan Tommins = wrote: >=20 > This is, as so often with such comparisons, an exaggeration: the = docblock for a function need not be any longer than the docblock for a = constant, and the function body can be simplified. Fair point. =20 > Add in the "initialise on first read", and you end up with 4 lines per = pseudo-constant vs 5 per method: But then you give an example which exaggerates in the other direction. =20= The point was when constants ARE used they result in a lot more compact = and infinitely more scannable code. Thus when you do not actually need = to use initialization code you do not have to and you get much more = scannable code, but you have the _option_ to add initialization code = _later_: class Environments { const PRODUCTION =3D 'master'; const STAGING =3D 'stage'; const TESTING =3D 'test'; const DEVELOPMENT =3D 'dev'; } vs.=20 class Environments { static function PRODUCTION() { return 'master'; } static function STAGING() { return 'stage'; } static function TESTING() { return 'test'; } static function DEVELOPMENT() { return 'dev'; } } What I am asking for is the ability to _evolve_ code over time and not = force code to forever retain its technical debt simply because of = initial choices.=20 And not just for constants, but for evolvability of all code. To me that = seems like such a no-brainer best practice for programming language = design that I am struggling to understand the fundamental objections to = it. > Overall, I agree with Larry that these aren't really constants, and = shouldn't be treated as such. The fact constants are limited to literal code is merely a limitation of = the opcode compiler, not a conceptual difference. In both cases they = are initialized before use, and then never changed. If they are = immutable once initialized, then they are conceptually constants.=20 And if the opcode compiler could offer more expansive initialization = than just literals then my use-cases would likely be addressed. Another approach would be for me to ask for an `immutable` option that = has the behavior I am proposing, but that would not empower the millions = of constants in the wild that already exist to evolve and would only = benefit new code: class Environments { immutable PRODUCTION =3D 'master'; immutable STAGING =3D 'stage'; immutable TESTING =3D 'test'; immutable DEVELOPMENT =3D 'dev'; } Yet another approach would be for me to ask for would be for a shortened = function declaration syntax. But again that would also not empower = existing constants to evolve: class Environments { function PRODUCTION =3D 'master'; function STAGING =3D 'stage'; function TESTING =3D 'test'; function DEVELOPMENT =3D 'dev'; } > Property accessors, or some native support for lazy-loading properties = (which would stop Doctrine needing to do tricks with unset() and __get), = would seem like a better fit for the use case. I would also LOVE property accessors added to PHP, but I disagree it = would address the use-case of existing constants in libraries in the = wild (and in future code) that are forever cursed to remain as hardcoded = literals to ensure backward compatibility. Both you and Larry have redefined my use-case to an idealogically = convenient use-case. Can we please address the use-case as I am = defining it: existing code that already uses constants and future code = where developers use constants because of lack of skill, awareness of = "best practices", or too-tight timelines? =20 Maybe there are other solutions for the use-case I am trying to address = besides the one I proposed, but we can't get there if everyone redefines = the use-case to support their arguments. -Mike=