Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123375 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 1469E1A009C for ; Mon, 20 May 2024 23:36:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1716248268; bh=wWtGhj/jgXgvIySqVZeOY6iYJPvaN/8VoewPUWJj9D8=; h=Subject:To:References:From:Date:In-Reply-To:From; b=PUCrIBms4L6AEQ2l6PPndKdGmJjkqD7ff5uDWcuULEHNE4IXWzaPROfM3ORXpF0Nw 1y1UJ+neLmFT+BTjljpOTtuVavgbIlG6zsIiy5WklAnH23SVPc6Nojv3DcREgawYVy Wup10OJZqMyzjxUJm0Zdg4BsPTrVBHXeEYDLsQAJcQQdpAuuSRfSYK+65mRPLSUcHK jNpFMGsC/kisdGJ8NIC2VsomZbwPS4yBj226T6jMBv93Ly+Mt2GXy0tHguRDhHVcnp cfbRRPwLAlslSDX19ma7BIYekXbZ3vFOQWp3/CvUjmn5QVPcznOB/ObvgzLnpO6nrS pGvnLmOEsyyDA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CE21318005F for ; Mon, 20 May 2024 23:37:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: * X-Spam-Status: No, score=1.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_40, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING, HTML_MESSAGE,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from giraffe.ash.relay.mailchannels.net (giraffe.ash.relay.mailchannels.net [23.83.222.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 20 May 2024 23:37:45 +0000 (UTC) X-Sender-Id: a2hosting|x-authuser|juliette@adviesenzo.nl Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id CCF06903EEE for ; Mon, 20 May 2024 23:36:49 +0000 (UTC) Received: from nl1-ss105.a2hosting.com (unknown [127.0.0.6]) (Authenticated sender: a2hosting) by relay.mailchannels.net (Postfix) with ESMTPA id 9EC0B902E2E for ; Mon, 20 May 2024 23:36:48 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1716248208; a=rsa-sha256; cv=none; b=izxKZMtcPITUyavwa4zc/o78Y9RNZhioilAqcSWsvFX9wTNFCDiwZdvvHLZGoqFi3Cva4Y qXWkLR46F+nXZ982IcK1oSRixCIa6AR04jO/37cWufo1Na1sgZJldfunK9TOQD765jJLWz goyuQxiZ9zi/auD7fxbxRnhX62Q6/XXsYSrIlK7S2iVUo6qwXmyO901YsPwlDBnkZeaUkB bHA+zJXcBzIrwiu1f+XDhgMRDZUesrjtQbNYrAeieNW/Vyp3e1huRcqfnhqmc6cHhUU28W XZ9O4L65f5GjtNRe5oL8TA7sAD1a8iyuamiy66kvG2chW0KvRoMZvEaqBpnYNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1716248208; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XoH3jy+KvgoBOBS+W+1PsdKyq4fwxaNOt5a0S6HfXOU=; b=oUMzYw0FU2OdCBvp37iRi847pgUhG3OP6AtXWQj9s2m+Pv0Em9MfjG++FcB3SGiEB1LMNx rFmThVPnSt/YF1Bt5orxMyRPC5yyqxkSWdLjYZwbRnZX7tYR3e6wMKyzLYmkLCyTA+BzXw yOsfdJdwmnKAfsWXn/d7tjj6GjLnxrOIwgyzd4SCGeZrwrf2dA/8fKICuOXSHHwEuiS6EH TL2UFsy2tIaPSzXrsyhRDuNfTMdbxTyWSePK5JqwaSC8F2YScjaMeuXGpJSAtGPm1XrwS3 WhLEktLf56UNsFnvswRatLfMaaPmGvjUuO5e21GCS6kfWnru1bZ9xwioNFKGUQ== ARC-Authentication-Results: i=1; rspamd-5d55749bb4-pwdvj; auth=pass smtp.auth=a2hosting smtp.mailfrom=php-internals_nospam@adviesenzo.nl X-Sender-Id: a2hosting|x-authuser|juliette@adviesenzo.nl X-MC-Relay: Neutral X-MailChannels-SenderId: a2hosting|x-authuser|juliette@adviesenzo.nl X-MailChannels-Auth-Id: a2hosting X-Cellar-Occur: 6016c5ad562b1da8_1716248209201_3635462614 X-MC-Loop-Signature: 1716248209201:2173696553 X-MC-Ingress-Time: 1716248209200 Received: from nl1-ss105.a2hosting.com (nl1-ss105.a2hosting.com [85.187.142.69]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.124.123.111 (trex/6.9.2); Mon, 20 May 2024 23:36:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=adviesenzo.nl; s=default; h=Content-Type:In-Reply-To:MIME-Version:Date: Message-ID:From:References:To:Subject:Sender:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=XoH3jy+KvgoBOBS+W+1PsdKyq4fwxaNOt5a0S6HfXOU=; b=aENg2kaCloAbF0kHD77XBsN8sz nyvq6aWX1JwUY6tUJZVWgPvvGeoKzX0v6/erDUn4Nk036Jx2o8LQd2uYa58sVy5Zl93g9ycxqiY6V YASObsFpobRcgxLtiY7eO/nWMj/z7Lp/aU0+o6evfo1728Vo5l8vjXPHB6mP/wg0PVJs=; Received: from mailnull by nl1-ss105.a2hosting.com with spam-scanner (Exim 4.97.1) (envelope-from ) id 1s9CYg-00000000Xjf-3Pb9 for internals@lists.php.net; Tue, 21 May 2024 01:36:46 +0200 X-ImunifyEmail-Filter-Info: UkNWRF9WSUFfU01UUF9BVVRIIFJDVkRfVExTX0FMTCBWRVJJ TE9DS19 DQiBSQ1ZEX0NPVU5UX09ORSBCQVlFU19IQU0gTUlNRV9VTktOT1dOIE FSQ19OQSBNSURfUkhTX01BVENIX0ZST00gSUVfVkxfUEJMX0FDQ09VT lRfMDUgTUlNRV9UUkFDRSBGUk9NX0VRX0VOVkZST00gRlJPTV9IQVNf RE4gVE9fRE5fTk9ORSBSQ1BUX0NPVU5UX09ORSBJRV9WTF9QQkxfQUN DT1VOVF8wMSBUT19NQVRDSF9FTlZSQ1BUX0FMTCBfRFJVR1NfTU1fRE lTQ09VTlQgQVNO X-ImunifyEmail-Filter-Action: no action X-ImunifyEmail-Filter-Score: 2.62 X-ImunifyEmail-Filter-Version: 3.5.14/202404301715 Received: from [143.178.147.245] (port=55922 helo=[192.168.1.16]) by nl1-ss105.a2hosting.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.97.1) (envelope-from ) id 1s9CYh-00000000XjQ-3ehM for internals@lists.php.net; Tue, 21 May 2024 01:36:46 +0200 Subject: Re: [PHP-DEV] [DISCUSSION] Checking uninitialized class properties To: internals@lists.php.net References: <878c09ca-860e-4c0c-85ae-2cd0246cda3c@rwec.co.uk> <5014b401-6341-44c6-a16e-f453ab52220c@app.fastmail.com> <47E8FDF4-9805-4A0B-A2E1-55CF8721253E@rwec.co.uk> <133158d0-1371-4b7c-a2f4-242bfe99ce10@app.fastmail.com> Message-ID: <664BDE7C.8080509@adviesenzo.nl> Date: Tue, 21 May 2024 01:36:28 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------080000000703010201030907" X-AuthUser: juliette@adviesenzo.nl From: php-internals_nospam@adviesenzo.nl (Juliette Reinders Folmer) This is a multi-part message in MIME format. --------------080000000703010201030907 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 20-5-2024 19:41, Erick de Azevedo Lima wrote: > > All of which is to say that, yes, there are use cases for an "is > this property initialized" check that is less fugly than the > get_object_vars() hack I have to rely on now. > > I'd like to see this: > > if ($objectVar->property is uninitialized) { > ... > } > > or/and: > > if ($objectVar->property is initialized) { > ... > } > > -- > Erick de Azevedo Lima > > Em seg., 20 de mai. de 2024 às 14:28, Larry Garfield > > escreveu: > > On Sat, May 18, 2024, at 5:41 PM, Rowan Tommins [IMSoP] wrote: > > On 18 May 2024 17:13:49 BST, Larry Garfield > > wrote: > >>However, that breaks down with readonly properties, which are > not allowed to have a sentinel. Uninitialized is their sentinel, > for better or worse. > > > > Sorry, I don't understand this statement at all. A readonly > property > > can be set to PatchState::KeepCurrentValue just like any other. > If the > > intention is that that state will be overwritten with an actual > value > > later, then it's not a readonly property. > > > > I guess you have some different scenario in mind? > > > > > >> And as I noted earlier in the thread, when writing a serializer > or other dynamic systems (an ORM probably would have the same > issue), you really need to be able to differentiate between null > and uninitialized. Even if you think the uninitialized value is a > sign of an error, it's coming from code you don't control so you > have to be able to handle it somehow. > > > > If a property is uninitialized, the object is in an invalid > state, and > > attempting to read that property gives an error. That's by > design, and > > as it should be. > > > > Are you saying that you want to be able to detect the error > before it > > happens? Why? > > For context, remember I maintain a serializer library, so I have > to support objects that may indeed be in an invalid state, and > depending on the incoming data may not be able to guarantee an > object is in a valid state. I have to do everything via > reflection and/or visibility-busting closures. I also maintain an > attributes library that, necessarily, has multiple methods that > get called post-constructor before the object is "ready." > Admittedly neither of these are common cases, but neither are they > invalid cases. > > Readonly's current design, and the (IMO, very bad) support from SA > tools, works on the assumption that your readonly properties are > 1. Based on constructor params; 2. are always guaranteed set after > the constructor. While those are true in the majority case, > they're not true in the universal case. > > IOW, "this readonly property is not set by the time the > constructor is done, so your object is invalid, so your argument > is invalid" is not a fair or accurate statement. And even then, > lots of code needs to be able to inspect an object to determine if > it is valid or not, even if just to give the user a better error > message than "Oops, you tried to read an uninitialized property, > we gonna crash now." (As noted, I maintain multiple such > libraries. ORMs would be the other big use case, I think.) > > All of which is to say that, yes, there are use cases for an "is > this property initialized" check that is less fugly than the > get_object_vars() hack I have to rely on now. > > --Larry Garfield > If we're talking syntax and introducing new keywords anyway, why not go with a new language construct like `is_initialized($obj->property)` ? --------------080000000703010201030907 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 20-5-2024 19:41, Erick de Azevedo Lima wrote:
> All of which is to say that, yes, there are use cases for an "is this property initialized" check that is less fugly than the get_object_vars() hack I have to rely on now. 

I'd like to see this:

if ($objectVar->property is uninitialized) {
...
}

or/and:

if ($objectVar->property is initialized) {
...
}

--
Erick de Azevedo Lima

Em seg., 20 de mai. de 2024 às 14:28, Larry Garfield <larry@garfieldtech.com> escreveu:
On Sat, May 18, 2024, at 5:41 PM, Rowan Tommins [IMSoP] wrote:
> On 18 May 2024 17:13:49 BST, Larry Garfield <larry@garfieldtech.com> wrote:
>>However, that breaks down with readonly properties, which are not allowed to have a sentinel.  Uninitialized is their sentinel, for better or worse.
>
> Sorry, I don't understand this statement at all. A readonly property
> can be set to PatchState::KeepCurrentValue just like any other. If the
> intention is that that state will be overwritten with an actual value
> later, then it's not a readonly property.
>
> I guess you have some different scenario in mind?
>
>
>> And as I noted earlier in the thread, when writing a serializer or other dynamic systems (an ORM probably would have the same issue), you really need to be able to differentiate between null and uninitialized.  Even if you think the uninitialized value is a sign of an error, it's coming from code you don't control so you have to be able to handle it somehow.
>
> If a property is uninitialized, the object is in an invalid state, and
> attempting to read that property gives an error. That's by design, and
> as it should be.
>
> Are you saying that you want to be able to detect the error before it
> happens? Why?

For context, remember I maintain a serializer library, so I have to support objects that may indeed be in an invalid state, and depending on the incoming data may not be able to guarantee an object is in a valid state.  I have to do everything via reflection and/or visibility-busting closures.  I also maintain an attributes library that, necessarily, has multiple methods that get called post-constructor before the object is "ready."  Admittedly neither of these are common cases, but neither are they invalid cases.

Readonly's current design, and the (IMO, very bad) support from SA tools, works on the assumption that your readonly properties are 1. Based on constructor params; 2. are always guaranteed set after the constructor.  While those are true in the majority case, they're not true in the universal case.

IOW, "this readonly property is not set by the time the constructor is done, so your object is invalid, so your argument is invalid" is not a fair or accurate statement.  And even then, lots of code needs to be able to inspect an object to determine if it is valid or not, even if just to give the user a better error message than "Oops, you tried to read an uninitialized property, we gonna crash now."  (As noted, I maintain multiple such libraries.  ORMs would be the other big use case, I think.)

All of which is to say that, yes, there are use cases for an "is this property initialized" check that is less fugly than the get_object_vars() hack I have to rely on now. 

--Larry Garfield


If we're talking syntax and introducing new keywords anyway, why not go with a new language construct like `is_initialized($obj->property)` ?

--------------080000000703010201030907--