Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114624 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15875 invoked from network); 26 May 2021 19:12:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 May 2021 19:12:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CA9AD1804C3 for ; Wed, 26 May 2021 12:24:20 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-io1-f45.google.com (mail-io1-f45.google.com [209.85.166.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 26 May 2021 12:24:20 -0700 (PDT) Received: by mail-io1-f45.google.com with SMTP id a6so2275978ioe.0 for ; Wed, 26 May 2021 12:24:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0b42EfnUS7oqTepKIqv/6DFA27mm4ZLvQN+6+0jt5xQ=; b=sQH+uAHNJMoPo7raQfaIdpPRYRseGk81nWX+xFjVY+L1RcKDLLiVYVCat0EOTcBbep YJQxavD525TuAFOXPBW0MCU9Qpe33ANlf1HB9B+Y32IQhZds9kkPycS3+H0sJ5mfeaw7 2RzRu1MGejQgzo4lfySmFwkGUMYpN2jWR4VfmaKhcg+NNgM22e8oOQWJTAgL89Yag6mV IifxX4+i3ycRbrLwFYIigx2/szRulG1/gizr/nKyu7yViCU2tQjb98bXOAqyXmdFC9tg /fzblbq1cnKNpCyOxL0Y2GQv/PdfDbixk7SFmXy3y5LwzuiyNCqbmNn5QyCewocttIqW CAxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0b42EfnUS7oqTepKIqv/6DFA27mm4ZLvQN+6+0jt5xQ=; b=aP0emxON5MqRVF5frRfsm1N4uE+kXqJ5VO3wjoKAzmLt7DKB6ixpowns6ZKUoGPGNX WWBdX4+k2shipD8ZLYL2d6AwV9bJAAetOn/61HFu23aM3yLGyZq3Z0HUYi5196N4d0jD hrUIeCMg1gYl/JFVZVy0eVTg6R+TURj4vWtNKFR/EWggJllIO60cCVc9WK0IKlYTcJut XQRsFFLR3X9FjTQVIm3hW4K8Vwni0y/pwjse+IjSBzzPNi03ZQzb7cA/wRuP0FO8Vg64 6mbDiQ8H27Z8BJZqqkx5PDj+Vu+u5al4Hp7WPK2/XyTXUVvXtsUi7RIQw0If5xCylZkZ CLJw== X-Gm-Message-State: AOAM533UAXy1pKh/5psMZOrB+xEmPVWLYG1IgIRvEMH2muD8BYbV2Af2 5xgPlXwDu1YTulm5wxMgWS6rBgtGXlQ5i93kPOk= X-Google-Smtp-Source: ABdhPJwNVhdp03uKoxwo29q3tsIW084AfpeO0ceLtb4Dzg9vQk2gF0BxbgP9U4G465CzZjbj/89v8y2xi6BzvfOjlt8= X-Received: by 2002:a02:a316:: with SMTP id q22mr4622290jai.26.1622057058688; Wed, 26 May 2021 12:24:18 -0700 (PDT) MIME-Version: 1.0 References: <80fb5a8e-d770-49fd-45d5-ea6f4b004513@gmail.com> <2874cf7c-e55d-4100-bb01-af11a5ab6e87@www.fastmail.com> In-Reply-To: Date: Wed, 26 May 2021 21:24:07 +0200 Message-ID: To: Guilliam Xavier Cc: Larry Garfield , php internals Content-Type: multipart/alternative; boundary="000000000000c9e4bb05c34096e9" Subject: Re: [PHP-DEV] Consensus Gathering: is_initialized From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?=) --000000000000c9e4bb05c34096e9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable =C5=9Br., 26 maj 2021 o 18:20 Guilliam Xavier napisa=C5=82(a): > On Wed, May 26, 2021 at 4:09 PM Larry Garfield > wrote: > > > On Wed, May 26, 2021, at 8:24 AM, Rowan Tommins wrote: > > > On 26/05/2021 11:13, Joe Watkins wrote: > > > > Hi internals, > > > > > > > > In response to: https://bugs.php.net/bug.php?id=3D78480 > > > > > > > > I implemented: https://github.com/php/php-src/pull/7029 > > > > > > > > > My general feeling remains that the "uninitialized" state is an awkwa= rd > > > hack that we should be working to eliminate with better constructor > > > semantics. A variable that remains uninitialised after the constructo= r > > > almost always indicates a bug in the constructor, not a state that th= e > > > rest of the application should care about. > > > > I am inclined to agree here. What I don't know about is the cases note= d > > in the bug, such as GraphQL or other serialization cases where "null" a= nd > > "absent" are not quite the same thing. That is probably sufficiently > > edge-case to not deal with directly, especially when the more verbose > > alternative still exists, but that's the only reason I'd even consider > > making uninitialized something other than "your constructor is bad and > you > > should feel bad." > > > > I think you said the word: serialization. And especially *deserialization= *, > e.g. from a JSON payload into a typed DTO *without* calling the construct= or > (then the DTO is passed through validation, which must handle uninitializ= ed > typed properties "gracefully"). > I don't think nowadays anyone does that without a kind of deserializer which reads the metadata of desired DTO and like Symfony's Serializer or JMS which just deal with such tasks!? Not using ctor even for manual construction would be just a waste of time writing validation etc. Consider JSON decoded into: $arr =3D ['name' =3D> 'main_window','width' =3D> 500,'height' =3D> 500]; class Window { public function __construct( public string $title, public string $name, public int $width, public int $height, public ?bool $unknown =3D null, ...$additional, ) {} } var_dump(new Window(...$arr)); That example errors, since the title property is not given in decoded payload. The behaviour is what we expect here since we require it. The solution is to pass a valid payload with the title. Additionally, you can see "unknown" was also missing but it was initialized with default null value since we don't require that. If you add nullability into the title as follows: class Window { public function __construct( public ?string $title =3D null, public string $name, public int $width, public int $height, public ?bool $unknown =3D null, ...$additional, ) {} } Then is passing built-in language sort of validation. If you wanna allow passing more values than expected, add not used variadic argument at the end. Variadic argument solves the issue with missing named arguments which might appear in the future. These examples show a very simple solution to container classes such as DTO= . All of them avoid dealing with an uninitialized state. IMHO there really is no need to deal with uninitialized properties in DTO! Cheers, Micha=C5=82 Marcin Brzuchalski --000000000000c9e4bb05c34096e9--