Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123348 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 DCD4C1A009C for ; Sat, 18 May 2024 14:40:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1716043299; bh=cElH9zIZHry0vPRsHpal8tHMuJWG7ACnK/9iz+UxOQI=; h=Date:Subject:To:References:From:In-Reply-To:From; b=dHcdv3YXXKoNw3RVE5pz8VMMtr/0Hh8idFlbuDsmCrAaj/9x08Uqpq+MZDUcFlD0w FhTyOTQtWbsZ/5q+9TNRpEltAX7lpIS/4OPHTfCU/0wGpPQxD4w8PHRko8rOlUTeGJ xL7uVQDypbmji8In843z6Tr7cOUSDBxVS+h7ei6k6XL7426kQXC3jHB2/r2/ICkIAb vpILIUi4HxEcTguyEW6x1T23RYzXHPWGsdmNObBo/GAr+lSGKT1LIIeXEfINk0J8x3 Ez5s++z7+k3uAbNMrXQGUWZ/41QkHyKWN+hQVY+Wdvd+TYtn03flrwxmQiOcBxJtqx IatNDyyGyBvyQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CBF7918057D for ; Sat, 18 May 2024 14:41:37 +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_MISSING,SPF_HELO_PASS, 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 wfout8-smtp.messagingengine.com (wfout8-smtp.messagingengine.com [64.147.123.151]) (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 ; Sat, 18 May 2024 14:41:37 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.west.internal (Postfix) with ESMTP id C4CED1C000BB for ; Sat, 18 May 2024 10:40:41 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Sat, 18 May 2024 10:40:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1716043241; x=1716129641; bh=lmhgV04tON9Lm2vdzIVzbkMILmiQk9jMOgXHCE4baYU=; b= FtXWzbHhpHiunlEGQXv7KZjuSfiiEdnipLgk3FcC/ofs2+OSEsXcPYgG0/+GIJCK ZhiqAOFPaLBBU3TEIiI7WYfT30QnrXAm1d3HRe9ClHtSTFrSw3Po9E4MU54UXt1c DuXHg9m3hw6kpiZL8G8HizETKuSxASCWTBmzw/nO67b3z7sDbAFU0fgBC71DlPNF DHvnS/4N6PBo0fNjUQmkmbMVDrhdcPzoxMIRDtLXif2sDzH01swrXIWkxuhVIMZS 701HoTQSzLNmbQjx9OWkZNIS5OPIEJm5caBztNDSEYs15gfTsivEuxCqNOr+7R0t KG5LfqOgf572pnq/Mzmdzg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding: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:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1716043241; x= 1716129641; bh=lmhgV04tON9Lm2vdzIVzbkMILmiQk9jMOgXHCE4baYU=; b=V JTy0RJZXkZS7Unh0dArRkU3vEg4favBSgM8gZ11KycNi53BoivCKmHBlQtIHNefV PnU11tyyistOfRaJobspwbvgGUvK43onaMAAldJpgKR1voKuPXHL1H2k/GAiZaZ8 5+qQZ5ixlTxK2AFgqcm3t6+srgWeI76KEGQHVB5/w/FD3gxh1pIqQFzJHmjJfUDf QqL6B+b7x3u5u/y0zGf55XOPSlVdlmatKDDHbUSpL08UG5ULuZgiG5QTNNk+U4GT zMX/V4le4pezTkuYmP3yNJ8sHsHc37nOze+FK0MPqDTR1yb06HMGCG1g8mogW459 yTLo8i7uBZ9Z+v7lszsWw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdehiedgieegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgoufhushhpvggtthffohhmrghinhculdegledmne cujfgurhepkfffgggfuffvfhfhjggtgfesthekredttddvjeenucfhrhhomhepfdftohif rghnucfvohhmmhhinhhsucglkffoufhorfgnfdcuoehimhhsohhprdhphhhpsehrfigvtg drtghordhukheqnecuggftrfgrthhtvghrnhepjeeggfehjeevkeelhfdugfffteevgeej heetgfetueeiheegvedtheelhffhueeunecuffhomhgrihhnpeefvheglhdrohhrghenuc evlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhp rdhphhhpsehrfigvtgdrtghordhukh X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sat, 18 May 2024 10:40:40 -0400 (EDT) Message-ID: <878c09ca-860e-4c0c-85ae-2cd0246cda3c@rwec.co.uk> Date: Sat, 18 May 2024 15:40:36 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [DISCUSSION] Checking uninitialized class properties To: internals@lists.php.net References: Content-Language: en-GB In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 18/05/2024 11:52, Luigi Cardamone wrote: > I am already using a solution like the one > proposed by Robert: it works but it > leaves some doubts since each project > can have a different name for NotSet An argument is often made that this is a good thing, in the sense that "null" and other "universal" terminal values combine multiple meanings in an unhelpful way. You'll see this frequently regarding SQL's handling of NULL, for instance - does it mean "unknown", "not applicable", "invalid", etc. A common solution put forward to this perceived problem is "algebraic data types" - a Maybe or Option type for "value or missing", an Error or Failable type for "value or error", etc. PHP doesn't have those (yet...), but the same information can be conveyed nicely with a final class or single-element enum. In your example, the actual value you want to represent is not "Not Set", it's "Keep Current Value", so that's the terminal value you need: final class KeepCurrent {} class MyDTOPatch {     public int|null|KeepCurrent $propA;     public int|null|KeepCurrent $propB; } or: enum PatchState { case KeepCurrent } class MyDTOPatch {     public int|null|PatchState $propA;     public int|null|PatchState $propB; } > Are there any downsides in adding a > specific syntax to check if a property > is initialized with any value? 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". PS: Etiquette on this list is to post replies below the text you're replying to, preferably editing to the relevant parts as I've done here, rather than adding your text above the quoted message. Required, -- Rowan Tommins [IMSoP]