Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58603 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 55213 invoked from network); 5 Mar 2012 15:45:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Mar 2012 15:45:16 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.170 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.170 mail-qy0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:51136] helo=mail-qy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 08/D3-35539-98FD45F4 for ; Mon, 05 Mar 2012 10:45:14 -0500 Received: by qcmt36 with SMTP id t36so2062870qcm.29 for ; Mon, 05 Mar 2012 07:45:11 -0800 (PST) Received-SPF: pass (google.com: domain of ircmaxell@gmail.com designates 10.224.182.201 as permitted sender) client-ip=10.224.182.201; Authentication-Results: mr.google.com; spf=pass (google.com: domain of ircmaxell@gmail.com designates 10.224.182.201 as permitted sender) smtp.mail=ircmaxell@gmail.com; dkim=pass header.i=ircmaxell@gmail.com Received: from mr.google.com ([10.224.182.201]) by 10.224.182.201 with SMTP id cd9mr2306695qab.92.1330962311522 (num_hops = 1); Mon, 05 Mar 2012 07:45:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=VZ0qZE7WNGoVd3WOhs9pgpNkKAWrPjZitQf/iX//LIY=; b=yros69oBcNfhtz10p3FSFtkLWxdvtpsmzTdurT8tv8hm/eqGFjOS5e3WIxgF/upctd nFEBCarGaWLjDoJQnuAiPaMK89K4RPqW4g16+a3Lwukts7Y9vn2ZCrWQEH7di/Cw+gAh 0EoX6ANwM2npZhPuicPSi0Bhgz6a3eZhqE6Q5BRA55kSfBepwcLO/adBwqUQ/1bsdxej pqC4n4rO2mZkYyNW5P5w7+2PQlNDxhZe7QsEtEXR3WusNF1n4zqzntZ3ankIDEkritmU uDMN/ISm36F9m5THmSE2duGDuVrFw53DB/zUx4pIQI+qXpIO0Xa2xlGY79wN32QbS+eV s4Zw== MIME-Version: 1.0 Received: by 10.224.182.201 with SMTP id cd9mr2035111qab.92.1330962311454; Mon, 05 Mar 2012 07:45:11 -0800 (PST) Received: by 10.229.49.74 with HTTP; Mon, 5 Mar 2012 07:45:11 -0800 (PST) In-Reply-To: References: Date: Mon, 5 Mar 2012 10:45:11 -0500 Message-ID: To: "Matthew Weier O'Phinney" Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [POC Patch] Scalar Type Hinting/Casting - Proof Of Concept From: ircmaxell@gmail.com (Anthony Ferrara) Matthew, Have you seen the new thread and RFC around this? https://wiki.php.net/rfc/parameter_type_casting_hints I went with option A, as I see erroring on cast as a more general problem. So for consistency, I implemented it exactly like normal explicit casts... Anthony On Mon, Mar 5, 2012 at 10:27 AM, Matthew Weier O'Phinney wrote: > On 2012-03-02, Anthony Ferrara wrote: >> Well, there are a few questions about the implementation: >> >> 1. *Which* type casting rules should it follow? >> >> a. Regular cast rules (like $foo =3D (int) $foo), where it converts >> always without error? >> b. Internal function cast rules, where it warnings on error and >> prevents execution of the function. >> c. Current type hinting rules, where if it can't convert cleanly it >> E_RECOVERABLE_ERRORS >> >> Personally, I like C the best. =A0Where if it is passed an invalid >> value, it attempts to cleanly convert, but errors out if it can't... >> But I can see other arguments being made... > > (c) seems the most sane option ot me as well. > >> 2. Should (array) be supported? =A0Perhaps. =A0So at that point, foo(arr= ay >> $bar) would do a "strict" check, and foo((array) $bar) would attempt >> to cast. =A0But my question would be: what would attempt to cast mean? >> Should it error out if you pass foo(1)? =A0That's what the internal >> function cast rules do. =A0And to me that's more obvious than silently >> converting it to foo(array(1))... > > Turn this around and look at it from the current state of PHP: > > =A0 =A0function foo($bar) > =A0 =A0{ > =A0 =A0 =A0 =A0$bar =3D (array) $bar; > =A0 =A0} > > If you pass a value of 1 for $bar, $bar is then converted to array(1). > That's what I'd expect the following to do as well: > > =A0 =A0function foo((array) $bar) > =A0 =A0{ > =A0 =A0} > > It's casting, and clearly different than: > > =A0 =A0function foo(array $bar) > =A0 =A0{ > =A0 =A0} > > which is doing a typehint check. > >> 3. Should references be supported? =A0My feeling is yes, they should. >> So if you do foo((array) &$bar), it would cast the original value (if >> possible) as well. > > I personally would expect casting and references to be mutually > exclusive -- if you're casting, you're changing the value type, and I > wouldn't expect a destructive operation like this from passing a value > to a function/method call. > > > >> 5. What about BC breaks? =A0Well, this entire patch (up to this point) >> wouldn't require one. =A0it's only adding the casting functionality >> (which is not implemented today), so no problem. =A0Existing code would >> still function fine. > > This is something that should be highlighted. I've seen a lot of folks > claiming type hinting is viral, and the arguments make no sense to me. > What your patch is offering is _opt_in_ type casting of function/method > arguments. You don't _have_ to write your functions or methods using > them, and for those who do, it should have no side effects on code > calling it. > > I would _LOVE_ to see this as part of PHP. > > -- > Matthew Weier O'Phinney > Project Lead =A0 =A0 =A0 =A0 =A0 =A0| matthew@zend.com > Zend Framework =A0 =A0 =A0 =A0 =A0| http://framework.zend.com/ > PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >