Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91699 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14398 invoked from network); 16 Mar 2016 17:40:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Mar 2016 17:40:06 -0000 Authentication-Results: pb1.pair.com smtp.mail=bobwei9@hotmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bobwei9@hotmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain hotmail.com designates 65.55.111.108 as permitted sender) X-PHP-List-Original-Sender: bobwei9@hotmail.com X-Host-Fingerprint: 65.55.111.108 blu004-omc2s33.hotmail.com Received: from [65.55.111.108] ([65.55.111.108:59662] helo=BLU004-OMC2S33.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 09/19-48430-47A99E65 for ; Wed, 16 Mar 2016 12:40:05 -0500 Received: from BLU436-SMTP185 ([65.55.111.71]) by BLU004-OMC2S33.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.23008); Wed, 16 Mar 2016 10:40:02 -0700 X-TMN: [jX3SIVw48e/7+f9zNFQIqwn2kXlcIlQl] X-Originating-Email: [bobwei9@hotmail.com] Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_443B5983-BFA5-47FE-971A-940C04387DDB" MIME-Version: 1.0 (Mac OS X Mail 9.2 \(3112\)) In-Reply-To: Date: Wed, 16 Mar 2016 18:39:57 +0100 CC: Phil Sturgeon , "internals@lists.php.net" References: To: Chris Riley X-Mailer: Apple Mail (2.3112) X-OriginalArrivalTime: 16 Mar 2016 17:39:59.0599 (UTC) FILETIME=[DCCC27F0:01D17FAA] Subject: Re: [PHP-DEV] [RFC Discussion] Typed Properties From: bobwei9@hotmail.com (Bob Weinand) --Apple-Mail=_443B5983-BFA5-47FE-971A-940C04387DDB Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" > Am 16.03.2016 um 18:27 schrieb Chris Riley : > On 16 March 2016 at 17:17, Bob Weinand > wrote: > > Am 16.03.2016 um 17:36 schrieb Phil Sturgeon >: > > 2. This whole temporary nullability situation, where unset = properties > > will error on attempted usage if not set. Should they instead error > > after the constructor has been called if they are still not holding = a > > value? >=20 > Erroring when using the unset property is the way to go. >=20 > The example from the RFC is showing exactly why... >=20 > class Foo { > public int $foo; > } > $foo =3D new Foo(); > echo $foo->foo; // Patch currently errors here >=20 > With one property, not so much an issue, we could force a constructor. > But with 20 properties, definitely an issue. >=20 > If we'd error after constructor call, there are also other = complications=E2=80=A6 >=20 > class Foo { > public int $bar; > function __construct() { > global $foo; > $foo =3D $this; > } > } > try { > new Foo; > } catch (Error $e) {} > var_dump($foo->bar); // not set, but instance is referenced >=20 > Sure, we could find ways to circumvent that and change the class to = totally non-functional and making every usage of it throw an Error, but = it isn't quite elegant. >=20 > I'm not 100% convinced that we need this now we have full type hint = support for parameters and return types; but it does have potential to = remove boiler plate for public properties. For point 2 nullable = properties, how about some syntax which allows for a property to be = null, or for it to be a specific type. I'd suggest a null assignment to = fall in line with parameter type hints eg: >=20 > class Foo { > protected int $bar =3D null; //can be null > private stdClass $baz; //can't be null > } The point is basically, the value should never be accessed and actually = return null. If it is accessed, it always should return the type I provided. Having it potentially return null would completely miss the point (and = also confuse static analysis which would assume the value could be null, = when in reality it never should be). Concrete example: = https://github.com/amphp/aerys/blob/master/lib/InternalRequest.php = That's a bunch of public properties (value object). The values are all = initialized when the object is created =E2=80=94 if a property is = accessed and was forgotten to be initialized, it should error. That's = helpful for detecting bugs. You definitely don't want to define these values, which are supposed to = be set upon initialization, to be _possibly_ null. Because they actually = are never null from their first read access on. E.g. InternalRequest->responseWriter is *always* a Generator object when = accessed. The value is *never* potentially null (and thus would = theoretically have to be checked for being null or not). Having to force null here, would be a big mistake. Semantically and for = static analysis. Bob= --Apple-Mail=_443B5983-BFA5-47FE-971A-940C04387DDB--