Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116039 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 10457 invoked from network); 13 Sep 2021 15:47:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Sep 2021 15:47:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CF3E21804CF for ; Mon, 13 Sep 2021 09:26:18 -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.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, 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-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 09:26:18 -0700 (PDT) Received: by mail-wm1-f49.google.com with SMTP id c8-20020a7bc008000000b002e6e462e95fso7462207wmb.2 for ; Mon, 13 Sep 2021 09:26:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=dOrI1jPTx2BKoypeFJ7dkwOIyfnamuLnK2BVyIzURfo=; b=fBTiTOAtOFOrMxDJKsX8xipkUJha5qdJ4Bxez3ouHu6edo310ROAYrdJAusnyfIo9e MuJomEdzN+r549KE5g5GDBOw2DoQGfEij1lRVUM+URCtIm3QwDP4C2u8tEFUrkcgzYJx Xmnel60c8xSCnCBXxZ4T7roT48ILzfzwiDBkqUxOOXOP2SbZDTL9ar/MkoJr+FfbQ7lN xjKd2lh+svxMu8pseXtZ2vDhnWxRGEwioL4NXC48FB2PZ3tX1aXkxNQ3A2Vj/l8IJb6P EFA1QxSvXYiO4Qdg2dXdMaSm6lzD8fe3mUOu8pR0dZXLGZZF4sNwCbMLL4zPHZu4BN0+ mDkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=dOrI1jPTx2BKoypeFJ7dkwOIyfnamuLnK2BVyIzURfo=; b=ZfnKSCg9/30BEUgl5Iy3kfLEO4uI06eWRQs/KWLb7mMi8gGPmqq6G5XJOsZMB+co2U BoRUbZtdRd+bhUPH3MRdzBR1qy8za777KKy/cHBrWlNLR4/sfhnnB/ehYfPcRhFY4FZz unVozXFKGnUF7w6j6GjAMDQtmnLya8VOiM00ZCVwkGO2XZcujZp1jS5tTVHseGkWWJB4 CY8rjvHbdstoxVVV8sGmYgdef3OjAdV/rSOPG1gMxfEiBoncA1b5KIg4HSkqdUnF98rr UpMtIlaClNma2EmsnEKtHI7WPMrDwv3sJ63vhocRGqVVFTHTnW45Au1jHHQCn8I/ca3v XE6Q== X-Gm-Message-State: AOAM530Ezp6+YGtCb+FOz0zNoVProHZTJ1R/p+pk7aNYuHb7ikZucWG7 NdcMDfrfd9ugA0ZwPODJlIYoj0/EP0A= X-Google-Smtp-Source: ABdhPJyfSex3v/cyBCc6CjoBNo5zVwL7NORksDwmqz2WbELhhD2hf/w4kWNHjht6YQvLMOegbdUKVQ== X-Received: by 2002:a1c:e91a:: with SMTP id q26mr12059549wmc.128.1631550374582; Mon, 13 Sep 2021 09:26:14 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id n186sm6624648wme.31.2021.09.13.09.26.13 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Sep 2021 09:26:13 -0700 (PDT) To: PHP Internals References: <538C20B2-3491-49DE-BCC0-47F43A51B98F@newclarity.net> Message-ID: Date: Mon, 13 Sep 2021 17:26:14 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: <538C20B2-3491-49DE-BCC0-47F43A51B98F@newclarity.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-GB Subject: Re: [PHP-DEV] Class static initialization block From: rowan.collins@gmail.com (Rowan Tommins) Hi Mike, I will leave others to discuss the use cases. It would be interesting if people could find the motivating use cases that led to support in C#, Java, and JS. I just wanted to comment quickly on these few points, though: On 13/09/2021 15:05, Mike Schinkel wrote: > 1.) Use static analysis to recognize a class has an initialization functionality and thus provide lint-specific to best practices for static initialization. Theoretically, a convention is enough for this, since PHP has no official static analyser. But certainly a built-in syntax is the strongest kind of convention, so point taken. > 2.) Make the initialization function private. I'm not sure how that would work: a private constructor means that it has to be explicitly called from within the same class, but a static initialiser would be called by the engine, before the class scope even existed. > 3.) Override and/or change order of initialization of parent in child class. Similarly, I'm not sure this makes sense: it would imply that static initialisers are inherited, and would be re-run every time a new sub-class was defined, rather than being run strictly once when the class is loaded. If the child class wants to re-use code in its static initialiser, it would make more sense to call a (protected) static method than to directly repeat the parent's initialiser block. From your link, it seems that C# restricts them much more than methods or constructors: > * > > A static constructor doesn't take access modifiers or have parameters. > > * > > A class or struct can only have one static constructor. > > * > > Static constructors cannot be inherited or overloaded. > > * > > A static constructor cannot be called directly and is only meant > to be called by the common language runtime (CLR). It is invoked > automatically. > Apparently, the JS implementation does allow multiple static initialiser blocks in one class, which are simply processed in order. But JS classes are weird creations, implemented as a complex sugar on top of a classless object system, so may not be the best point of reference for PHP, whose object system much more closely resembles Java or C#. Regards, -- Rowan Tommins [IMSoP]