Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108018 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 20461 invoked from network); 7 Jan 2020 02:25:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Jan 2020 02:25:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 652691804F6 for ; Mon, 6 Jan 2020 16:30:00 -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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, 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-yw1-f66.google.com (mail-yw1-f66.google.com [209.85.161.66]) (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, 6 Jan 2020 16:29:59 -0800 (PST) Received: by mail-yw1-f66.google.com with SMTP id n184so22700090ywc.3 for ; Mon, 06 Jan 2020 16:29:59 -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=tcGhZlAm4eBEL5qOq5uh1cpnxq/eM0L7mBKOUXmw+wE=; b=Wz+6JgXrpHhqHFEL3bo8pFk+e4FMNhFPhNVuN+9A49QsbCNj0v1e0HhRdN95wwavgY sQh7sRFkBqlxeg9g8+bO60Po4yOVLnj9JpLIkf2xFJQZzmmYlhoYlgvhbvMzF+fHI8nA ltTbf4E0ZsRjkfdur52bAoZCI417wgmPndf5jM2dVEOuih0/vE2L1Hguw+18qL+WV3rd WP+QCwydiQN7cO/caC2BdEY+7rSeWjFkJRK3b+aLVjEwbebhdmS7donUshe0kIU4NA2s sqK1xgY5GwyfoO6Rav0pF7FQLLfpoTWgoqryjudj3/6Jt0AyHTkYW2OICQ5LtmQe/qHg CZww== 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=tcGhZlAm4eBEL5qOq5uh1cpnxq/eM0L7mBKOUXmw+wE=; b=IvCPZXKOCl2htqdNo2ylxEj7+n7IE3Qe+5Mv63qv2Tz23sRUXXnvg2LBltt0/PBB03 epLTRMF29W2bpk34s+TWX7S3x6JwcBcHAH6Q9X2FT7g9CGYJY4xT4+RQ+cavYwC2uoQ8 VDfOf47J0ND3avCK5ADm1WvuXudr0KTHHq3uRaKYOmxQC0snkLtOGipRydweflo28dY5 mHkBEExdCk80IaWJUbt5HdulXKppxO3gS9oJZ+prF51HOKIGFQr2sAJ7hojoJzLpUgVD d4m8gCnfwLpkrDcB0GsSL8kPZAemfk8qfLEGDJo+TEmfnHFu/3jFHclcwi2FyHo7Jmci fmFQ== X-Gm-Message-State: APjAAAUOPl/k+y7DLQ0OAygeRxCpI3QBHkbjiceYN4ErCoIjWzl/7bUf Mic3pykSPrfRRBpoZattD5ugVHzXTXzJ7w== X-Google-Smtp-Source: APXvYqwSQtxb3FjRINVKLwIenjaY1xu+xA/eARiHQ0odKBGhGil+mMefpsjiZqh0Bu8vc8vZ4AosiQ== X-Received: by 2002:a81:f00d:: with SMTP id p13mr648195ywm.275.1578356998225; Mon, 06 Jan 2020 16:29:58 -0800 (PST) Received: from ?IPv6:2601:c0:c680:5cc0:d059:ddab:12ae:4ac7? ([2601:c0:c680:5cc0:d059:ddab:12ae:4ac7]) by smtp.gmail.com with ESMTPSA id w74sm28654252ywa.71.2020.01.06.16.29.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Jan 2020 16:29:57 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) In-Reply-To: Date: Mon, 6 Jan 2020 19:29:56 -0500 Cc: php internals Content-Transfer-Encoding: quoted-printable Message-ID: <010807A2-E26A-45B6-9F15-8415CDA5CD71@newclarity.net> References: <5e0d723f.1c69fb81.e2ae8.24e2SMTPIN_ADDED_MISSING@mx.google.com> <74F2DBFC-E63C-428C-A37F-2D0CEE15AD0F@newclarity.net> <54CAFCD7-2B99-4BA4-8A5F-3D7EBC9D810B@newclarity.net> To: Larry Garfield 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 6, 2020, at 4:24 PM, Larry Garfield = wrote: > For example: >=20 > const DEBUG_MODE =3D false; >=20 > function foo() { > if (DEBUG_MODE) { > // Do stuff. > } > } >=20 > PHP will currently (AIUI) inline the DEBUG_MODE value at compile time, = notice that the code path is unreachable, and omit it from the compiled = opcodes entirely. (I'm pretty sure it does that now, at least; if not, = it certainly can be.) >=20 > If you use a define, however: >=20 > define(DEBUG_MODE, $_ENV['debug'] ?? false); >=20 > function foo() { > if (DEBUG_MODE) { > // Do stuff. > } > } >=20 > That optimization cannot happen. But just from looking at foo() I = cannot tell which is going to happen. I view that as a negative. To be fair, you cannot tell in the first case either, unless your = constant is in the same context as foo(), which it typically would not = be. You can track down DEBUG_MODE and know it is fixed, but if you ship the = code for someone else to set DEBUG_CODE, you can't know what they will = set too. So I am not seeing a big distinction other than with compiler = optimization. And if the alternative is to use a function, that can't be = optimized any better so it seems moot to me. > No, it's because you cannot override them. It makes the conditional = logic around defining them more complicated. Specifically, with = WordPress for example,... Ah! You and I actually are on the same page there.=20 I have a project that is a local development environment (WPLib Box) and = wp-config.php's structure has been a huge PITA for us. I am completely = in sync with you here. In fact I wrote an alternate wp-config to resolve that problem, but then = had to task switch and I have not put any effort into evangelizing it: - https://github.com/wplib/better-wp-config Which brings up the point; it would be *really* great if we could = redeclare declare() constants in PHP, for automated testing purposes. > Now suppose you have a lazy-loading "constant" that uses $_GET. If it = gets called before that conversion happens in some code paths but not = others, its value will be unpredicatable. It may get built before = $_GET['q'] is changed or after, which would change its value. I concur. However, if I do that in a function it will be the same = issue. =20 I think it just comes down to us having a different value judgement. To = me I don't see it being a problem if the bug is in a constant or a = function =E2=80=94 it is still a bug =E2=80=94 but you clearly classify = those differently. > That's not something I ever expect a "constant" to do. =20 To be fair, if constants were able to be dynamically initialized in PHP = 8.0, you could change your expectation.=20 But as you say, that's not your preference. > (Is this a subjective and partially emotional interpretation? Of = course it is. Much of language design, like any other design, is. But = I don't think my subjective view here is unique.) Maybe for PHP. But clearly the designers of Ruby, Javascript and Python = thought differently, no? > The specific use case you presented is not one I believe PHP should = support. If I have not convinced you of my position on that yet, I = don't think I am going to, nor are you going to convince me of yours. Fair enough. So if I were to create an RFP you'd get a vote "no", and = sadly I will not get a vote. So it depends what others think. > Certainly. But a good language nudges people toward writing good = code, or at least not-bad code. (How hard it should nudge is a subject = of much debate and is one of the reasons we have many languages.) I strongly agree with that.=20 I just disagree with your assertion that what I am proposing would = result in "bad" code. > That wasn't what I was thinking of, although I suppose that's also a = concern. I was more thinking of the inconsistent initialization time, = as in my earlier example, leading to a constant that is variable = request-to-request even if it's nominally constant-once-set within a = specific request. That's a very different behavioral pattern from = guaranteed consistent request-to-request. IMO they should remain = separate. Okay. Thank you for engaging. I think we have ended up having a well = reasoned discussion and have laid out our respective positions = succinctly. You feel strongly that inconsistent initialization would be = a concept and I few that as just bad coding that would need to be fixed = no matter if the code initialized a constant or if it ran through a = function. I guess we'll just have to see how others feel about the subject. -Mike P.S. another use-case I remembered for constants is poor-man's class = annotations, which I actually use a lot. For example, I use a POST_TYPE = constant in a class designed to load a WordPress post of the given type. = It would be really nice to be able to initialize those types of = constants differently based on testing vs. production.=