Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88275 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 82797 invoked from network); 17 Sep 2015 00:14:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Sep 2015 00:14:14 -0000 Authentication-Results: pb1.pair.com header.from=mails@thomasbley.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=mails@thomasbley.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain thomasbley.de from 85.13.137.24 cause and error) X-PHP-List-Original-Sender: mails@thomasbley.de X-Host-Fingerprint: 85.13.137.24 dd15934.kasserver.com Received: from [85.13.137.24] ([85.13.137.24:54561] helo=dd15934.kasserver.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 84/08-41443-4D50AF55 for ; Wed, 16 Sep 2015 20:14:13 -0400 Received: from dd15934.kasserver.com (dd0802.kasserver.com [85.13.143.1]) by dd15934.kasserver.com (Postfix) with ESMTPSA id C02C526077F; Thu, 17 Sep 2015 02:14:09 +0200 (CEST) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-SenderIP: 95.90.238.173 User-Agent: ALL-INKL Webmail 2.11 In-Reply-To: References: To: internals@lists.php.net, yohgaki@ohgaki.net Message-ID: <20150917001409.C02C526077F@dd15934.kasserver.com> Date: Thu, 17 Sep 2015 02:14:09 +0200 (CEST) Subject: Re: [PHP-DEV] Make strict mode more strict? From: mails@thomasbley.de ("Thomas Bley") Yasuo Ohgaki wrote on 17.09.2015 00:10: > Hi all, > > PHP 7 has strict_types mode for function parameters/return values and > these are binded to certain type strictly. > https://wiki.php.net/rfc/scalar_type_hints_v5 > > Why not make strict_types mode more strict? > The idea is as follows: > > declare(strict_types=1); > > function foo(int &$i) { > $i = "string"; // Raise error > $i = function_returns_string(); // Raise error > $i = 1234.5678; // Raise error > $i = function_returns_float(); // Raise error > > $n = 123; > // do something > $n = array(); // Raise error > } > ?> > > Assigning different type to already initialized variable is a bug most > likely. There may be cases that variable should have several types, > e.g. return INT for success and FALSE for failure, but programmers can > use different variable or return value directly or make BOOL/NULL > exception. > > This is useful with reference especially. For example, > > declare(strict_types=1); > > function foo(int &$i) { > $i = 'string'; > } > > $i = 0; > foo($i); > var_dump($i); > ?> > > outputs > > string(6) "string" > > > > Just an idea. Any comments? > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net Hey, I think there is a lot of code out there that changes types, but is not a bug, e.g.: $params = array('a', 'b'); $params = json_encode($params); So I would extend the declare parameters: declare(strict_types=1, strict_declare=1); function foo(int &$i) { $i = "string"; // Raise error ... Regards Thomas