Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58617 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 54457 invoked from network); 5 Mar 2012 19:16:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Mar 2012 19:16:12 -0000 Authentication-Results: pb1.pair.com smtp.mail=simonsimcity@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=simonsimcity@googlemail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 209.85.214.170 as permitted sender) X-PHP-List-Original-Sender: simonsimcity@googlemail.com X-Host-Fingerprint: 209.85.214.170 mail-tul01m020-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:36627] helo=mail-tul01m020-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 77/2B-35539-BF0155F4 for ; Mon, 05 Mar 2012 14:16:12 -0500 Received: by obbwd1 with SMTP id wd1so5251744obb.29 for ; Mon, 05 Mar 2012 11:16:09 -0800 (PST) Received-SPF: pass (google.com: domain of simonsimcity@googlemail.com designates 10.182.155.68 as permitted sender) client-ip=10.182.155.68; Authentication-Results: mr.google.com; spf=pass (google.com: domain of simonsimcity@googlemail.com designates 10.182.155.68 as permitted sender) smtp.mail=simonsimcity@googlemail.com; dkim=pass header.i=simonsimcity@googlemail.com Received: from mr.google.com ([10.182.155.68]) by 10.182.155.68 with SMTP id vu4mr8456960obb.61.1330974969185 (num_hops = 1); Mon, 05 Mar 2012 11:16:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=a3Xh7rRzahWGOA8ms+gjLsbpZhI87OVlvkI/l40F1WI=; b=JNoVrQTyrUwfcup8mw6zdctDlG+DUJeFfaNLOdFPtjV3k6iGzpRTMwaeZXy/dQgS6P r9gc0IySaGMPB6NUoPxlO+VLE+Hhp1aZldNEdvxRsZb3UxP8cFB6MeCB4V4m5PSyMSxL wtXSQX0y+GJbiLmjjKedBZ4JMQMtocc5BA14Wzab2jDqBVP1z2LNST9ldKcNtTyhMvU/ Yp96744WKlq8DrrJzeuWGp1vBynD+SAOk8AXY5pwhvPrbwKpqrWh5Hb/Ab+PhrYKfXHW UdadV1dcpijbc4bt7zGbR2+akHeBxjEZgHBhRmCZjRwwAPJUbHWdPYe6QZfa0s6snerP OX+w== MIME-Version: 1.0 Received: by 10.182.155.68 with SMTP id vu4mr7425571obb.61.1330974969071; Mon, 05 Mar 2012 11:16:09 -0800 (PST) Received: by 10.60.56.101 with HTTP; Mon, 5 Mar 2012 11:16:09 -0800 (PST) In-Reply-To: References: Date: Mon, 5 Mar 2012 20:16:09 +0100 Message-ID: To: Lazare Inepologlou Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=f46d044784973c88a304ba83c229 Subject: Re: [PHP-DEV] [POC Patch] Scalar Type Hinting/Casting - Proof Of Concept From: simonsimcity@googlemail.com (Simon Schick) --f46d044784973c88a304ba83c229 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, Lazare Sorry, I've only looked at your first array-example :) Bye Simon 2012/3/5 Lazare Inepologlou > In your examples you are accessing an maybe non-existing array-key > > > Yes, this is why I used the error silencing (@) operator. But anyway, it > is irrelevant to the whole proposal. > > Lazare INEPOLOGLOU > Ing=C3=A9nieur Logiciel > > > > 2012/3/5 Simon Schick > >> Hi, Lazare >> >> In your examples you are accessing an maybe non-existing array-key. >> This will raise an E_NOTICE. See the note below this example: >> http://php.net/manual/en/language.types.array.php#example-85 >> >> Maybe you also want something like that: >> isset($x) ? (is_null($x) ? null : (int)$x) : null >> >> But let's discuss that in a different thread. >> >> Bye >> Simon >> >> 2012/3/5 Lazare Inepologlou >> >>> Anthony, >>> >>> I still don't like the null-as-a-default-value solution. I find it >>> confusing. >>> >>> I know that something similar appears in class type hinting, but: >>> 1. Class type hinting does not do casting (yet). >>> 2. Apart from null, no other value could be placed anyway. (Even that i= s >>> a >>> little bit wrong as null belongs to a different type than the hinted >>> class). >>> >>> ------- >>> >>> I have a different proposal. The argument type hinting/casting should n= ot >>> be bothered with that at all. Instead, we could expand the type jugglin= g >>> system a little bit, with the introduction of a special type of casting >>> that leaves null unchanged. Something like this: >>> >>> (int?) $x >>> >>> which should be strictly translated to the following, without any way t= o >>> change that behavior by any type casting overload system: >>> >>> is_null($x) ? null : (int)$x >>> >>> Examples: >>> >>> (int?) 13 // 13 >>> (int?) '' // 0 >>> (int?) 0 // 0 >>> (int?) null // null >>> (int?) '342.3Test' // 342 >>> >>> I can think of many real world scenarios that could benefit from this. >>> The >>> first that comes to my mind is reading from a database, in cases that t= he >>> value of null totally different than the value of 0. >>> >>> $parent_id =3D (int?) $db['PARENT_ID']; // null and 0 mean different >>> things >>> here... >>> >>> A second example is reading from the query string: >>> >>> $id =3D (int?) @$_GET['id']; // the error-silencing operator will ret= urn >>> null on error. >>> >>> >>> Thoughts? >>> >>> >>> Lazare INEPOLOGLOU >>> Ing=C3=A9nieur Logiciel >>> >>> >>> 2012/3/5 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 convert= s >>> > >> 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 i= t >>> > >> E_RECOVERABLE_ERRORS >>> > >> >>> > >> Personally, I like C the best. Where 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? Perhaps. So at that point, >>> foo(array >>> > >> $bar) would do a "strict" check, and foo((array) $bar) would attem= pt >>> > >> to cast. But my question would be: what would attempt to cast mea= n? >>> > >> Should it error out if you pass foo(1)? That's what the internal >>> > >> function cast rules do. And to me that's more obvious than silent= ly >>> > >> converting it to foo(array(1))... >>> > > >>> > > Turn this around and look at it from the current state of PHP: >>> > > >>> > > function foo($bar) >>> > > { >>> > > $bar =3D (array) $bar; >>> > > } >>> > > >>> > > 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: >>> > > >>> > > function foo((array) $bar) >>> > > { >>> > > } >>> > > >>> > > It's casting, and clearly different than: >>> > > >>> > > function foo(array $bar) >>> > > { >>> > > } >>> > > >>> > > which is doing a typehint check. >>> > > >>> > >> 3. Should references be supported? My 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? Well, this entire patch (up to this poin= t) >>> > >> wouldn't require one. it's only adding the casting functionality >>> > >> (which is not implemented today), so no problem. Existing 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 usin= g >>> > > 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 | matthew@zend.com >>> > > Zend Framework | 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 >>> > > >>> > >>> > -- >>> > PHP Internals - PHP Runtime Development Mailing List >>> > To unsubscribe, visit: http://www.php.net/unsub.php >>> > >>> > >>> >> >> > --f46d044784973c88a304ba83c229--