Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:48403 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 2560 invoked from network); 23 May 2010 07:52:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 May 2010 07:52:55 -0000 Authentication-Results: pb1.pair.com header.from=peter.e.lind@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=peter.e.lind@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: peter.e.lind@gmail.com X-Host-Fingerprint: 209.85.214.170 mail-iw0-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:47484] helo=mail-iw0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 0D/A5-54147-5DED8FB4 for ; Sun, 23 May 2010 03:52:54 -0400 Received: by iwn10 with SMTP id 10so2753813iwn.29 for ; Sun, 23 May 2010 00:52:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=u6BweFdNfuF5XXl2z14IuexuAny4ebvO+1XK2aw9u7M=; b=WBOuyJCwkqVINPZm46BbEC9aebmqWIfMGVQ9Ky4qxYSe5PIq9G1DxXngHYMZxcnM8+ wZXu9BF8NzCyVS16ewURhxOEnYedxasbA9LwlaysA8B5BL4j+P05Pu++HnBqrNgZqGBN EASyTKAwag5GHwd3Qps6ewygO5V56d4f3ceG0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=e2F+5ytxCJ4XRQt9mSFq5E5J1iWZtTcrSlGqyB9o/AlziesQyzlYmDivw3Py9x40Vl zjIgRmmpE+cwMsnfqDiEZZIXxZHReD2NdCCK8YSgD7r8vuTznFvMV+ZugpkxjwZS41yd g9Og5xApqhg02xqdoTsLgWSTnHDs7/ui6DfaY= Received: by 10.231.178.135 with SMTP id bm7mr3339873ibb.73.1274601171155; Sun, 23 May 2010 00:52:51 -0700 (PDT) MIME-Version: 1.0 Received: by 10.231.19.9 with HTTP; Sun, 23 May 2010 00:52:31 -0700 (PDT) In-Reply-To: <201005230052.05661.larry@garfieldtech.com> References: <7.0.1.0.2.20100522175819.0a601c68@zend.com> <7.0.1.0.2.20100523073125.0a601600@zend.com> <201005230052.05661.larry@garfieldtech.com> Date: Sun, 23 May 2010 09:52:31 +0200 Message-ID: To: Larry Garfield Cc: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Type hinting From: peter.e.lind@gmail.com (Peter Lind) On 23 May 2010 07:52, Larry Garfield wrote: > On Saturday 22 May 2010 11:43:50 pm Zeev Suraski wrote: >> At 01:01 23/05/2010, Hannes Magnusson wrote: >> >On Sat, May 22, 2010 at 22:39, Lukas Kahwe Smith > wrote: >> > > On 22.05.2010, at 18:30, Josh Davis wrote: >> > >> As you wrote, you worked on PHP's _type system_ which is dynamic, >> > >> really cool, juggles strings with ints and what not. However, the >> > >> topic here is the _type hinting system_. As far as I know, there's = no >> > >> "weak" type hinting; if a method signature hints for an array and i= s >> > >> given an integer, it throws a catchable fatal error. Therefore, as = a >> > >> user, what I am familiar to is a strict _type hinting system_. >> > >> Anything else would feel inconsistent to me. >> > >> >I agree. function foo(array $x) {} foo(123); doesn't cast int(123) to >> >array(123) so introducing different meaning for scalar types feels >> >very very wrong. >> >> You're ignoring one simple fact - in PHP, scalar types are >> interchangeable. =C2=A0Unlike array->scalar which has always been a corn= er >> case and a sketchy one (and I said numerous times that had I rewrote >> the language now, array->scalar conversion would have probably >> resulted in an error) - string->number, number->string, >> string->boolean conversions are a cornerstone of the language. =C2=A0If >> you don't like the fact that PHP's scalar types are interchangeable - >> you're in the wrong language! >> >> In terms of consistency, the difference between array type hinting >> and scalar type hinting you allude to is negligible in comparison to >> the inconsistency with, well, just about every other part of the >> language - operators, control structures and built-in functions >> (expecting sqrt("64") to fail, since sqrt() expects a >> number? =C2=A0expecting if (7) to fail, since it expects a >> boolean?). =C2=A0Auto-converting type hinting extends the same semantics >> available to internal functions (through zend_parse_parameters()) to >> userland functions. =C2=A0That consistency is by far the most important = IMHO. >> >> Zeev > > I have to largely agree with Zeev here. =C2=A0I'm not an internals develo= per but I > do write a lot of widely used framework-ish open source PHP. > > The current OO type specifying system I have always mentally viewed, and = most > people I know seem to view it, as a check not for "is this an X", but "ca= n I > treat this as X without things exploding"? =C2=A0In the context of an obj= ect, > "exploding" would mean a "method not found" fatal. =C2=A0In the context o= f an > array, it would be the dreaded "parameter 1 to foreach() is not an array"= . > The idea is two fold: One, better documentation (especially for code > assistance IDEs), and two, if it's going to explode it should explode in = a way > that's useful in terms of where it exploded. =C2=A0(Vis, the error messag= e is on a > useful line and tells me what the actual problem was.) > > For scalar types, exploding would mean "loss of precision". =C2=A0There i= s no loss > of precision in converting "5" to int(5), so that doesn't explode, nor sh= ould > it. =C2=A0There is similarly no loss of precision converting from int(5) = to > float(5), so that also shouldn't explode. =C2=A0"123abc" does have a loss= of > precision (data would get lost in the conversion) so that should fail bot= h int > and float checks. > > If anything, I'd be even more forgiving than the table in Zeev and Lukas'= RFP. > float(12) converting to an int doesn't have any precision loss so I don't= think > that should fail. > > The RFP includes a variety of good reasons why a more naive strict check = is > inconsistent to the point of making it a bug rather than a feature. =C2= =A0One in > particular I want to call out: Everything that comes back from a database= does > so as a string. =C2=A0To wit: > > table users(user_id, name, pass, blah); > > $user_id =3D $pdo->query("SELECT user_id FROM users WHERE name=3D'bob' AN= D > pass=3D'foo'")->fetchColumn(); > > > doStuff($user_id); > > function doStuff(int $user_id) { > =C2=A0// Whatever. > } > > > The above code will fail with the current implementation, even though we = know > for a fact that user_id is integer data because that column is an int in = the > database itself. =C2=A0Requiring an extra blind (int) stuffed in there is= pointless, > and if anything just trains people to blindly cast data without thinking = about > what it really is. =C2=A0I can see that introducing more bugs than it avo= ids. > > --Larry Garfield > +1 Regards Peter --=20 WWW: http://plphp.dk / http://plind.dk LinkedIn: http://www.linkedin.com/in/plind Flickr: http://www.flickr.com/photos/fake51 BeWelcome: Fake51 Couchsurfing: Fake51