Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:108687 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39365 invoked from network); 19 Feb 2020 21:05:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Feb 2020 21:05:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 25EA01804F4 for ; Wed, 19 Feb 2020 11:21:51 -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_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-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) (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 ; Wed, 19 Feb 2020 11:21:50 -0800 (PST) Received: by mail-qv1-f50.google.com with SMTP id g6so722016qvy.5 for ; Wed, 19 Feb 2020 11:21:50 -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=4chAW/ayw/su8/4o5E2UbWGoysC1I84mmY+ASy2O7jw=; b=KGUL6VcEYoRWHoIAtjvaSFtwnnHLm6y8hQDbhhFvgq7+lYiEdH5hDLK/Pazt05yFph 3gX1C8Usaeu9hY3ftJFbmU+BHoNiWGqBIraIs9Yf1bElZEvtahNP6B0se++QERYlJATj 9PtngH953FjcXGxNOmsRKNqdIA7pz9hLRn7Db3N6ZhfHyG+ITewNTY1qdUIIJzFP4suk HiW24eACV/ohUPIxo0TlqmBFMt7r77Dvq8ytMqg8dje8Q2GXOQBhMvoF3cqhs3Y0OQ9k jGBf3CGWuWKyS5IhbE7QV0ut1NvrCojfMWwZK9nFtuHK6zoJIGPyx0Za/az14YkwgmxG K8hQ== 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=4chAW/ayw/su8/4o5E2UbWGoysC1I84mmY+ASy2O7jw=; b=Wm5DX6JSFjPIBgibyKQfSzLAdc/oJCbxuOyIxPZB/Z9HXL0f9RVB38aD6II2lu+QGl B0rc42xQQ7F5LShOB5+wx6WYAmFlYyO7oZFdkIkOR+NoUQ0TGUn/XsD7GRM3UrC/uTPB I+W0lYGKzfPKQguSFZVe9JIgmKYLd2TksP5BQWLT0htYxrypfB/Z9gc5f1gLKuOtNMaX 2XS95wk8P8edfBZpGpBwrE0suBtIeEqJ9vOvLDZEWUELofWKwFxzPTU8bPxlw0Bqk/pv MXIL42NNNvoflmeSQB1LJdD3yK/m0Km3vKXRYYHrd6pAspSxQrb2805z3301t7k2eUtO jizw== X-Gm-Message-State: APjAAAXilFCHsxeJu1gZgZuxk/hL9dwOeS1r221qWZH+zC71pvCOP30D MBYq10Q9uPYXwRjC3K1nWBZEtw== X-Google-Smtp-Source: APXvYqwcdfLhLPip4Q/QekHzyLdIwmhzFUCKC8xNU9lq4ToUdQkwnvDOQAAB5npcH98oQ/QGWJEsYA== X-Received: by 2002:a0c:e146:: with SMTP id c6mr21524311qvl.116.1582140109113; Wed, 19 Feb 2020 11:21:49 -0800 (PST) Received: from [192.168.1.10] (c-24-131-44-78.hsd1.ga.comcast.net. [24.131.44.78]) by smtp.gmail.com with ESMTPSA id c73sm326143qkg.86.2020.02.19.11.21.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Feb 2020 11:21:48 -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: Wed, 19 Feb 2020 14:21:46 -0500 Cc: tyson andre , "internals@lists.php.net" Content-Transfer-Encoding: quoted-printable Message-ID: References: To: bishop@php.net X-Mailer: Apple Mail (2.3445.104.11) Subject: Re: [PHP-DEV] Re: Straw poll: Places to allow function calls in constant expressions From: mike@newclarity.net (Mike Schinkel) > On Feb 19, 2020, at 2:00 PM, Bishop Bettini wrote: >=20 > Consequently, the only way to safely initialize class constants and = static > members is at run-time, and I can only think of one way to do it. = Apologies > if this has already been suggested: taking a cue from C#, a static = class > constructor ([1]) would allow us to have expression-initialized = constants > and static members. >=20 > class Config { > public const URL =3D null; // compile time initialization > protected static $mtime =3D null; // compile time initialization > private static function __constructStatic() { > $env =3D json_decode(file_get_contents('config.json')); > self::URL =3D $env->url; > self::$mtime =3D filemtime('config.json'); > } > public function reload() { > if (self::$mtime < filemtime('config.json')) > self::__constructStatic(); > } > } > } >=20 > echo Config::URL; // assert: runtime has already called = _constructStatic > $config =3D new Config; // assert: __constructStatic called only once = by > runtime > $config->reload(); // instance may call its own static constructor > ?> >=20 > Trying to do the same for global constants, static variables, or = function > default parameters resists a similar initialization mechanism because = we > don't have defined common entry point analogues. If we had a main(), = or if > we had framed @before decorators, we could do something similar, but = we > don't -- so I feel we should leave these off the table. For different reasons than this RFC I have wanted a static class = constructor for a really long time.=20 I hope this is something we can seriously consider. Thanks Bishop for = proposing it. -Mike=