Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78226 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 30551 invoked from network); 22 Oct 2014 10:28:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Oct 2014 10:28:01 -0000 Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.218.44 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 209.85.218.44 mail-oi0-f44.google.com Received: from [209.85.218.44] ([209.85.218.44:33755] helo=mail-oi0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 39/54-01590-0B687445 for ; Wed, 22 Oct 2014 06:28:00 -0400 Received: by mail-oi0-f44.google.com with SMTP id x69so2481174oia.17 for ; Wed, 22 Oct 2014 03:27:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=VbLwyMPI9Bp+CU4kwvecyd3JMJ1kU5aUwZk2RthmHmU=; b=ifHOGD7hVkTvyXSiz7f7ner6LcPlL2S8V44fSJ2envJ601julROcJ7M9u3+RBZokvc 3IZ2TNveaVjKz9Rk6WmNboTlibWTr1oAnjJjwtrw9C2bomk0KM69JY6yEPMzuAdHwXr7 Gu4qgTjpW6g7WhL820FPejaW/Uou9xK2JcIhFiK6X0L17nURGO+DBbYLDEI94B6wpJ8M GUUBMoenst0RpnDr3KHq4pBpSMoiCbPUJhHMsE16k74PZKY1ppV3vrKfvoutMM8uBdyi 6vlLbOAp27tEZrIvy9CCSV9dnqUvj7/LpGDBH1HfYUTdD32hfFLOeAJhnwrCk6ygBITb ErgA== X-Gm-Message-State: ALoCoQnhIMB2nlVe+fr3/dnO4LhH2RT/qdtSgvGRPt5yhjN5gO5NJm2zuSQcTSmhLFtONMaUVUmsF8DqgNzP5q9xtIDJA4lQaUMItFbVwMBFyuVKxb53clDCNBfTZ4H4cVqJdy52agizT4xkge2A2RECTrrCseM+7A== MIME-Version: 1.0 X-Received: by 10.202.196.10 with SMTP id u10mr33359109oif.44.1413973677512; Wed, 22 Oct 2014 03:27:57 -0700 (PDT) Received: by 10.60.70.41 with HTTP; Wed, 22 Oct 2014 03:27:57 -0700 (PDT) In-Reply-To: References: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> Date: Wed, 22 Oct 2014 14:27:57 +0400 Message-ID: To: Bob Weinand Cc: Andrea Faulds , PHP Internals Content-Type: multipart/alternative; boundary=001a1134e000c5303e050600663f Subject: Re: [PHP-DEV] [RFC] Safe Casting Functions From: dmitry@zend.com (Dmitry Stogov) --001a1134e000c5303e050600663f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable "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 malform= ed > 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 re= turn 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/ > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a1134e000c5303e050600663f--