Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81634 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 17836 invoked from network); 2 Feb 2015 19:19:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Feb 2015 19:19:07 -0000 Authentication-Results: pb1.pair.com header.from=j.boggiano@seld.be; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=j.boggiano@seld.be; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain seld.be designates 74.125.82.45 as permitted sender) X-PHP-List-Original-Sender: j.boggiano@seld.be X-Host-Fingerprint: 74.125.82.45 mail-wg0-f45.google.com Received: from [74.125.82.45] ([74.125.82.45:50181] helo=mail-wg0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2E/61-34915-8ADCFC45 for ; Mon, 02 Feb 2015 14:19:06 -0500 Received: by mail-wg0-f45.google.com with SMTP id x12so40608175wgg.4 for ; Mon, 02 Feb 2015 11:19:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=3Tp8mvVS9CR7ItCVtUlztypKiAF0WIgpGvGww25qd4s=; b=BGVEI/5c9Uhe1ifAUhg8yx06jnL7WALyGt7FGzCPVdp0+3Q4HVBpsuVLUapdI+ra72 BgVU+ngn4AcBhIEyVXsE4M1C8U3t54ZvRl18HPASj9UxA/1CUrznYk5OYmD+q8vbUILO u3rfA7++XLcq3AkHtmdr/+JPlw8QgxwvDSKPELHfo1oA412SKnDRCwlYGigPGIsfYnyg zn5Iy0NH42SLbCYMF4YJqqkmaZG0m8Hz9t4VQP4yaUfHyelDE+tJr5VpZVIJZMy3gGEB wYIPbL2HabOsvZRzm5A1AmFhfFA5BMZwtxmnuJu5VzzUEK/2QlRat4t+sA1WcW+CmM2D AZqQ== X-Gm-Message-State: ALoCoQnG8YzsjthtY00G7fSim48h9fUsg4EXS/+WhmD3fvpUEl0nsMGXFgoxZPdAiYfR3KAUm1A0 X-Received: by 10.180.92.199 with SMTP id co7mr27657220wib.47.1422904742461; Mon, 02 Feb 2015 11:19:02 -0800 (PST) Received: from [192.168.1.76] ([87.112.217.174]) by mx.google.com with ESMTPSA id 18sm29200705wjr.46.2015.02.02.11.19.00 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 02 Feb 2015 11:19:00 -0800 (PST) Message-ID: <54CFCDA8.8070301@seld.be> Date: Mon, 02 Feb 2015 19:19:04 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: internals@lists.php.net, Dmitry Stogov , Stas Malyshev References: <54CFC849.3080001@gmail.com> In-Reply-To: <54CFC849.3080001@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] What do we need strict scalar type hints for? From: j.boggiano@seld.be (Jordi Boggiano) On 02/02/2015 18:56, Stanislav Malyshev wrote: > Hi! > >> Keeping that in mind, the declare statement, as ugly as it may look, >> could be actually a killer to finally get what both camps want but >> never (and never will) manage to agree. > > I think having two conceptual frameworks in one language and having to > deal with (and potentially maintain) code bases with mixed conceptual > framework is a very bad idea. That would lead to a language > fragmentation as strict code will become hard to combine with non-strict > code within one application, and pretty soon people will start asking > questions like "is this library written in strict-PHP or non-strict-PHP? > ow, dang, we can't use it because we use the other one..." Stas, I respectfully think you misunderstand the RFC. I think one good way to make a parallel is to compare it to having error_reporting(0); or error_reporting(E_ALL); on top of every file. The RFC as it stands would make type hints be silently coerced by default, so it would have "error reporting" disabled by default, but if you set the declare line you do the equivalent of enabling error reporting but *only for your calls and returns*. Right now if you use a sloppy library for example that throws E_STRICT, and you have reporting enabled, you do have fragmentation and it sucks, for example: // strict.php include 'non-strict.php'; error_reporting(E_ALL); foo(); // non-strict.php error_reporting(0); function foo() { echo UNDEFINED_CONSTANT_FTW; } foo(); This produces: UNDEFINED_CONSTANT_FTW Notice: Use of undefined constant UNDEFINED_CONSTANT_FTW - assumed 'UNDEFINED_CONSTANT_FTW' in non-strict.php on line 5 UNDEFINED_CONSTANT_FTW In this case the strict file by using a non-strict function gets warnings thrown, so the community has to encourage/force everyone to behave strictly in libraries otherwise their code does not integrate well. On the other hand, with the type hints, every file chooses how they want to behave and since it is not an ini setting it can not be influenced by another file like in my example above. Consider this for example: // strict.php declare(strict_types=1); include 'non-strict.php'; foo(10); // non-strict.php include 'strict2.php'; function foo(string $output) { echo $output.PHP_EOL; } foo(5); strictFoo(5); // strict2.php declare(strict_types=1); function strictFoo(string $output) { echo $output.PHP_EOL; } This results in: 5 // output of foo from non-strict context 5 // output of strictFoo from non-strict context Catchable fatal error: foo() expects parameter 1 to be string, integer given // failure of foo from strict context So we see that the non-strict code works just fine on it's own, 5 gets coerced to string, no matter if it's calling strict libraries or non-strict ones, since it is itself non-strict. The strictness of others does not influence non-strict code and everyone integrates very well together. When strict code calls non-strict code, they get errors if they pass invalid params, but that is what they want since they asked to be strict, so they get full on error reporting/static analysis of their code, but do not push it on the whole ecosystem. Cheers -- Jordi Boggiano @seldaek - http://nelm.io/jordi