Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116038 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 657 invoked from network); 13 Sep 2021 13:26:50 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Sep 2021 13:26:50 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9CC761804C0 for ; Mon, 13 Sep 2021 07:05:47 -0700 (PDT) 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-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) (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, 13 Sep 2021 07:05:47 -0700 (PDT) Received: by mail-qk1-f169.google.com with SMTP id b64so10619803qkg.0 for ; Mon, 13 Sep 2021 07:05:47 -0700 (PDT) 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=4QfV27Qq7TbRRhGMqq1uJrKGUek9XEDrbQt8KhstcqA=; b=Oq4izHTj8ly2dBR5b/1bEAmLR4B8AUzfXvTn3CiRVlqSP0Y9KLmmhHz8HabF3Ctq3n 5ckYpoWd1nKGuMoXuaMyVCJeqhYZbdiW66B38yhWxJ3ic4Fqo2mHYn/WsVPNrp2fLyI1 GaJ6C/nx+eQh2ZioABk57jXSy5JBcOQqhoKVZesCT8Yg9IUnpoSXkaRozSC0yGqmX21a 7BAZ8SV779yx7ETjXBfQWk9b6UKhyHpfSS5+WJVA3yOmh4D7O6hu9nRf7pdlH/90QszU 9tjQ3FxMSNYLk3akH8xWhd3EVkGl2UzU93QGSJoKLqPXtZ9qo94OLDlNWdbch0fLs1F8 tKwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=4QfV27Qq7TbRRhGMqq1uJrKGUek9XEDrbQt8KhstcqA=; b=xWq6drKgsWZcB1c3Xy8tMH3u8wk8n6S32LU7oW2pvES6Wy81LtNajscSE0nLUdcXhh UWqoKEmDFnPAsE7IH+b335F+1n7oi8NCup7Gmo1gUddIoBGQQDaPllhEwxn73Kyu7Ww6 fr9B32xArkOeZNQvMA8k43QwwsqbqiLuVKHl5D7jML2cEih5WK2JgdIv3g16GO3S689A lZJdvTzCitVL2HS7+VSVN/cLVV5Lofhwxa8MFfdLKJsfTUIrMJYIQMJg0FH6fbwO2t+l J9fLH9yNWssRh+ZJwNrN4JlE8lBB1uzt3FLR2AtsF0MMB/8RaiVKptt0brM4/NdPjR/H d+ZQ== X-Gm-Message-State: AOAM533kSNd3fra90fzqxSeGKgvLg+KWEsazo6XfagsSmeH9XWLPXHXU celjqFSCZlC5Z7J/oDvKcnyGzA== X-Google-Smtp-Source: ABdhPJwzN3AZgql08sWAZxtopTsrJGlWazbqWPVUCTrqR3b9v3M34PrX5hj1wGtx5Dzr6OOKD7PwGw== X-Received: by 2002:a37:6144:: with SMTP id v65mr9603983qkb.457.1631541944690; Mon, 13 Sep 2021 07:05:44 -0700 (PDT) Received: from [192.168.1.10] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id s204sm5339041qke.109.2021.09.13.07.05.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Sep 2021 07:05:43 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) In-Reply-To: Date: Mon, 13 Sep 2021 10:05:43 -0400 Cc: David Rodrigues , PHP Internals Content-Transfer-Encoding: quoted-printable Message-ID: <538C20B2-3491-49DE-BCC0-47F43A51B98F@newclarity.net> References: To: Marco Pivetta , Rowan Tommins X-Mailer: Apple Mail (2.3608.120.23.2.7) Subject: Re: [PHP-DEV] Class static initialization block From: mike@newclarity.net (Mike Schinkel) > On Sep 11, 2021, at 7:35 PM, Marco Pivetta wrote: >=20 > On Sun, 12 Sep 2021, 01:28 David Rodrigues, = wrote: >=20 >> Hello! >>=20 >> I would like to suggest a feature I saw being implemented in the V8 = 9.4 >> engine called "*class static initialization block*". >>=20 >> https://v8.dev/blog/v8-release-94 >>=20 >> In short, it is a method that is executed once when a class is = imported and >> loaded, allowing for some more advanced initialization process to be = done. >>=20 >> class Test { >> static readonly Carbon $now; >>=20 >> static () { >> self::$now =3D now(); >> } >> } >>=20 >> Currently I can only do this in a very hacky way, usually by creating = a >> public static method and calling it after class initialization. >>=20 >> class Test { >> static Carbon $now; >>=20 >> public static function init(): void { >> self::$now =3D now(); >> } >> } >>=20 >> Test::init(); >>=20 >> I think the way the V8 does is much more interesting. >>=20 >> Another alternative would be to create a magic method like = __initialize(). >>=20 >=20 > This already works without any magic. Not true. Here are three (3) things you cannot currently do with your = proposed solution and without said magic: 1.) Use static analysis to recognize a class has an initialization = functionality and thus provide lint-specific to best practices for = static initialization. 2.) Make the initialization function private. 3.) Override and/or change order of initialization of parent in child = class. I do not know of use-cases of #3, but #1 and #2 use-case should be = obvious. > ``` > MyClass::horribleInitializationPractices(); > ``` You named that in a rather condescending and passive-aggressive way. Is = that really helpful for respectful dialog? > That's really all there is to it. >=20 > What you define as hacky is really normal/boring, and does not need = special > constructs. >=20 > Also, it's generally not a good idea to sprinkle static mutable > runtime-bound all over the place: it should be an exception, not a = rule. Valid use-cases I have come across: 1. Hooking actions and filters in plugins for frameworks and CMS that = use hooks as an extension mechanism. 2. Initializing what would ideally be constants but because of PHP's = constraints regarding constant initializes cannot be constants. > On Sep 12, 2021, at 6:42 AM, Rowan Tommins = wrote: >=20 > On 12 September 2021 00:28:02 BST, David Rodrigues = wrote: >> Hello! >>=20 >> I would like to suggest a feature I saw being implemented in the V8 = 9.4 >> engine called "*class static initialization block*". >=20 >=20 > Hi David, >=20 > There was a similar proposal for PHP a few years ago: = https://wiki.php.net/rfc/static_class_constructor >=20 > Although it didn't go to a vote, the proposal was dropped for lack of = support. There's some discussion summarised in that RFC, and you can = probably find the rest by searching for its title on = https://externals.io >=20 > Regards, >=20 > --=20 > Rowan Tommins Here is the external thread: =20 https://externals.io/message/84602 Rowan, you were definitely the most vocal one to argue against it with = the most messages on that thread. But all the arguments against appeared to bog down into discrediting = Johannes Ott's desired use-case for static class constructors and did = not really look for other reason why they would be useful. As I mentioned above, hooking actions and filters in plugins for = frameworks and CMS is where it would be useful, especially since these = generally need to be hooked upon initial load of the plugin.=20 Also, initializing variables to act as constants because PHP does not = support complex initializations on constants is another use-case. One of your arguments against was the timing of the initialization = because evidently Johannes Ott wanted them to be lazily evaluated. = Given the use-case I just stated I think they should always be loaded = immediately whenever the file is loaded. That makes that argument about = debugging cause-and-effect becomes a moot point.=20 And lazy evaluation should be considered as a separate feature. I = blogged about that a while back but never brought to the list: https://mikeschinkel.org/2021/lets-add-lazy-evaluation-to-php/ The other main argument you had against was Singletons. But that was a = moot point because one of the reasons to use static initialization *is* = to instantiate a Singleton. Java has static initialization blocks[1], C# has static constructors[2] = so the idea they would be useful for PHP is not a stretch, -Mike [1] https://www.geeksforgeeks.org/g-fact-79/ [2] = https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-a= nd-structs/static-constructors