Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116032 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 1096 invoked from network); 11 Sep 2021 22:57:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Sep 2021 22:57:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D8E541804AA for ; Sat, 11 Sep 2021 16:35:53 -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-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.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, 11 Sep 2021 16:35:53 -0700 (PDT) Received: by mail-oi1-f180.google.com with SMTP id p2so8754267oif.1 for ; Sat, 11 Sep 2021 16:35:53 -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=PcyIwNxNF5agH+ZdkCjxvXeoxePHP8spnsmg6nCjUXQ=; b=NqTYdTjbgDAfBp6DgPiCPtiAVOz/vFm5t/Yezzw4x/f0lbJ5Rq5KUKLfD5CfzNoJMA mPcNTvaqJufPI+c9aIthVtxFn2m1FpFg9QBN/nw/gyykpVM713nBw5Uixt65s1eaOjua gOR66oYN8BxCOUFLOQdTwdypmwQZI2p/5qhcw25eNUamE85xaoEpAezwiaPq8pN//1Hj uAbG36cWSBG12gV5/XAEP2kXxEETBfFzkAqtlgucvnS3MKkJsmv6DNUqeDEsv8vYSqnX jcLzKNuE6ZFhVB7drIQAPjPxaF5DTy+oWk10wa+m4p4mw1EFyVQ67vC+NmFEneRAGfkP zzjA== 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=PcyIwNxNF5agH+ZdkCjxvXeoxePHP8spnsmg6nCjUXQ=; b=NDlUaLVaGfwt+k1as+B3C8w+ueQ1j4RITo6fPDi3Q4ivs1/N7u48DNbEdw+tCdrAua XC397Pi4oydnBkh5dCB1jquI6EQxmLF+WGC/2CSOkyq5rakeijQS4uVQM/kmYJgIjh6R 3YVdrwnLInIqEjYyG2rpGoS+/XJHSFAANkLuQXmkSFv/0jHDLNNyFphorigHtXTbB0He ufhGeZZd4f4VFnaT8gsNtLWgGqEaaYItY4Nc4tLyEKLN77S8LzvVzgf90FAVKcYOFMLD UY2OzVSt5Mda6PKA6UdrXo3OOo0S0hJ2tqu/v9vRLulHr62yBAi8KBesr4a6ElCDdY70 STJw== X-Gm-Message-State: AOAM533yBYZyzh9ONekgqy9dR3Ppf99uegs6+v+fJ+m+FXvp5C1NT3uP K18cNH5U746KLuqeNtpC9u6Axvn8DJqE4IpGMTpMcsQ9KV0= X-Google-Smtp-Source: ABdhPJzlYIHLUtWs4CoBSIknBrVdk6QpPMaoO2sz/VrvJK5yLiPaL9TqITI+6gceBo86OH9xpJO4s3y8sYR30gUiRZs= X-Received: by 2002:a54:4513:: with SMTP id l19mr3175367oil.143.1631403352641; Sat, 11 Sep 2021 16:35:52 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 12 Sep 2021 01:35:41 +0200 Message-ID: To: David Rodrigues Cc: PHP Internals Content-Type: multipart/alternative; boundary="00000000000051d4fb05cbc0b1c0" Subject: Re: [PHP-DEV] Class static initialization block From: ocramius@gmail.com (Marco Pivetta) --00000000000051d4fb05cbc0b1c0 Content-Type: text/plain; charset="UTF-8" Hey David, On Sun, 12 Sep 2021, 01:28 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(). > This already works without any magic. In `MyClass.php`: ```php class MyClass { public static DateTimeImmutable $startup; public function horribleInitializationPractices() { self::$startup = new DateTimeImmutable(); } } MyClass::horribleInitializationPractices(); ``` That's really all there is to it. What you define as hacky is really normal/boring, and does not need special constructs. 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. > --00000000000051d4fb05cbc0b1c0--