Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119812 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 58264 invoked from network); 31 Mar 2023 13:23:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Mar 2023 13:23:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 842EC1804B3 for ; Fri, 31 Mar 2023 06:23: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.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29838 64.147.123.0/24 X-Spam-Virus: No X-Envelope-From: Received: from wout4-smtp.messagingengine.com (wout4-smtp.messagingengine.com [64.147.123.20]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 31 Mar 2023 06:23:52 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id B9D313200936 for ; Fri, 31 Mar 2023 09:23:51 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Fri, 31 Mar 2023 09:23:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-type:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1680269031; x= 1680355431; bh=pdbNHlV3hFw2vkxAyj6ttfxEMUAwTBUItK/e6abc4Io=; b=Y 34KkMmVmRDZtTR5wN676C/Yfk7KhRHm7oQ4VIsxJjsheXD70ERz+U7MlnlMnzTER FWNChiESXnwlXPRpAzymrX27KGQz1Hr+o7uEN8eyT0Eu3Djk5pj8fcx453hJboHV Fkp1kBD74VTaGqbXUElptxihNdDY8aJ7OwWuhIe3RwcSezuyVuaSzbkw+h0aESIv zoiqywKKQ6CEENokX0zHlHLgikzZ2aunkXWjSYp4yOGQQc0FAEXPJ3SBN5uzmL2f aPP6DwlsELRZr96ZksLw6GL6iIak8V3/CJUMPv2EMEh4LjvBndp2heuAWqlohovm B9/zoAnURpEdPGlFX6WJg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; t=1680269031; x=1680355431; bh=pdbNHlV3hFw2v kxAyj6ttfxEMUAwTBUItK/e6abc4Io=; b=MAsv0dwlkTZSsTZjPVUwLeTs7guE4 Dts/Tvz7RRN8ytXTXLF26D1jZduOWeBTWKnWIe88/3xJSqnO7JMKLhVW3tleKhZ3 plLAVHD5eOm2JqGcxzTEwCWh9uONuSfXagKp9bJQ4a35Vy0mAumH/psnIbbeAy1C XxWXcAj5CtaYGbwSOp8IEDhe1hLkdQx+uirSDiEcfSDlQFrfQF2jSD0r8ftcrxnp XdW6nd9m60f0DNskKr82p/+BiZUV7DHRCehzqOoqGeydVAN0feB8M7kybfM+r3Be 6WjkuoR44gFK+yhqWQnwAt4LAmcczIyhtEyuxFp46a1XSVevm3CAl8ntw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdeiuddgiedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeevheehvdevjeelvdevgfelvefftdejkeelvdekgeeh fffgiedvjefhhfeltdduteenucffohhmrghinhepphhhphdrnhgvthenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhi vghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 2391D17000A0; Fri, 31 Mar 2023 09:23:51 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-238-g746678b8b6-fm-20230329.001-g746678b8 Mime-Version: 1.0 Message-ID: In-Reply-To: References: Date: Fri, 31 Mar 2023 13:23:04 +0000 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] Property Hooks Discussion From: larry@garfieldtech.com ("Larry Garfield") On Fri, Mar 31, 2023, at 8:56 AM, Robert Landers wrote: > Hello, > > I couldn't find the thread about Property Hooks > (https://wiki.php.net/rfc/property-hooks) -- I think it got embedded > in various places? Or maybe my search-fu in gmail is failing me. > > Anyway, I was reading it carefully and I had a couple of questions. > > What would happen with the following class: > > class BigNumbers { > public \Gmp $myNumber { > beforeSet(int|string $value) { > return gmp_init($value); > } > } > } > > Would $bigNumber->myNumber = gmp_init('25') throw an error because it > isn't a `string|int`? Can hooks only accept the type they are setting > or any type? > > In other words, will it be possible for this to no longer be true (I'm > not sure it is always true now, fwiw): > > $x = $y = $z = 1; > > $x === $y && $y === $z; > > This looks like a very powerful change to the language and I'm really > looking forward to using it! As Rowan said, this is a "known unknown" at the moment, and one of the reasons the RFC hasn't been officially put forward yet. (Expect that soon.) As it is right now, the value beforeSet receives MUST be the same as the property is typed as. You're not allowed to declare a type yourself; the property type is implied. However, that still would allow you to narrow a type yourself in practice. The RFC has an example or two of that with strings -> UString objects. We are considering (and I would favor, if it can be done cleanly) allowing the beforeSet hook to accept a contravariant (wider) type than the property type, but still be required to return exactly the type of the property. That would allow, for instance: class Foo { public int $val { beforeSet(int|string $new) => (int) $new; } } That is, you can accept a wider set of compatible types and then, in beforeSet, fold that down to the type of the property. The type of the property is still guaranteed to be the only thing you get on read. That would mean the following as a side effect: $f = new Foo(); $val = '5'; $f->val = $val; $f->val === $val; // false, because different types! Personally I think that's acceptable for the flexibility it gives; the point of hooks is primarily to be "a thing you can add to a property in the future without having to precreate a method just in case", you can already do the above behavior with a method quite easily. So being able to do the same with hooks (and thus obviate another "just in case" reason to add a verbose but pointless method on the off chance you need it in the future) seems reasonable. There might be other edge cases we haven't thought of yet; right now that feature is pending on Ilija making sure it could even work, which is a prerequisite for including it, obviously. :-) --Larry Garfield