Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119484 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 35603 invoked from network); 8 Feb 2023 14:22:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Feb 2023 14:22:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A791118033A for ; Wed, 8 Feb 2023 06:22:22 -0800 (PST) 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, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f46.google.com (mail-ed1-f46.google.com [209.85.208.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 8 Feb 2023 06:22:22 -0800 (PST) Received: by mail-ed1-f46.google.com with SMTP id a10so13591972edu.9 for ; Wed, 08 Feb 2023 06:22:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=QrobRlkgs23i7cIe4a+yQpZvjwFPHG7f5Q0B3hp1YUA=; b=T5oE2jFD0GiTx7j8L8tPxBHGwxowCYiNMBAJixmKT1jkjz5BxcjIUo69bFqTVuB43q ixrbO5lUFYhWg8GdZ5nOXCrf8sQleaK7AR4V/BxNbIcx3bnZkxqTQ1vkpoJJ6eiiA3im cqaDFiCzjqLq5jIkZ2VJSBb4bIUs950b9wEh/3NE4PHkh4pr6q4thbRHTQ7I/EUvCZPs s2I4T2c8ZEbIL1hqD4Wv/A2eCcdB/Xi29KTiuxvvwhA/W3jtQAFfew0Nj3PfysGAxWzc FRZK8FW1H1iDkBIC8FMTKmCd5VioC4dA2hnWy/brG7ZEdsDQwnfG5a+iJ0kuqaHmw2Cp 7gJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QrobRlkgs23i7cIe4a+yQpZvjwFPHG7f5Q0B3hp1YUA=; b=8FAHsWupOcEVHwdYMvaUpCbdEZ8W/pwPdsB+pWwpt0jE1aqNqgoJyqe20/YHZ5v05F 07hznuy2qYz9FXOsZ8M0oeHpfz6KxycnlyFWdVIKuAx32yPFxukAtUFt6W+3s52wqFwN mRsZ5acZxuzch/XuFAlblQwtzie/W5HcEhFyjozK4aMn4zmLlf/BgbJREnyhlMIfBAYl lF8FiX4MwqYEpmeLGUNlGSFxxsujgRFWm4SLvPudSbYS6o8biuiSH9KaSHeCsuOvA6YX yiBCp9d7KaLq16mT6HRZiMMwF6sBnIpJhCR88L/J8tcXF/hHJuLs4i8TXNqBqIlEwTdz RIMA== X-Gm-Message-State: AO0yUKWeheYUZZqfSrRA+uBaEyD4gMeKvpn3ZrB5FAQBAshX1LVqxeml cDaZLqBBLgGubK2ltz5PsEfLcU8Hi0ABwTVXFeRP9zh7bZg= X-Google-Smtp-Source: AK7set++SIQB2fd0g/PBu744JKj/1v1NFcbH7tpdvNmFO9KZhC9hLPgY24d3ArR0EJsRXxbslC1M4y+v5/b8YxxJFZM= X-Received: by 2002:a50:d5d3:0:b0:4aa:a4ff:a79a with SMTP id g19-20020a50d5d3000000b004aaa4ffa79amr1832178edj.22.1675866140427; Wed, 08 Feb 2023 06:22:20 -0800 (PST) MIME-Version: 1.0 Date: Wed, 8 Feb 2023 15:22:08 +0100 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000fdca8705f430fd64" Subject: 'Uninitialized' creates a lot of cluttering From: flexjoly@gmail.com (Lydia de Jongh) --000000000000fdca8705f430fd64 Content-Type: text/plain; charset="UTF-8" Hi, After posting this in the issues list , I was asked to mail it here. This is my first time here, so I hope I do it the right way. ------------------------------- At last I have the opportunity to start using php8, I am very happy with it and how mature it has become; implementing all kinds of code quality. But this new *uninitialized state* is really a pain. When you set a property in a class, I consider it as set into the class; it is part of that class, that object. Whether it is undefined or not. You could access it with an easy if or ternary operator. And you got a *clean overview* of all your properties and how they are initialized. class Test { protected string $name; protected string $type = 'mytype'; protected ?array $arrLog; protected ?string $optionalVar; protected string $table = 'category'; protected bool $isOk = false; } *This looks clean and gives clear, easy insight* to the purpose of each property. But since php8, you have to use isset() or empty() to check whether a property is initialized. Also php is offering more and more shorthand alternatives like nullsafe operator, ternary operator etc. Which I was not in favor of, but it seems to be the standard nowadays, so I let phpstorm do its thing and use the shorthands. But when you start using the (very necessary!!!) typed properties this is not working anymore unless you 'initialize' the property with at least an empty or dummy value or null. I read the RFC trying to understand..... Maybe it is technically difficult or it is against high-standard rules (which I love!), but this is really unpractical and gives to much overhead and clutter. I love good quality in coding, I endured years of being called crazy by colleagues who learned php by google and stackoverflow and loved it because it allowed all the bad coding. (Good is good enough) But now it seems to go over the top, making php less practical. For this uninitialized-thing we can choose one the following solutions at the moment: - leave properties untyped, which is really bad - set many properties to null or other empty values, which clutters your property list - add those empty values inside the constructor, which creates useless extra lines of code - use isset or empty to check if it is set or not, and not be able to use shorthands To use typed properties and profit from all the new shorthands and nice new feature of php 8, my class has to be something like: class Test { protected string $name = ''; protected string $type = 'mytype'; protected ?array $arrLog = []; protected ?string $optionalVar = null; protected string $table = 'category'; protected bool $isOk = false; } This looks cluttered. And I do not understand that we get all those nice shorthands, but for typed properties, we need to type more.... Why does a nullable type not automaticly default to null, or even to the empty variant of the type (by giving it not an question-mark but the exclamation mark, maybe...) like: class Test { protected !string $name; // defaults to '' protected string $type = 'mytype'; protected !array $arrLog; // defaults to [] protected ?string $optionalVar; // defaults to null protected string $table = 'category'; protected !bool $isOk; // defaults to false protected !float $someNumber; // default to 0 protected ?myObject $oObject; // no default for objects... to complicated and risky, or not??, so only nullable } I hope this can be considered again. Greetz, flexJoly (Lydia de Jongh) https://www.linkedin.com/in/flexjoly/ https://divaeloper.wordpress.com/ --000000000000fdca8705f430fd64--