Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75402 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41798 invoked from network); 13 Jul 2014 14:29:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2014 14:29:58 -0000 Authentication-Results: pb1.pair.com header.from=zeev@zend.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=zeev@zend.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.220.179 as permitted sender) X-PHP-List-Original-Sender: zeev@zend.com X-Host-Fingerprint: 209.85.220.179 mail-vc0-f179.google.com Received: from [209.85.220.179] ([209.85.220.179:32845] helo=mail-vc0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 69/94-16748-5E792C35 for ; Sun, 13 Jul 2014 10:29:58 -0400 Received: by mail-vc0-f179.google.com with SMTP id id10so5360392vcb.38 for ; Sun, 13 Jul 2014 07:29:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:references:in-reply-to:mime-version :thread-index:date:message-id:subject:to:cc:content-type; bh=uxoZz+j54roCWd+TDY4NWZ6kpom8uSXxasOJ2dJkJuI=; b=dFHpgsoFTBAmn502xwTl17X1F0CXPWSLB1u7+Ppp9KcQE3yPGN7xCeUYco+pZdleDk PtomgEIcXvYIajCsmoyMTJujG5dP5NfuefPr2p+oOmSvd7VdGC4yyWgwHC/y00Gi7nmc cQfWCBDduY1dFUgwYxGybEgNXcbfU3Y3rUQZ8J/hcR/Oy4df6BfByXAGvR+GJHP+9UUT aeO+j5O26RXr4ouzFwsu9bh1Ntb0AKwCYyMPl0kc73ZDE0uGaxTajFC61lv3TqTaGS3t W/R3LAezsANex4TIbvb1gH1Rl+P6+sqpwJXWDQkDfBjoaIHZCa1Ol8RlSzJ3IEmMukpo 1BNg== X-Gm-Message-State: ALoCoQltNYELcQcEMX8bL9Ca0EiPQWwMmEUEsiIhiVxhvVVnfs+XT4daOZLRc3MOpmU7EZc3GtEdNwBL1y2mpf/t24Z2I7TR3biPXeDWuDrxB06+DJVwfpWl/3SuMBPQ5doU09yYJ6Iw X-Received: by 10.52.103.97 with SMTP id fv1mr243006vdb.42.1405261794928; Sun, 13 Jul 2014 07:29:54 -0700 (PDT) References: <08503591-EFC8-48E6-984E-FFC292C5EA5F@ajf.me> In-Reply-To: MIME-Version: 1.0 X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQJuUFVOlGiX172RfpkkW7aXwqSXwwOWY2SKmkP33FA= Date: Sun, 13 Jul 2014 17:29:54 +0300 Message-ID: To: Nikita Popov , Andrea Faulds Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Subject: RE: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: zeev@zend.com (Zeev Suraski) > I haven't yet closely reviewed the details of what is and isn't allowed, > but one > things I would strongly recommend against is introducing any "accept but > throw a notice" cases. Either a value is allowed or it isn't. > "12foo" to an int/float argument should throw a recoverable fatal error, > like > everything else. (I'd change that in zpp as well, but that's a different > discussion). The use case of converting string based values into integers and floating point numbers is a very common one, given anything that comes into PHP as input is a string. In terms of productivity, it's much easier for me to stick an 'int' type hint (or whatever we call it) on a piece of $_GET value that I know needs to be an integer, than it is for me to now surround it in a try/catch block and protect against a user or a wannabe hacker sending in '123abc' in there. In such a case treating this as 123 (int) makes perfect sense, with the erroneous input silently converted to something sane the code can live with. A key advantage of these casting type hints is saving the need to write lengthy and somewhat obscure pieces of code that just validate inputs. If we force developers to surround them with try/catch blocks, we lose at least half of the value here IMHO. I therefore think conversion from string to any of the basic types should never result in an exception even if information is lost; And it does make > The question of passing floats like 12.5 to an int param is somewhat > tricky. > One could argue that it's possible to end up with floats like > 12.0000000001, > which are nearly integers but not quite there, and a user might expect > them > to behave the same as the integer 12. On the other hand one can also argue > that it's easy to end up with the inverse, namely 11.999999999999. In this > case the end user might also expect it to be treated as integer 12, > however an > integer cast will truncate it to 11. > > The truncation behavior of the float to int cast is why I tend towards the > behavior the RFC currently proposes (forbid non-integral floats): It is > rather > unlikely that truncation is what was intended, as such an explicit > rounding call > is necessary anyways. I'm undecided on this one too. Zeev