Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58768 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 3291 invoked from network); 8 Mar 2012 09:04:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Mar 2012 09:04:19 -0000 Authentication-Results: pb1.pair.com smtp.mail=arvids.godjuks@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=arvids.godjuks@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.170 as permitted sender) X-PHP-List-Original-Sender: arvids.godjuks@gmail.com X-Host-Fingerprint: 209.85.212.170 mail-wi0-f170.google.com Received: from [209.85.212.170] ([209.85.212.170:40861] helo=mail-wi0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 42/50-00152-216785F4 for ; Thu, 08 Mar 2012 04:04:19 -0500 Received: by wibhj13 with SMTP id hj13so181082wib.29 for ; Thu, 08 Mar 2012 01:04:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=soh2zkC23vh2+iGWWH5rof7HSPclxIdbEYLEsWRxqDo=; b=cO/ghX7RU9tsRPwp+DlunlDf64MWkSNY90Y9slP2Q3z2MsQhy7c8UCfursR/bolde1 D2jWxwlvP3bL5U5SA1ss0zE/LnHZT7EvDT27+DIFJUggbJQPINcf+ObjkOq7FqGG0zNN vieBzGi169pW/YpQqZIw89sKrh4jbAbarIsq+R+EsWOlHvuDGyOmWo6fK56WNCFVppD9 jyBSU6Lv3DAJmca8sfny84XM9yoh/7uVFNF1yiEgwd8DiyeEvgSjZ4aXmA7E/QS4402V 14g8PYQyhcs9ZJxj1VqhXDfISwi2R5WD+txhwJXdIaDSNEXnFmx4NAPQCJBg5oiAhKcd u78w== Received: by 10.180.85.69 with SMTP id f5mr10763263wiz.18.1331197456132; Thu, 08 Mar 2012 01:04:16 -0800 (PST) MIME-Version: 1.0 Received: by 10.227.117.1 with HTTP; Thu, 8 Mar 2012 01:03:55 -0800 (PST) In-Reply-To: References: <52.38.15021.3A1E65F4@pb1.pair.com> <4F570EBB.6030103@sugarcrm.com> <60744d7ea642fe03c7110502a3f4276a@mohiva.com> Date: Thu, 8 Mar 2012 11:03:55 +0200 Message-ID: To: Simon Schick Cc: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Scalar Type Hinting From: arvids.godjuks@gmail.com (Arvids Godjuks) Hi Simon! 2012/3/8 Simon Schick : > Hi Arvids, > > I pretty much like this idea as it's more strict. Let me say something > to the questions you pointed out here. > > 2012/3/7 Arvids Godjuks : >> I realize that with scalars it's not that straight forward, but >> complicating things by adding an auto-cast syntax and so on is just >> ridiculous. Hints should stay, well, hints. The only problem we have >> is complications of accepting numerical strings or numbers as strings. >> And what to do with "null". > > I'd like to handle it the same way as it's handled with the classes > right now. If null is not the default-value you'll get an error when > you pass null in there. > One thing I'd like opened here: If you define a default-value > different than null, should you be able to pass null as well and the > compiler will use the default-value? > >> function a(bool $bool) {} >> a(10); // Kill your self against the wall - write a(true); >> If you define bool - use the damn bool! > > I like that. What should we do if this appears? As it's now - just > throw an "Catchable fatal error" and let the script blow-up? I would > go this far. I think "Catchable fatal error should" be fine and users are familiar with such mechanic because it already exists. Consistency, man, consistency :) >> I consider interchangeable only three cases: >> 1. Numerical string. >> 2. Integers and floats as strings. >> 3. Integer and string =C2=A00 1 as bool. >> >> Any other cases should error out. > > Until now I thought about the weak variable-types as a order ... > string, float, integer, Boolean. > All Boolean values are compatible be an integer (0 or 1) and all > integer are compatible to a float and so on. Do you think it's good to > have it this way? This would mean that you could also get a Boolean > true as string "1" ... I personally don't like that ... but I don't > know where to draw the strict-line. > Now think about that backwards. Can a "1" be passed as a parameter > that expects Boolean? If yes, I'd keep it consistent in both ways. > > Bye > Simon That's a good tricky question, I like it. Well, I think the lower should work just fine. function a(bool $int) {}; a("1"); Because it's conversion to bool is straight forward. What of the integer values [-=E2=88=9E, -1] and [2, +=E2=88=9E]? Really tricky question= . From one point of view they are a valid boolean true in the expressions. But the question here is not if it's a valid boolean, but the fact that we want our function to be passed with valid data and be more strict that usual on what is passed to the function/method. So I would like to see valid boolean values for a hinted argument these: true false 1 0 "1" "0" Simple and clear. 2012/3/8 Simon Schick : > Hi, > > Just a small addition to what I wrote about handling null ... > > function foo(array $d =3D array()) { var_dump($d); } > foo(null); // This fails with the message: Argument 1 passed to foo() > must be an array, null given > > As this code fails I'd not expect to change this behavior for the weak-ty= pes. > > function foo(int $d =3D 20) { var_dump($d); } > foo(null); // This should then also fail. Don't care about what's the > default-value. > > Bye > Simon > Totally agree here. I would even say that if you need a null as default value - that can't be a type hinted argument, because it already has to accept two different types of data and code inside has to handle that anyway. The type hint is essentially useless in this situation. Adding something like function a(null|string $a =3D null) kind'a silly and again is adding unneeded complexity. 2012/3/8 Simon Schick : > 2012/3/8 John Crenshaw : >> >> Conversion the other way is essential. Consider the following URL: >> >> http://example.com?foo=3D1 >> >> In your PHP script $_GET['foo'] =3D=3D=3D '1' (a string). >> >> In fact, nearly every input to PHP is a string. This is why PHP was desi= gned with some seriously robust type juggling on scalars. Any typing propos= al that wants to actually pass a vote is going to have to allow appropriate= implicit conversions from string to other types. >> >> John Crenshaw >> Priacta, Inc. > > Hi, John > > Ok .. the example with the get-parameter is quite good. > You'll often have the case that you submit a string "0" or "1" and > want to have it as boolean (f.e. if you have a dropdown with two > options). > Please keep in mind not to mix it up with checkboxes as unchecked > checkboxes won't be sent back to your webserver :) > > Bye > Simon This is exactly the example for witch type hinting should not be used. $_GET and $_POST data can be anything - string, number, array (lord, how many scripts break into fatal errors when you just pass an array instead of simple string). And passing data from these sources as params to type hinted functions is a suicide. Type hints are meant to filter input from external sources - that data should be passed through filtering and validating layer - only after this you can work with the data and passing it to type hinted methods and functions. So I will remind to every one: Type hinting is not a filtering layer. This is an instrument for designing a more strict internal layer and prevent people passing unfiltered and insecure data to that layer. It also helps to track the errors in code where invalid data slips through the cracks. It also helps with the future optimization, documentation generation and reflection. It does not mean that you can now write only type hinted code - you can't and you shouldn't even try (that will only make things unnecessary hard). I have a very good relationship with array and object type hints right now - they are used in a few key places, but their usage relative to the whole application is not very wide. Same I imagine for the scalar hints.