Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123374 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 781A41A009C for ; Mon, 20 May 2024 22:06:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1716242815; bh=ZKqEFWfyFDuGOE0bBuE3AnK9OTcZNJ2mKF0nzeumb4Q=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=nPgB4jQS4ru8O1rNCAFkzEhYER/KHuotsUn/rWiSx7gNIu8BkJkb4Vn1Q0QrRl7ie i1KA8D5ThMrxRzDUKoJ+RC5k/Qdk6rh0xPxI2AoHmaklgCtgTxvldIsoCqBIQVn1k2 MJTk5I4Jgv+hKUbjuR8OXONds2qxnEDwuLEKS3zqLmNK7hEVa94RlnHF1e/djQwjzS KtxpcTO4bHmQYVuSe3/gWDHADiNCoGLxp++lSGFqPknat1kwdk/ojk0ggxHFSjgHrP 3Y8rer2N2favXV6OChPLwH+zAaR3QH3EjQJY4ZmxKkT9bW/fI79u1J2o8SpkmljcJc VuQFJEjZrI2zw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id BA77218007E for ; Mon, 20 May 2024 22:06:54 +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_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-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (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 22:06:54 +0000 (UTC) Received: by mail-lf1-f49.google.com with SMTP id 2adb3069b0e04-5241b49c0daso2455526e87.0 for ; Mon, 20 May 2024 15:05:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716242757; x=1716847557; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ZKqEFWfyFDuGOE0bBuE3AnK9OTcZNJ2mKF0nzeumb4Q=; b=J5Ny6BSgdevjdGiQ3qG+pWoLrjqlyiFqTUzo9oULZKGbeg9uWlthd2Huati2kjy4d8 Hdr7beWDAXky/a8tUccx/5+rHKixpP+yJ9tphrTvBHK4Q58VNZqYnVQMJbb9y5Hl0wHp i6Y6D/5NIkPM3VmOruaYVpNoxTFYp77XIRsm2onEpHHV/b+3Zv6krKa6ZjfXZ9hYfocp cF9W9wcgTV5mmqSrgHHMslTOPT/7fhFuaJSdar+/7eCRsB32HHsSpakd6QXBUZMYcmrP OKDmtPag/XA+2+vHEMZemyO1IGCrTT2j9RNcuXfrf3c3Dvt0YrZJ1BUPrR2UiXRmL7AX pi0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716242757; x=1716847557; h=cc: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=ZKqEFWfyFDuGOE0bBuE3AnK9OTcZNJ2mKF0nzeumb4Q=; b=jZdQ6KXi/LIgvohdbfCGZd/g6TQHWJg3smcmpJHdjiBG57JMlpCSeD7onLmQlKUFfy pK538E7QvHou+VdoNqpMdDwNWqx/n2IR2W2xD5QxzoZxFB/Pw9A3PjdsGa5rPM12Q+GM lAXARczkuDlA3kmKm4x7XI8KwCqeRmRVJHPaTdK+ELqKYxQhUt5ZlW4zw3+Rq4cHAgR5 aVbH9lcTPUrEsAGAITOpW86kC+0gsjSshWMMI1DXsOhMLsH6mnYy2wRxPlTHw71Dch+n wh+Mg9SHDe8YRVB7+aaa63+nFdTiS2WtSmPGT6tGQrlD9Sj3dfusesaZK3DN2OAMT5a2 iS/w== X-Gm-Message-State: AOJu0Yz1aPlPxviZzmd+xq8zKVWsfQUk8VGCe7S8juqlWaQpGkmztU4F zjSiMPHcIeM5c5DDh1OQ41XNNbtkS62AZenS+Ip5vH8d5hKQxiEHaZrvmDtO8+9HJuHFqAWqQC/ w3tO/6+HCcOXtSeqOT9ZculS9To8Am5xx X-Google-Smtp-Source: AGHT+IFxE6b751hmewZUYczj9cqNNvqMqX2oMKQjK6uKqYnBR3dSWUWvQtz2a2UD2X/ChGrpTTJci9qAdW72ghahbP4= X-Received: by 2002:ac2:454b:0:b0:51d:8283:cf72 with SMTP id 2adb3069b0e04-5221017e670mr17940790e87.57.1716242756868; Mon, 20 May 2024 15:05:56 -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> In-Reply-To: <878c09ca-860e-4c0c-85ae-2cd0246cda3c@rwec.co.uk> Date: Tue, 21 May 2024 00:05:45 +0200 Message-ID: Subject: Re: [PHP-DEV] [DISCUSSION] Checking uninitialized class properties To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000df23e50618e9e7bf" From: cardamoneluigi@gmail.com (Luigi Cardamone) --000000000000df23e50618e9e7bf Content-Type: text/plain; charset="UTF-8" On 18/05/2024 18:08, Rowan Tommins [IMSoP] wrote: > > In my opinion - and I stress that others may not share this opinion - > the entire concept of "uninitialized properties" is a wart on the > language, which we should be doing our best to eliminate, not adding > more features around it. > > As a bit of background, the concept was created when typed properties > were being added, to handle a limitation of the language: given the > declaration "public Foo $prop;" there is no way to specify an initial > value which meets the type constraint. For nullable properties, you can > write "public ?Foo $prop=null;" but since PHP (thankfully) distinguishes > nullable and non-nullable types, you can't write "public Foo $prop=null;" > > Some languages, e.g. Swift, require that all properties are initialised > before the constructor returns, but retrofitting this to PHP was > considered impractical, so instead it was left to a run-time error: if > you fail to initialise a property, you will get an error trying to > access it. > > To track that, the engine has to record a special state, but assigning a > meaning to that error state is a bit like using exceptions for flow > control. It would be more in keeping with the original purpose to have > an object_is_valid() function, which returned false if *any* property > had not been initialised to a valid value. > > > PHP actually has a bewildering variety of such special states. In a > different compromise added at the same time, calling unset() on a typed > property puts it into a *separate* state where magic __get and __set are > called, which they are not if the property has simply not yet been > assigned, e.g. https://3v4l.org/C7rIF > > I have always found this a mess. If a property says it is of type > "?int", I want to know that it will always be an integer or null, not > "int or null or uninitialised or unset". If it needs more than one > non-integer state, that should be specified in the type system, e.g. > "int|NotApplicable|NotSpecified|NotLoaded". > > I get your point. Now I see "uninitialised" as a dangerous thing. Unfortunately this state exists and I don't see any general way to avoid objects with these properties. Fighting the "uninitialised" is something big that we may try to discuss in another thread. We have to deal with this state and a syntax to reliably check those invalid properties will be helpful. Something like "$objectVar->property is uninitialized" will be awesome. Luigi Cardamone --000000000000df23e50618e9e7bf Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On 18/05/2024 18:08, Rowan Tommins [IMSoP= ] <imsop.php@rwec.co.uk> = wrote:

In my opinion - and I stress that others may not share this opinion -
the entire concept of "uninitialized properties" is a wart on the=
language, which we should be doing our best to eliminate, not adding
more features around it.

As a bit of background, the concept was created when typed properties
were being added, to handle a limitation of the language: given the
declaration "public Foo $prop;" there is no way to specify an ini= tial
value which meets the type constraint. For nullable properties, you can write "public ?Foo $prop=3Dnull;" but since PHP (thankfully) dist= inguishes
nullable and non-nullable types, you can't write "public Foo $prop= =3Dnull;"

Some languages, e.g. Swift, require that all properties are initialised before the constructor returns, but retrofitting this to PHP was
considered impractical, so instead it was left to a run-time error: if
you fail to initialise a property, you will get an error trying to
access it.

To track that, the engine has to record a special state, but assigning a meaning to that error state is a bit like using exceptions for flow
control. It would be more in keeping with the original purpose to have
an object_is_valid() function, which returned false if *any* property
had not been initialised to a valid value.


PHP actually has a bewildering variety of such special states. In a
different compromise added at the same time, calling unset() on a typed property puts it into a *separate* state where magic __get and __set are called, which they are not if the property has simply not yet been
assigned, e.g. https://3v4l.org/C7rIF

I have always found this a mess. If a property says it is of type
"?int", I want to know that it will always be an integer or null,= not
"int or null or uninitialised or unset". If it needs more than on= e
non-integer state, that should be specified in the type system, e.g.
"int|NotApplicable|NotSpecified|NotLoaded".


I get your point. Now I see "unin= itialised" as a dangerous thing.
Unfortunately this state exists an= d I don't see any general way to
avoid objects with these properties= . Fighting the "uninitialised" is
something big that we may tr= y to discuss in another thread.
We have to deal with this state and a sy= ntax to reliably check
those invalid properties will be helpful.

= Something like "$objectVar->property is uninitialized" will be=
awesome.

Luigi Cardamone
--000000000000df23e50618e9e7bf--