Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78223 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19933 invoked from network); 22 Oct 2014 08:37:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Oct 2014 08:37:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=bobwei9@hotmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bobwei9@hotmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain hotmail.com designates 65.55.111.157 as permitted sender) X-PHP-List-Original-Sender: bobwei9@hotmail.com X-Host-Fingerprint: 65.55.111.157 blu004-omc4s18.hotmail.com Received: from [65.55.111.157] ([65.55.111.157:53948] helo=BLU004-OMC4S18.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B8/C2-01590-BBC67445 for ; Wed, 22 Oct 2014 04:37:15 -0400 Received: from BLU436-SMTP237 ([65.55.111.136]) by BLU004-OMC4S18.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.22751); Wed, 22 Oct 2014 01:37:12 -0700 X-TMN: [OXOPdpukq3dAdT4jW7p5sWpnms1Bi2/v] X-Originating-Email: [bobwei9@hotmail.com] Message-ID: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 (Mac OS X Mail 8.0 \(1990.1\)) In-Reply-To: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> Date: Wed, 22 Oct 2014 10:37:11 +0200 CC: PHP Internals Content-Transfer-Encoding: quoted-printable References: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> To: Andrea Faulds X-Mailer: Apple Mail (2.1990.1) X-OriginalArrivalTime: 22 Oct 2014 08:37:09.0456 (UTC) FILETIME=[5E647D00:01CFEDD3] Subject: Re: [PHP-DEV] [RFC] Safe Casting Functions From: bobwei9@hotmail.com (Bob Weinand) I know we have that already discussed a lot now, but I=E2=80=99d like to = expose my points on the return value here: I imagine code like (supposing that we ever will have scalar typehints): function acceptsInt (int $i =3D null) { if ($i =3D=3D=3D null) { $i =3D 2 /* default value */; } /* do something with $i */ } When we return false: acceptInt(($tmp =3D to_int($_GET["userinput"])) =3D=3D=3D false ? null : = $tmp); When we throw an exception: try { acceptInt(to_int($_GET["userinput"])); } catch (CastingException $e) { acceptInt(null); } When we just return null: acceptInt(to_int($_GET["userinput"])); Also, when we want to pass a default value defined outside of the = function, it=E2=80=99s a lot easier now with the coalesce operator: acceptInt(to_int($_GET["userinput=E2=80=9C]) ?? 2 /* default value */); Also, independently of possible scalar typehints: Generally exceptions are also a bad idea as the casts probably will be = used on external input and exceptions are **not** a way to handle = malformed user input. Really not. Furthermore, false is a bad idea in the same sense (if we get scalar = type hints once), because people then might just catch the = EngineException=E2=80=A6 Also, null means "no value"; that=E2=80=99s exactly what we need. If the = to_{type}() functions cannot return a meaningful value, just return "no = value", that means null. And not false, which is a real value. That=E2=80=99s why I strongly feel that null is the only true thing to = return here. Thanks, Bob > Am 21.10.2014 um 00:57 schrieb Andrea Faulds : >=20 > Good evening, >=20 > I am presenting a new RFC to add a set of three functions to do = validated casts for scalar types: >=20 > https://wiki.php.net/rfc/safe_cast >=20 > Please read it. >=20 > Thanks! > -- > Andrea Faulds > http://ajf.me/