Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93509 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91075 invoked from network); 25 May 2016 15:49:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 May 2016 15:49:20 -0000 X-Host-Fingerprint: 137.50.159.218 oa-edu-159-218.wireless.abdn.ac.uk Received: from [137.50.159.218] ([137.50.159.218:24830] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7B/12-14311-F79C5475 for ; Wed, 25 May 2016 11:49:20 -0400 Message-ID: <7B.12.14311.F79C5475@pb1.pair.com> To: internals@lists.php.net References: Date: Wed, 25 May 2016 16:49:15 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:43.0) Gecko/20100101 Firefox/43.0 SeaMonkey/2.40 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 137.50.159.218 Subject: Re: [PHP-DEV] [RFC][Vote] Typed Properties From: ajf@ajf.me (Andrea Faulds) Hi Niklas, Niklas Keller wrote: > > I disagree here. Properties are always null by default. The current patch > disallows > access to uninitialized variables only if they're not nullable, since null > isn't a valid value then. > > I don't think having to explicitly set them to null is the think we want. > And it's not what I'd expect given the current system. PHP's existing untyped properties are implicitly initialised to null, and so yes, we would essentially only be copying our existing behaviour. However, I think it is worth asking whether our existing behaviour is useful before we preserve it here. From my perspective, it is unhelpful that PHP does not warn you about using properties you haven't initialised, and this applies to both typed and untyped properties. In some cases (like in my linked list example in a previous email), null might be a meaningful value and worth distinguishing from a property not having being initialised yet. We can't change the behaviour of our existing untyped properties (or at least, that's beyond the scope of this RFC), but we could choose the behaviour we find more useful for our typed properties. Consider that we did something like this for parameters. Not providing an untyped parameter gives an E_WARNING: $ php -r 'function foo($a) {} foo();' PHP Warning: Missing argument 1 for foo(), called in Command line code on line 1 and defined in Command line code on line 1 But not providing a typed parameter gives a TypeError: $ php -r 'function foo(int $a) {} foo();' PHP Fatal error: Uncaught TypeError: Argument 1 passed to foo() must be of the type integer, none given, called in Command line code on line 1 and defined in Command line code:1 Stack trace: #0 Command line code(1): foo() #1 {main} thrown in Command line code on line 1 So there's a precedent for applying stricter rules when type declarations are used. Thanks! -- Andrea Faulds https://ajf.me/