Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116041 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 33430 invoked from network); 13 Sep 2021 20:33:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Sep 2021 20:33:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DCFE11804C0 for ; Mon, 13 Sep 2021 14:12:24 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) (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 14:12:24 -0700 (PDT) Received: by mail-ed1-f41.google.com with SMTP id j13so16377266edv.13 for ; Mon, 13 Sep 2021 14:12:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=UmGfAYGxPXAkhXRx7CPtlJKyy08H2sLgLmhlv0S6FVI=; b=AZ7n0neWb6MLE7xwBM/FQV7AvKf/4NHyWMueNk1Blq6FziO5boKA32y2jEFGr69Ejd FqeF8i5ifSBIN91rBlK/a74aJPdDUtKbVpdkVN4Ff9jmXJ9kSj9wsGeGUrLNs8YxFAJw 5MGRowr3KIMaGB0nPzO9wvvlRYnA4Kr01qo+H2gDKwt0B91AwmulRACmmGhRwjZTX2v1 y0Euk5t+NwM9+ILY2THyGQ9aXq2/V23DyvJyHIalQDKvE2KmpkL72+1gikHh/FhWb146 yudIPWl3SuXG6Fj/+OapulczrrN2b4u0QO+8gR1aPTkwOSVeyUmuVlQ8S6UulBWa1zLZ 3TxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=UmGfAYGxPXAkhXRx7CPtlJKyy08H2sLgLmhlv0S6FVI=; b=7ZycZyA3VrfDpFLS0sYYlrBJLVLeWmeW9r9NHNZDh8tkuJjHpp+4Z4xhPyCfYYHfda SpLHGCX29s14gIR0GqFa/SmwSAAuGhKFzkIFkIk+kv4wdbNF+zqdIZC6tFXp5xuh+TVD vxSfSgfFyl2xQwt6n77H3eWVGXfxketMNgrAIferU/MZXF7gf047xpRN6ILFXyFixa0O e4423uy0uQaSpSYVcvkxH9++hqFgiduDDs14Gf97KX27to72pC7i6trIk31CnKN/tpHF lYMpmyAJ5OJNT3IdVq8/ZE5OQ5DW4YsJwPZmqX6FUFsoih2yrVfvwuC0Lo4O7a0GGhNs d6bg== X-Gm-Message-State: AOAM530NyNnaQfUBwj4wNbPrr5fSAeo1/1ezO2o6Aj54v9xO8KU8bOty LwdRqCmCMtns/SlGi8rmY7LT/PXi+wrla+b/TL/m0CB2LA7GFQ== X-Google-Smtp-Source: ABdhPJw7ffuvZ0ZkU+PPa/GjSwHC6YghBZVFHTavIYysP2LNKEtmb8vpmx4Rw0p52gzu7apGtm2tHPLCrUY1LKaI2oM= X-Received: by 2002:a05:6402:1011:: with SMTP id c17mr15807681edu.144.1631567543177; Mon, 13 Sep 2021 14:12:23 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 14 Sep 2021 00:12:06 +0300 Message-ID: To: David Rodrigues Cc: PHP Internals Content-Type: multipart/alternative; boundary="000000000000d6940e05cbe6eb21" Subject: Re: [PHP-DEV] Class static initialization block From: drealecs@gmail.com (=?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?=) --000000000000d6940e05cbe6eb21 Content-Type: text/plain; charset="UTF-8" On Sun, Sep 12, 2021 at 2:28 AM David Rodrigues wrote: > Hello! > > I would like to suggest a feature I saw being implemented in the V8 9.4 > engine called "*class static initialization block*". > > https://v8.dev/blog/v8-release-94 > > 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. > > class Test { > static readonly Carbon $now; > > static () { > self::$now = now(); > } > } > > Currently I can only do this in a very hacky way, usually by creating a > public static method and calling it after class initialization. > > class Test { > static Carbon $now; > > public static function init(): void { > self::$now = now(); > } > } > > Test::init(); > > I think the way the V8 does is much more interesting. > > Another alternative would be to create a magic method like __initialize(). > Hey David, The first time I built static initializers for classes was about 15 years ago when, in my class loader (composer was not a thing), if the class had a static method called __init(), it would call it after loading. I don't remember why I needed it but I'm pretty sure I was used with the Java static initialization blocks. Maybe also the limitations that exist on the static values not being able to be dynamically defined could have played a role but I mainly blame it on my lower skill as a programmer. I have never used it in a similar way in the past 10 years, as far as I remember. That's because I almost never use static state as that's just global state and it should be avoided. If you really want to compute some static state, I think that's going to be in PHP in a few years. An attempt to add it with the new keyword was done in https://wiki.php.net/rfc/new_in_initializers#unsupported_positions but was eventually dropped for class static properties and constants. You can read details about it there. Once the identified problems will be sorted out and probably the option to have static initialization blocks will be possible. So far, the current possible approach with having a public static method and calling it after class definition works well in most of the cases. If it's really needed, I think we can use it and be happy that it is supported on all past PHP versions. There is only one place that I know where I think it will not work, if the class is preloaded. But I guess you can just avoid it for now. Alex --000000000000d6940e05cbe6eb21--