Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123339 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 9AAE01A009C for ; Fri, 17 May 2024 21:48:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1715982537; bh=PogInY4ZJSe5wIoSl83LZkGJGQX00CcPw9r5rueOFcg=; h=In-Reply-To:References:Date:From:To:Subject:From; b=nZ8ARni4dN5QaF3j1DQ4i8Kp9oYsnbJkDTb1FvQSGsUTtvzJPJePNTrSN1792Euq2 FplSTLXgwn1T5pp+tjwixfLoLK39gTGyfWpUX7Gwny4bIdACeLzBG5vKDc/twsYON2 b2AbYDOkF7gEWaFf67w/UIUZ0XCzYlP5j/NmE2N6Uw/kNlnVEAd4p+lYaOUG3H9tbl K89prJbbIcUeqJi2Z/E6aReIRjk1aDL80Z5CaG8u/zkoGKy2HsXbmFesM1jcJYnLjq jPS2hg8D5hJWbmYpMe7Jn9Rqe7hg3cjrT6NQ9DkDRPRSuyhaZb2hAjkbCWgyR56m26 rwkvo/go8wJxA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D94F318003B for ; Fri, 17 May 2024 21:48:56 +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.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE,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 fout3-smtp.messagingengine.com (fout3-smtp.messagingengine.com [103.168.172.146]) (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 ; Fri, 17 May 2024 21:48:56 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfout.nyi.internal (Postfix) with ESMTP id 24A3C138149D for ; Fri, 17 May 2024 17:48:02 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Fri, 17 May 2024 17:48:02 -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:subject:subject:to:to; s=fm3; t=1715982482; x= 1716068882; bh=6W+2XWOHFDMHZvzzGaqcPKXy/sjfVcoC1V4YLZASJHk=; b=G bZ16LHGNr0YUxqXmbOLMOdpUnyCBxqvODxsopX1PFDZqUqK4jR9aqewIWh/wTdmU +FuDeXChIIA8ovkTVLNyhjQnJm5KzZBB8ijFv1oIIdtHdqMsVOsQYgftke6JT9BD oFYssUvpkfnSDmYmMa0gZtGJaQxroxPD4udm/RNXanXbS7wolhDItdzJQJdrtH+c iv/V7a3JZWDPk62Cl5EJv7oCD3NQDC1oSzlc4DLSLGXRhPWmXxHbdKI2cAdjB3mQ oubcilVY7b2FESuF19wV7whmDchT1N5umcpiJXhemqdR1szDwkVUZZwsuXuP6Qw0 ghVSWEndKDl23DEpwIwMg== 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:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1715982482; x=1716068882; bh=6W+2XWOHFDMHZvzzGaqcPKXy/sjf VcoC1V4YLZASJHk=; b=ZKgGRNzsgWig2DICeZdrHbvAO5q2DnSzvj7DCtOivHDD 5XVvW/IVAWaqO3030j/OUkQN1EfY+zKsIOGAA2h9vgvUtZewcn6wthD9RUF1VBIf H9R3Q4vP47VpzX6zop+o/RBMCnb+vN0q2ihSWVLxA8Opg149GCJw+N6JL9JTGmfl GHqcEX/Mpuh3bZMGGhL2ETnbIsG5oLrhwLUgDFSzRTnjZ7X5rqpuQ4VEUy0pHhbA ckz72TkiEKlbKdqtu9jxYABGbgh+DERMdKFs9M8SmmME5wvjBBzT3MrZE7RqdeKH BrwbAfE8pzpkoQobogYxCL790l5Mwxwk4RJ+44D2qQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdehhedgtddvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne goufhushhpvggtthffohhmrghinhculdegledmnecujfgurhepofgfggfkjghffffhvffu tgesthdtredtreertdenucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolh grrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpeef leeuledtgfegveehkefghfehtdekteetgeegfedukeevhfeuieeklefhtdehueenucffoh hmrghinhepfehvgehlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghm pehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id A6DC11700093; Fri, 17 May 2024 17:48:01 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-456-gcd147058c-fm-hotfix-20240509.001-g0aad06e4 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: In-Reply-To: References: Date: Fri, 17 May 2024 21:47:41 +0000 To: "php internals" Subject: Re: [PHP-DEV] [DISCUSSION] Checking uninitialized class properties Content-Type: text/plain From: larry@garfieldtech.com ("Larry Garfield") On Fri, May 17, 2024, at 9:40 PM, Luigi Cardamone wrote: > Hello Internals, > during last PHPDay in Verona I discussed this topic with some of > you and it was suggested to me to send an email here. > > Here is an example to describe my problem. Imagine a simple > DTO like this: > > class MyDTO{ > public ?int $propA; > public ?int $propB; > } > > Imagine that a Form processor or a generic mapper fill some of > these fields with a null value: > > $dto = new MyDTO(); > $dto->propA = null; > > Sometimes we use DTOs to handle PATCH requests and not all > the properties are mapped with a value. In a scenario like this, > "null" is often a valid value. > > At this point, I need a way to find if a property was initialized or > not but unfortunately "isset" is not a solution. When I write: > > echo isset($dto->propA) ? 'init' : 'not-init'; > > I get "not-init" since isset returns true only if the variable is set > and different from null. > > Full example: https://3v4l.org/4cCj0 > > Is the language missing a clean way to check if a property is > initialized or not? Is there any solution to this problem? > > The only alternative is using reflection but I need to pass the > property name as a string losing static analysis. > Proposing a new language syntax like > "is_initialized($dto->propA)" can be an interesting solution? > > Thank you in advance. This is a gap in the current API, and I've run into it myself. What I figured out was: array_key_exists($prop, get_object_vars($obj)) Because get_object_vars() omits uninitialized values. I would absolutely like a better alternative. --Larry Garfield