Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78230 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 48801 invoked from network); 22 Oct 2014 14:16:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Oct 2014 14:16:13 -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.145 as permitted sender) X-PHP-List-Original-Sender: bobwei9@hotmail.com X-Host-Fingerprint: 65.55.111.145 blu004-omc4s6.hotmail.com Received: from [65.55.111.145] ([65.55.111.145:53541] helo=BLU004-OMC4S6.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7C/B6-01590-A2CB7445 for ; Wed, 22 Oct 2014 10:16:11 -0400 Received: from BLU437-SMTP44 ([65.55.111.137]) by BLU004-OMC4S6.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.22751); Wed, 22 Oct 2014 07:16:08 -0700 X-TMN: [cfhmd+TcTy4IR0j1jvy8/xrGVba4EZjo] X-Originating-Email: [bobwei9@hotmail.com] Message-ID: Content-Type: multipart/alternative; boundary="Apple-Mail=_14DA84C5-9A62-4BA9-8823-409A1064BE99" MIME-Version: 1.0 (Mac OS X Mail 8.0 \(1990.1\)) In-Reply-To: Date: Wed, 22 Oct 2014 16:16:01 +0200 CC: Andrea Faulds , PHP Internals References: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> To: Dmitry Stogov X-Mailer: Apple Mail (2.1990.1) X-OriginalArrivalTime: 22 Oct 2014 14:16:05.0855 (UTC) FILETIME=[B7D4B6F0:01CFEE02] Subject: Re: [PHP-DEV] [RFC] Safe Casting Functions From: bobwei9@hotmail.com (Weinand Bob) --Apple-Mail=_14DA84C5-9A62-4BA9-8823-409A1064BE99 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If we really want an integer at all price we just can use a simple (int) = cast. That=E2=80=99s AFAIK not the point of this RFC. And at that point where we can add a default as second parameter, we = also just can use NULL with ??. The latter is at the same time more = powerful and less restrictive. Also, with a second parameter, we don=E2=80=99t have any possibility to = check if the conversion was successful or if the fallback was used. Bob > Am 22.10.2014 um 14:49 schrieb Dmitry Stogov : >=20 > for me it's weird that to_int() that must return "int" may return not = "int". > NULL with ?? seems better than FALSE :) >=20 > but if we talk about safety, we should be able to relay on to_int() = return value without additional checks. >=20 > Thanks. Dmitry. >=20 > On Wed, Oct 22, 2014 at 4:35 PM, Weinand Bob > wrote: > So, what exactly changes here if we have a second parameter or just = return null by default? > It doesn=E2=80=99t make any difference, it=E2=80=99s just another way = to write it: >=20 > to_int($a, $default) > or > to_int($a) ?? $default >=20 > Also, if you want exceptions, you always can wrap a userland function = around it =E2=80=94 but I=E2=80=99d rather not wrap an userland function = around something throwing an exception=E2=80=A6 inefficient and weird. >=20 > Thanks, > Bob >=20 > > Am 22.10.2014 um 12:27 schrieb Dmitry Stogov >: > > > > "null" or "false" return value would make these functions not really > > useful, because they won't guarantee to return desired type. > > > > printf("%d\n", to_int("abcd")); // will print 0 > > > > The only reliable option to support wrong input is exceptions. > > On the other hand, exceptions maybe difficult to use or = inefficient. > > We may avoid exceptions throwing, if provide a default value: > > > > function to_int(mixed $a , int $default_value =3D null): int; > > function to_double(mixed $a , double $default_value =3D null): = double; > > function to_string(mixed $a, string $default_value =3D null): = string; > > > > Thanks. Dmitry. > > > > On Wed, Oct 22, 2014 at 12:37 PM, Bob Weinand > wrote: > > > >> 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 >: > >>> > >>> Good evening, > >>> > >>> I am presenting a new RFC to add a set of three functions to do > >> validated casts for scalar types: > >>> > >>> https://wiki.php.net/rfc/safe_cast = > >>> > >>> Please read it. > >>> > >>> Thanks! > >>> -- > >>> Andrea Faulds > >>> http://ajf.me/ = --Apple-Mail=_14DA84C5-9A62-4BA9-8823-409A1064BE99--