Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114905 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 6694 invoked from network); 16 Jun 2021 11:30:53 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Jun 2021 11:30:53 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B9EBA1804C8 for ; Wed, 16 Jun 2021 04:47:38 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 16 Jun 2021 04:47:38 -0700 (PDT) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 631125C0196 for ; Wed, 16 Jun 2021 07:47:38 -0400 (EDT) Received: from imap8 ([10.202.2.58]) by compute3.internal (MEProxy); Wed, 16 Jun 2021 07:47:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=18AxPq 4lFha/Kk2Av+jrv5FbVDdq+r9/3FtJUk2B/JQ=; b=taQdKPHqqXl5SRg370YAqg gSwUCxPSQ8IBHGMnLka6xx7WjSq2wWnTwO5KHdY+NVp2OJfE9s7AOS5uIIA/Lbih bKwRFB56vQWSBzCBLlso73npfNsRZNA+pT+rfnRPxPVVo/moWHom3yIE1a+MMM2q 1Y0AUJLrw0wO0k86KmCk9Mv41hPc7/TrmANMW6QrOncIt5irf1/a2mGIC0srcydk REcb9zmTltlnGq1J7ddFH5TnnV8LNS8Qg3LM6xVnfHpAksXyoHX1frXRzSsS833r LwFzCOV2oSkLJhkAlTjbF6Ax7qWzGTab4xogcbxafEUC8L/EBpHSJRVQ6VffMXHw == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfedvledggeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 1E98F3A040C; Wed, 16 Jun 2021 07:47:38 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-526-gf020ecf851-fm-20210616.001-gf020ecf8 Mime-Version: 1.0 Message-ID: <85f85e0e-a926-4b57-8509-72e9c852c78b@www.fastmail.com> In-Reply-To: References: <88588b8f-5729-4458-90b1-c602f751e128@www.fastmail.com> <33fd3541-8518-4f98-a258-705f85180ed1@www.fastmail.com> Date: Wed, 16 Jun 2021 06:47:17 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] New in initializers From: larry@garfieldtech.com ("Larry Garfield") On Wed, Jun 16, 2021, at 3:16 AM, Nikita Popov wrote: > > Arguments and attributes are enough to justify this RFC on its own, but is > > there a way we can resolve the static property question? Right now the RFC > > says "these initializers are evaluated lazily the first time a class is > > used in a certain way." Can you be more specific about that certain way? > > Is there a certain way that would be minimally disruptive? > > > Well, here is a non-exhaustive description of current behavior: > > * If you access a class constant, only that constant is evaluated. > * If you access a static property, all initializers in the class and > parent classes are evaluated. > * If you instantiate a class, all initializers are evaluated. > * Inheriting from a class or calling a static method doesn't evaluate > anything. > > As you can see, the rules are rather ad-hoc. To the user, it's probably not > obvious why instantiating an object of a class would require evaluating > class constants at that point. The reason is that instantiation requires > resolved property defaults, and we happen to evaluate all initializers at > once. > > The options where static properties and class constants are concerned are: > > 1. Eagerly evaluate initializers on declaration. This is what I tried in an > earlier revision of the RFC, and I don't think that approach works. It > breaks existing code and has various other unpleasant complications. > 2. Precisely specify the current behavior. I don't want to do this either, > because the exact places where evaluation happens are something of an > implementation detail. If in the future we find it convenient to separate > evaluation of non-static properties on object instantiation from evaluation > of static properties and class constants (which are not strictly needed at > that point), I'd like to retain the liberty to make such a change. > 3. Do not specify an evaluation order, beyond that evaluation happens at > certain uses of the class. Evaluation order may change across PHP versions. > If your code relies on any particular order, your code is broken. > > Unless I'm missing a fourth option here, option 3 is the only one I would > be willing to go for at this time. Thanks. To clarify, the concern about evaluation order is only relevant if you are initializing a class whose constructor has some kind of side effect, right? Writing to disk or printing or something like that. Otherwise, at worst you may initialize a few more objects than you expect there should be no behavioral change. Given that constructors that have side effects are arguably broken to begin with (modulo debugging), I'd be comfortable with explicitly saying that the evaluation order is undefined, and nothing is guaranteed except that the value will be there when you first access it. In the future, if function initializers or something like that are added we can revisit that question, though I would be tempted to say the same thing in those cases; if you want to do some kind of DB read in a function that is a default value for a property or a parameter, frankly odds are you're already doing something wrong to begin with. But that's a bridge we can cross if and when we get to it. Would others be comfortable with that, if it allowed new-initializers for static properties and class constants? --Larry Garfield