Re: Typed properties patch

From: Date: Thu, 07 Apr 2016 18:37:49 +0000
Subject: Re: Typed properties patch
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-92142@lists.php.net to get a copy of this message
On 4/7/2016 8:21 PM, Dmitry Stogov wrote: > Today, PHP works with arguments in this way. > If we decide to enable nullable "?" syntax and/or "union types" we will > have to use them for properties as well. > but for now, we don't have anything accepted yet, and we are going to > push both RFC on next week. > I know, I just wanted to clarify it. >> >> class StupidExample { >> private int? $x = 42; >> >> public function setX(int? $x = 42) { >> $this->x = $x; >> } >> } >> >> $o = new StupidExample(); >> $o->setX(null); >> >> Note how the nullable (union null) type is much more useful than the >> current implementation in PHP. > > I don't see a lot of use cases, but it's not a problem to implement this. > Actually the implementation was proposed more than a year ago. > > https://wiki.php.net/rfc/nullable_types > I also don't see many use cases but it makes the intention of a program clearer and it makes it easier for a compiler to reason. Simply because the assignment *int $x = null* is ambiguous since *null* is not an *int*. The type declaration should always explicitly state legal values so that they can be validated everywhere at all times. -- Richard "Fleshgrinder" Fussenegger

Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
« previous php.internals (#92142) next »