Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123340 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 2E9AC1A009C for ; Fri, 17 May 2024 22:21:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1715984532; bh=XN7eGPHilbH6Bbverc9RmbkBlmjyDo3VrbpyQIKFTGU=; h=References:In-Reply-To:From:Date:Subject:To:From; b=W7BE4cRpINZmYfp5vtJeL072FvtYLERv8Y1/4ES+kbdxgr2w9UXbdiKQKNXPypHtF CLTk/cQA8L6gO61Dkwybw5DbA+YFlvtfuYS/5CCYLEto2FRVSOYZMhpueTP8s8boob ezKUu1V1kpTwsxvzVzZVsXXRSYBeS7HARaUlnePl46Hfb9D9zbkTiHcfs5htPqLrti lynKn9B/JANrVXskg3Wt3zm6o0sVqgqG0emgWfaisEv9JxcyUOawHM2D3FAsAizXR8 TTNwbpBoyCwcD2C6iOC89iwQ5yE4f0aJXtBZ6teBypVEe4lpibxD17jeoXXO3+blCb qYpKJgnOI2uWw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C5D8F18067A for ; Fri, 17 May 2024 22:22:11 +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, 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-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) (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 22:22:11 +0000 (UTC) Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-79305314956so74626485a.3 for ; Fri, 17 May 2024 15:21:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715984477; x=1716589277; darn=lists.php.net; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=GLy/t3Q1iPlIofURjo/zhgT1rFoGqky4hoHkGc56p2M=; b=U+JerHg40MWE2ZZmsE1VAkbyPCdhUX4vCB81zwWc5Vx/tqalm/0hkTw7XI6Pw0RtxG Dc4Vjx13hT6za9VGJ1UcmAKnzHoeOwwy0YhQOnvc7MJi7n6DaVII4Mtjn0ba5xayagvL VlY0utf9p9s62bRGDqbQxpYsroRYyxqOeDO+TaTp526fdHZfxGa/s2EnkPrZL46GAlvd DgMhiT916FrWiFuH0eusqP66bOJu3HVPT9agIb2NADmC2NKBn+S7BF7lXj6PUah3aDhA NTlWZPFe2vkGGYnUYlIPSWvghEeSAb5Xo8EvAnsDwq/JhelojudlOz+oY20CYGykg0md utzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715984477; x=1716589277; h=content-transfer-encoding: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=GLy/t3Q1iPlIofURjo/zhgT1rFoGqky4hoHkGc56p2M=; b=ajW20e6y2Eu+yQga/p5I618qWyF9nQrTICUOYFoSHFUioo4Zt+QHa9QIOYgStF1rH+ /B58Yi2UmLvQGhJVKcVMq2MGzTZQx4GDZNHdSPI0BkcuLm4zIUmmyN8ADJaMlb6OuSjh alAzSkc6m1wI+nvnIkx4506p2pXA53lalDwA1MZIMSFKWWj47GiJPRmJQs+E2wz05ovv Mlsdi8dvhL3daxhE3GBhCGBRhwk8NwT98cMFT2sHK8XCRKi2b8m+XdTdQwiwQSFHkAuI 2YJXtpAOXIsWlUps3FfzTVdE7V+FUZl16d6LV5W4yvfSlYAQYCDfiTZBZmyhuzzVcgSN r3rg== X-Gm-Message-State: AOJu0Yyy9NcyWF3PbZ4gptfphcv5L5UMsm7i5cuE1aWaF4OORLoTdlVp KnGms2hKAl7BgUA7GWXOPotXqFdi4olColAc4RppI5gkR+jHejq68aAb7SF7JMB9iViVgXpLsN4 fGEds816aByduIu5Yfj+zox2TiTeT4k6AS8kH0w== X-Google-Smtp-Source: AGHT+IFJVkvfALrGoKcpQvRxad6WU7Enag3rQxxR3Lv6/dF7Ck39jAbJwfEIJkpBrIdTY6JJzhwSwY6N62269hIIJng= X-Received: by 2002:a05:6214:440f:b0:6a0:8bc5:ae51 with SMTP id 6a1803df08f44-6a168167bb2mr267754816d6.18.1715984476559; Fri, 17 May 2024 15:21:16 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 18 May 2024 00:21:05 +0200 Message-ID: Subject: Re: [PHP-DEV] [DISCUSSION] Checking uninitialized class properties To: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Luigi On Fri, May 17, 2024 at 11:40=E2=80=AFPM Luigi Cardamone wrote: > > 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 =3D new MyDTO(); > $dto->propA =3D 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'; IMO, "uninitialized" should not be treated as a value. Code outside of the constructor should generally not have to deal with uninitialized properties. Instead, make sure the constructor leaves your object fully initialized, in this case likely by setting it to null. If you need some additional "undefined" state, I think that's better modeled in other ways, maybe by wrapping it in an object. ADTs [1] should help with that in the future. enum Age { case Unknown; case Unborn; case Years(int $years); } public Age $age; Or: enum Age { case Unborn; case Years(int $years); } public ?Age $age; Silly example, but you get the point. Ilija [1] https://wiki.php.net/rfc/tagged_unions