Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123338 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 47D891A009C for ; Fri, 17 May 2024 21:40:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1715982086; bh=6bhcDzjwnWqrX5MrleXdi8mTHj1g21VtYHw02ds6QYc=; h=From:Date:Subject:To:From; b=W134dRwgRdXJSYg8t+0fktglQ84QrizJ4DZhb9GVdUYfiLdmHFetQ05jZKjzhy/c8 wSy8Q/49bwohBKTvZJhhJbf8xEYvFq91KVNmxIhBrK4KuH5yKWvM7HfoyG7COgywR/ mb7cvVeMkCjKMf5JuzAb4cnBR9DciHjo4b4ZrmkEsFvcvTEMzRXyU3Nzjw3fpi+aBe wsPs7wBx395UkLR9j4KSc7jViH1twmIredKxhZjkDfvEmAjVqJKvVfJy2MueS92bzi eLvqwrEdO4dnQYu1G10UqeJDiMgDbWP1ix6rOVdomD5m2Y7eQR/tRDkIkuTmgGR/vr QdD+d3WPtxmew== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1F12318005F for ; Fri, 17 May 2024 21:41:26 +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-ed1-f44.google.com (mail-ed1-f44.google.com [209.85.208.44]) (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:41:25 +0000 (UTC) Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-572f6ee87c1so5024049a12.2 for ; Fri, 17 May 2024 14:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715982031; x=1716586831; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=x30d9Dx0lxTQMiuotYNMF+0ODWB7yJFc7qX4/dk+p6s=; b=Y8u2Lp679gnZM/VW+fJs7ilZ07+xfGFhPMJebVGxT2y+j71AqE9/nAjgoH/K2vSCxK itrRvsamniSg2p3yypvtxbC4CoRnNrQG4QdaMbrz/xyoAyiMgzJ3dIPMOfrvjQpe2KzS 3Zc+T5THz5qcK61m710RS5Q5t+0lm7SEJhtUYUCoEGZ0l3iGw2V16TZVL18bxhS9t8qv fzn7sqh9asS5eB9coRbPWblfdbLHIrXXkmwF+3HMcX0QF0pVWFWPQxZ19yuOr7CAIJgd n4fnqPzs5oF07R3XGK3n7JEk9PK0n2RtmLmFk20QiQ7vAPCXa++vpL14EYvAcSZ31BhI /fmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715982031; x=1716586831; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=x30d9Dx0lxTQMiuotYNMF+0ODWB7yJFc7qX4/dk+p6s=; b=LKb8f0VBQVR1mrDD5LPAUjWT2sr2SbjIgrwgWjG+kX9n+bDQ2tk6dBkxoFBvt2AEhF NEzuuD7yA/ihL64DuAZizwSabyq+YnPllOCFy0kuji3EhDvmKAUCfpDSwPnVvMMoahlT rRs9wQIocEb24BfbPjXZ50Zh81zvQ2KNXEmiQCYeq2IDTmwGflTemYS4WS+eQ7WC8NKs 9sNczvH3piVsVDChoOmIfCVNvjsEBSNRtywmyOXbFYX799j0upjMD3rAwL1NiDUFua06 sEY4MRHxW98RiAIkEYJjjKZNyGOzaMt0ne3PzYmohMpiES61jBwJCmfEIuD6CalLYK9b e7+w== X-Gm-Message-State: AOJu0Yx/NIbh7BTnCEhVnZWZmyLky9q/K7X7u2wFXw1HlvRMAxEBsV7m IfD8cu8pC6GNHMdOwWoDakW9IjHyIsKxdqL41269F8gXXwaR5b9CFgOTrI38kLzroAMomwjPuvc 3DINpr/n54339hKBGbJKqA5TE4/FiQiKI X-Google-Smtp-Source: AGHT+IFqGJSsxYyYeaj8Y0c3KyMkhdzN40Dr5oUA6BU5TvnEpjcKYjKRNCblzK3f9y+69NQUu7k9PQAE5TncHhyK6L8= X-Received: by 2002:a50:d502:0:b0:56e:42e0:e53c with SMTP id 4fb4d7f45d1cf-5734d67e866mr14784129a12.34.1715982030592; Fri, 17 May 2024 14:40:30 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Date: Fri, 17 May 2024 23:40:19 +0200 Message-ID: Subject: [PHP-DEV] [DISCUSSION] Checking uninitialized class properties To: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000005fe1a60618ad33fd" From: cardamoneluigi@gmail.com (Luigi Cardamone) --0000000000005fe1a60618ad33fd Content-Type: text/plain; charset="UTF-8" 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. Luigi Cardamone Backend developer Italy --0000000000005fe1a60618ad33fd Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello Internals,
during last PHPDay in Verona I discuss= ed this topic with some of
you and it was suggested to me to send an ema= il here.

Here is an example to describe my problem. Imagine a simple=
DTO like this:

class MyDTO{
=C2=A0 =C2=A0 public ?int $propA;=
=C2=A0 =C2=A0 public ?int $propB;
}

Imagine that a Form proce= ssor 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 initia= lized or
not but unfortunately "isset" is not a solution. When= I write:

echo isset($dto->propA) ? 'init' : 'not-ini= t';

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 langu= age 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 r= eflection 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 a= dvance.

Luigi Cardamone
Backend developer
Italy
--0000000000005fe1a60618ad33fd--