Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123373 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 D20F31A009C for ; Mon, 20 May 2024 17:41:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1716226962; bh=iBjwIOdsATn1oremhad8qC/YqmhvdQmkE96Likb66l8=; h=References:In-Reply-To:From:Date:Subject:To:From; b=PfdYM1XG0MeCz3ECACwSD/s/OegRCZh6q2iWCnmkf5Sez8Q0Zi7cF8zJcHNDCfSsZ E3moXAcb48Z2uvc6Qv0/LH1uRoNM0X0wGodBnCRM+Ce6yolWuWw0cAwKCuB+r1YCWJ YxCxZrNAw7/4F0uYl1MoU2DRwGxCuebs+m8f7L9pKUeLBZ9Yws+rOZTjcTrjD6j9YQ X2WAfSnDza+tDa4kiSq696UexrSb/EXY9yonU/jme8OXCFtZgdfzZ7OyIP7kGBu0CK T2X66bNihEJePpV+yZ9i1MA0kR79zneOanAGYbqpoXWPvF+NBiVfOfaoHdVicpVCXO 81J4jaeYo7OuQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 207ED180554 for ; Mon, 20 May 2024 17:42:42 +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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS,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 mail-vk1-f172.google.com (mail-vk1-f172.google.com [209.85.221.172]) (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 17:42:41 +0000 (UTC) Received: by mail-vk1-f172.google.com with SMTP id 71dfb90a1353d-4df344eecd7so1181807e0c.3 for ; Mon, 20 May 2024 10:41:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716226905; x=1716831705; darn=lists.php.net; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=lGa9NW6qV5uUoIpoKDBuwDWuTiniIgsu6153tPvbc7k=; b=QRuoeufSV4iCU+Rh6wCEvp5qgMJSdXvWSghOoOPtCWySZQh2OPloblcQXek8/J6jZM rExY2E9ycGmfLGWQUZUzAHxTTzjIp6qac80Iwng2uI/70UPrplQEIpzQzhuu0kgOXsVx pl4x70d34H5O2JVm/4amU5cuFR1zL0vrZMDSdKO+agnPSDJ841XTn46pS8tbZaKCn/nM gTsAtXKINNis+yH6H+OHzD1ybpYDNA8ZBQ0GYT0w6LmxOPgRUi5s2bbXFGwfLHfMf1of IxneiIEb2MTUAU2sLii7yKFpbLv1SnVFb//3/9IX1jguNPP+D5CvsSWeFr4tkHYMlO7T u6kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716226905; x=1716831705; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lGa9NW6qV5uUoIpoKDBuwDWuTiniIgsu6153tPvbc7k=; b=K8cHA2x8M/xEP/JtKsgG39n+BSsDGvufXVITHWmG+llTveWkLNrw24HQ9LXnfj7fEJ 5j4A/HAZS2+VEhhc+07PKaozZbxwEJTrWNX7wsd9/DYlvX6WQ8fkQWpdnHq5y3B7ReXn JvnDjXm4lwuyo0hG2oy8KJEQCfrupfJjCyelxmEcyhQBk8jo0Vg2rQJwGAxKWdOXIwaA CJCZzzJTkzM68vSNycbu8xFMbY3CNn8RxJOu8WW8SbX2ikDghqcvOtJ29AiEZC3S359b HsH2Q2vdSHBqpZtrazlroJv9d1OEvjE2Jcbv1aw8/tbRuDyCn1XP7nQlw2+of5IpVLUC gRog== X-Gm-Message-State: AOJu0YwoHQypPLuxv05idfTIM5hMThy4ZwJyG3oGJZDheWPDERCipnbF JE0qfdBtidUSsM/tXWR+awymfHe6KKrWWOlEG8g/nvCmscnP8tJJtYjw3b4Na2hsCO8+QdHjZY6 vq43LKpLcVidzUqtBFMuTi0lZbr9Ohnpf X-Google-Smtp-Source: AGHT+IFbF8RI+NVIuWcruh7JUnCcfmGkkWDDU38lLmjv+23Uh67cucU9r5R4lCud1RQC129tcLEkT9wFdGYJUcT8HAU= X-Received: by 2002:a05:6122:3c91:b0:4d3:45a2:ae53 with SMTP id 71dfb90a1353d-4df8838b136mr23397446e0c.16.1716226905313; Mon, 20 May 2024 10:41:45 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 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> In-Reply-To: <133158d0-1371-4b7c-a2f4-242bfe99ce10@app.fastmail.com> Date: Mon, 20 May 2024 14:41:09 -0300 Message-ID: Subject: Re: [PHP-DEV] [DISCUSSION] Checking uninitialized class properties To: php internals Content-Type: multipart/alternative; boundary="0000000000000b9d410618e637dd" From: ericklima.comp@gmail.com (Erick de Azevedo Lima) --0000000000000b9d410618e637dd Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > 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 =C3=A0s 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 fa= ir > 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 giv= e > 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 > --0000000000000b9d410618e637dd Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
> All of which is to say that, yes, there are use = cases for an "is this=20 property initialized" check that is less fugly than the=20 get_object_vars() hack I have to rely on now.=C2=A0

I'd like to see this:

if ($objectVar-&g= t;property is uninitialized) {
...
}

=
or/and:

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

--
Erick de Azevedo Lima

Em seg., 20 de mai. de 2024 =C3=A0s 14:28, = Larry Garfield <larry@garfieldtech.com> escreveu:
On Sat, May 18, 2024, at 5:41 PM, Rowan Tommin= s [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 a= llowed to have a sentinel.=C2=A0 Uninitialized is their sentinel, for bette= r or worse.
>
> Sorry, I don't understand this statement at all. A readonly proper= ty
> 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 rea= lly need to be able to differentiate between null and uninitialized.=C2=A0 = Even if you think the uninitialized value is a sign of an error, it's c= oming from code you don't control so you have to be able to handle it s= omehow.
>
> 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 incom= ing data may not be able to guarantee an object is in a valid state.=C2=A0 = I have to do everything via reflection and/or visibility-busting closures.= =C2=A0 I also maintain an attributes library that, necessarily, has multipl= e methods that get called post-constructor before the object is "ready= ."=C2=A0 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 tool= s, works on the assumption that your readonly properties are 1. Based on co= nstructor params; 2. are always guaranteed set after the constructor.=C2=A0= While those are true in the majority case, they're not true in the uni= versal 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.=C2=A0 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."=C2=A0 (As noted, I ma= intain multiple such libraries.=C2=A0 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_var= s() hack I have to rely on now.=C2=A0

--Larry Garfield
--0000000000000b9d410618e637dd--