Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102850 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 94441 invoked from network); 16 Jul 2018 13:28:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Jul 2018 13:28:58 -0000 Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.68 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 74.125.82.68 mail-wm0-f68.google.com Received: from [74.125.82.68] ([74.125.82.68:34785] helo=mail-wm0-f68.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DC/BB-39793-89D9C4B5 for ; Mon, 16 Jul 2018 09:28:58 -0400 Received: by mail-wm0-f68.google.com with SMTP id s13-v6so10036168wmc.1 for ; Mon, 16 Jul 2018 06:28:56 -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=jAqiKyt2NTkBPKFhQPnQccWpXeaM8lEwaqmpc+yHFzY=; b=ByzbNUw4BbStRENlhS6RLSxmEJ1O9+W5EXBcrrTrC2yINTkm3epdZLMKldcW7IxR+i nBNoWOEXL+2R6HMmfDe3ITyuIHLzgxc2hwBy+ZEJq5hlBwDji8HlvaEqHUdK6ZUlb59S bcWtMVDV3RsH4IQ8ZY55dTjnzHG4vo8gGi4UM07LoY+MKeaOA1hF5RwD5dk9vacY3+IT XbAeTzTCRjUXT3I0/4z1h8Br9VGESxh0hTwvCRzn2APgGytRauL3TpWeIxYa8K605zuC Cyb1xjNtExKCYq3aUb5fbk8zwBA4pXXwgN78oVhtOM5BjRKUkuDA+0F6fsodk2SY1efE zgFw== 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=jAqiKyt2NTkBPKFhQPnQccWpXeaM8lEwaqmpc+yHFzY=; b=YfkFTvCfT7JqUpEV3ZeAXMIqXC06xwivChdR6Y6AMkzlXAjY75WP9n5HFTC6kfiBQz vfxgBdUBrwhhW9ZlZ+IIhZkhlMwxRY+FrB9WdD2o4UBAkWmHTF91uLJPtycqjsyRz/Eu Nzy3fTZ13mCq+wCWvzt6Rd/GDTISwdkxZz5ndE5IU0aGlryiPKsEj4GGg+VT0vjKlaIt ZR5dBzKyI4PQo1CqsMSm6cqqnhL9NcpZF/OeIcOEHy5ciVsze3tVqVSMhY48e6mLiaun VYZIVBu6Ru0ne2TVCKZg+kMuIOfbkCiKQlyEILyP9mX6CipKI7hWq3AN/7PW2JEMwGRP aPbg== X-Gm-Message-State: AOUpUlH/Cvy9HSEmFqTkBx+G4xTwbqqOxGxfybPvWodasqQFIZ2UpwBM /ZkjR7ym+WxZjRd7nM5Lya47ipPOvLoT1iCWNk4= X-Google-Smtp-Source: AAOMgpeeybTJxzHZyaW5h62QVmpGpM5TZG37zjQa5kmUdlACMAYVsaNyxZTNhBNrrXubiWEHBVmZLwpm2PNaYADPOqA= X-Received: by 2002:a1c:be13:: with SMTP id o19-v6mr9216377wmf.1.1531747733995; Mon, 16 Jul 2018 06:28:53 -0700 (PDT) MIME-Version: 1.0 References: <8916EC21-D368-40F8-9ABD-CE0C04A73539@gmail.com> <0AA3251E-A0D8-492E-AEAC-3B2FBBFDB944@gmail.com> In-Reply-To: Date: Mon, 16 Jul 2018 15:28:41 +0200 Message-ID: To: Rowan Collins Cc: Dan Ackroyd , PHP Internals List Content-Type: multipart/alternative; boundary="00000000000092044e05711dcfb2" Subject: Re: [PHP-DEV] Non-nullable properties From: ocramius@gmail.com (Marco Pivetta) --00000000000092044e05711dcfb2 Content-Type: text/plain; charset="UTF-8" These don't really need explicit tests in most cases, but rather static analysis (currently happening via docblocks). Static analysis tools like vimeo/psalm already pick this up. I'd even be happy to get type hints that only have effect on `ReflectionProperty#getType()` as a massive improvement over the current situation we've monkey-patched us into, but the patch is indeed consistent with PHP's current state. On Mon, 16 Jul 2018, 15:24 Rowan Collins, wrote: > On 16 July 2018 13:00:57 BST, Dan Ackroyd wrote: > >On 16 July 2018 at 09:43, Rowan Collins > >wrote: > > > >> There's no contradiction here; throwing an error when a > >> property is *read* is not the same as enforcing that it always has a > >valid value. > > > >That's true, but claiming the RFC just 'trusts' the users to > >initialise them is a miss-representation of the RFC to me. The RFC > >catches the mistake if the programmer fails to initialise the variable > >then reads from it. > > Perhaps a better wording is that it requires one user to trust another > user: the mistake that leads to an unitialized property is the > responsibility of the author of the class, but the error only appears when > the object is used, which could be in a completely different library. > > > > >> If not, the object created would not be fully initialised, > > > >This is always going to be the case, unless you're proposing to > >deprecate ReflectionClass::newInstanceWithoutConstructor .... which is > >going to break a huge number of applications. > > It only needs to be banned (or modified) *for classes which claim their > properties are non-nullable*. Or, don't introduce non-nullable properties, > because they can't actually be enforced. > > > > > For me > >it is the equivalent of this: > > > >function foo() : int { > > return "five"; > >} > > > >This gives an error when the code is run, not when the code is > >compiled > > It's not about run-time versus compile-time, it's about when the error is > raised. In this case, it's raised when the return statement is executed. > The equivalent to the proposed behaviour would be this: > > function foo() : int { > return "five"; > } > $foo = foo(); // runs OK, but marks the variable as invalid > // ... Many lines of code later > // $foo has not been touched so should be an integer > $bar = $foo + 1; // Error! Sorry, somebody messed up a return value > somewhere, it's now up to you to figure out where > > > > >That would only be a problem in production if you don't run that code > >at all in development or in test.... > > If you are going to write tests to see if all your variables are being > initialized correctly, then you don't need non-nullable type hints, you can > just assertNotNull on each property. > > But again, the user writing those tests is not the user relying on them. > You are asking users of a library to trust that the library has appropriate > tests. > > Regards, > -- > Rowan Collins > [IMSoP] --00000000000092044e05711dcfb2--