Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98342 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 9690 invoked from network); 23 Feb 2017 07:00:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Feb 2017 07:00:27 -0000 Received: from [127.0.0.1] ([127.0.0.1:17065]) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ECSTREAM id AD/F9-11648-B888EA85 for ; Thu, 23 Feb 2017 02:00:27 -0500 X-Host-Fingerprint: 89.231.48.169 host-89-231-48-169.dynamic.mm.pl Received: from [89.231.48.169] ([89.231.48.169:9322] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C6/E7-11648-B000EA85 for ; Wed, 22 Feb 2017 16:18:05 -0500 Message-ID: To: internals@lists.php.net X-Mozilla-News-Host: news://news.php.net:119 Date: Wed, 22 Feb 2017 22:18:00 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 89.231.48.169 Subject: Nullable types and strict type-hinting with default null value From: aaatoja@o2.pl (=?UTF-8?Q?Micha=c5=82?=) Hello PHP internals, Scalar types declarations were introduced in PHP 7.0 but it was not possible to pass null as a default value to function/method. It was finally done by a small workaround described in documentation as "The declaration can be made to accept NULL values if the default value of the parameter is set to NULL". In PHP 7.1 and it's new feature - nullable types - this hack is, in my opinion, completely unnecessary. It breaks valid type-hinting, is not logical for advanced programmers, and may cause potential bugs. According to docs "Type declarations allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.". Following this, first line of code should throw error - but it does not, because of 7.0 work-around. a(string $test=null) // incorrect type, should throw error a(string $test='test') // valid type, ok a(?string $test=null) // valid type, ok a(?string $test='test') // valid type, ok There is no reason, except of backwards compatibility, to keep this behaviour. It's too late to change this in 7.1, maybe even in 7.2. But, what is Your opinion? Should it be preserved or fixed?