Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75412 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57840 invoked from network); 13 Jul 2014 15:10:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2014 15:10:41 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.178 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.178 mail-we0-f178.google.com Received: from [74.125.82.178] ([74.125.82.178:61027] helo=mail-we0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 25/18-16748-071A2C35 for ; Sun, 13 Jul 2014 11:10:40 -0400 Received: by mail-we0-f178.google.com with SMTP id w61so2135227wes.23 for ; Sun, 13 Jul 2014 08:10:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=ajSB9Wsya4Th9YZWgY9xuHkpa/oGGbpp3H5KlDDnp9M=; b=eAtcKl38CsodH6EyCJf5gsI8nrEbj00ECdi76nsziocdWlDYVp6NjxSzd+SID7Ojul w3v1Q0X7GXY14zCrD1g8+q52b77vv7iR09D9cBHo2dUVuQ13Ap0Z76g0m+UHRmxaVFxt 8beb3AeQlaZ6oc3SsmmBCslN1XuNZuR50s7AGsxki9sEkbrCz1t6MHmMJf9Ns8uF+1RE F2L9EQSCQ6DEfEo4o3Vpiaipx0yusY8+QJPDSMVjrT3KFeKm07vFimKMg0j1G6kkXv00 ZkglyIpocdh+8sDggi02SYgG3ty5Juglo4KdUMnBmrixD3Oj3rxzJLKiS6FGqYdKN0aW XoTQ== X-Received: by 10.194.9.198 with SMTP id c6mr739928wjb.131.1405264237226; Sun, 13 Jul 2014 08:10:37 -0700 (PDT) Received: from [192.168.0.2] (cpc19-brig17-2-0-cust25.3-3.cable.virginm.net. [81.101.201.26]) by mx.google.com with ESMTPSA id w10sm19773108wie.22.2014.07.13.08.10.36 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 13 Jul 2014 08:10:36 -0700 (PDT) Message-ID: <53C2A169.2080501@gmail.com> Date: Sun, 13 Jul 2014 16:10:33 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: internals@lists.php.net References: <08503591-EFC8-48E6-984E-FFC292C5EA5F@ajf.me> <027E65EF-C4FC-474C-92BB-D99EFADDEEED@ajf.me> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: rowan.collins@gmail.com (Rowan Collins) On 13/07/2014 15:41, Zeev Suraski wrote: > In other words, if someone types in '12a' > (by mistake or intentionally), I think it's a very sane behavior to just > treat it as 12, as opposed to forcing me (the developer) to write error > handling code with a try/catch block, and a fairly hairy try/catch block > too. Sure, but what if they type 'abc'? You *might* want that to be cast as 0 (not for an age input, obviously, but perhaps a "minimum price" field), so you could argue for *always* casting, rather than erroring. On the other hand, you might well want to default to some other value, which would require custom handling somewhere. If you want to allow for people *accidentally* adding letters, why accept '12a' but not 'a12'? If you want a "best guess", surely both should come out as 12 (whereas an integer cast would give you 0 for the latter). If you want validation, both are equally invalid. To my mind, the more values are let through with a bit of hand-waving, the less useful the check is. The worst culprit for this is is_numeric(), which sounds like a useful general-purpose function, but will happily accept exotic numbers like '-1e-10', and thus is almost never what you want. > Your example of "12 " (that I didn't even think about before) is an > even a more likely scenario, but I think there's no strong reason not to > allow both. I disagree, and think there's a stronger argument to be made for handling whitespace as a special case. Notably, int casts already do so - (int)' 12' (leading space) is 12, but (int)'a12' (leading non-digit) is 0 (no such special case is needed for trailing whitespace, because any trailing characters are ignored.) Regards, -- Rowan Collins [IMSoP]