Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98343 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 12018 invoked from network); 23 Feb 2017 07:23:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Feb 2017 07:23:06 -0000 Authentication-Results: pb1.pair.com header.from=narf@devilix.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=narf@devilix.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain devilix.net designates 209.85.218.43 as permitted sender) X-PHP-List-Original-Sender: narf@devilix.net X-Host-Fingerprint: 209.85.218.43 mail-oi0-f43.google.com Received: from [209.85.218.43] ([209.85.218.43:36239] helo=mail-oi0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 45/5A-11648-ADD8EA85 for ; Thu, 23 Feb 2017 02:23:06 -0500 Received: by mail-oi0-f43.google.com with SMTP id s205so13550425oif.3 for ; Wed, 22 Feb 2017 23:23:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devilix.net; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=4lbMwlSooBNWqxNYPunuHfCA1mqQ6+1bZo5Azal7iBc=; b=ONLkPFje11CwylSRXIix3obgiJ8rQHe3ccrW1XfUpw3wqqEf+q7JZQxH9EFbwR5wEa pBulkMQGTaga3Pg1aC2k3Ln9DH2HiegW9rJNzd3/G5IgxSVkTSeG0Ry+/VY7OF/eNh7o DU4uS65xvp3uOQn3C4l+hA4glFUhE5NbITgNw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=4lbMwlSooBNWqxNYPunuHfCA1mqQ6+1bZo5Azal7iBc=; b=IhOpo67CmVfPHIBoKY6hIl3BgGfDD3IZAL+RNk9FGEM/iFoq9uAphLdUWorLDd6mzB q4EjKCWHozO1CcFxmti4BWLh7wKuXwQBkeZCarHUDY9t4Z0/4uxIAiLLQhHegjdHuKZh RiCJGn13ufuDWDmKglQdZZUAV+eQvOEybYHuAhQ3Da74mUuKhzRtYNQxFbSgPuDXAtdj xpyPGxI0Xjw5+aJ+0BT4u3N3C8Qyg+y3MEO5PHmz7uT/D4oVZ+NqYIAjPleiLei+9sEw 6x+KfBDU3clSPJD8sXW6ZCAh6Z1lkLlM866UYNUqmT7GiqghGAUO+Tn9rjAFmZ6DMLeB hxhA== X-Gm-Message-State: AMke39nTmKfc/DTNLDB/gNOYAmFOD9wPYhW7pStklvoILoLT4BjBZFliOhyFsLjvqeAl/KGsIsyQAL6bmdYHsA== X-Received: by 10.202.180.8 with SMTP id d8mr14406994oif.26.1487834583300; Wed, 22 Feb 2017 23:23:03 -0800 (PST) MIME-Version: 1.0 Received: by 10.182.138.72 with HTTP; Wed, 22 Feb 2017 23:23:02 -0800 (PST) In-Reply-To: References: Date: Thu, 23 Feb 2017 09:23:02 +0200 Message-ID: To: =?UTF-8?Q?Micha=C5=82?= Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a113cc2a2d2955905492d7b7d Subject: Re: [PHP-DEV] Nullable types and strict type-hinting with default null value From: narf@devilix.net (Andrey Andreev) --001a113cc2a2d2955905492d7b7d Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, On Wed, Feb 22, 2017 at 11:18 PM, Micha=C5=82 wrote: > 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 final= ly > 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 i= s > 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=3Dnull) // incorrect type, should throw error > a(string $test=3D'test') // valid type, ok > a(?string $test=3Dnull) // valid type, ok > a(?string $test=3D'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? > This isn't just a PHP 7 work-around for null types, it has existed since object type hints were introduced and is very widespread. Cheers, Andrey. --001a113cc2a2d2955905492d7b7d--