Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75538 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 59617 invoked from network); 15 Jul 2014 12:24:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jul 2014 12:24:39 -0000 Authentication-Results: pb1.pair.com header.from=tyra3l@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=tyra3l@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.42 as permitted sender) X-PHP-List-Original-Sender: tyra3l@gmail.com X-Host-Fingerprint: 209.85.216.42 mail-qa0-f42.google.com Received: from [209.85.216.42] ([209.85.216.42:61726] helo=mail-qa0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 63/3C-15121-68D15C35 for ; Tue, 15 Jul 2014 08:24:39 -0400 Received: by mail-qa0-f42.google.com with SMTP id j15so4565154qaq.1 for ; Tue, 15 Jul 2014 05:24:35 -0700 (PDT) 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; bh=isqAxiW9L0t9F2uduTtQg2lP5pWaOFHYBXEw6mmnZXE=; b=AxvcnhfRaiY2mcj1kZvmx6fWBiTXKCwcUjUKyW4Gc5xDp1k6lOOXoUUUh9TJsJVKrc hhG7uLuGug79j2OPxypWFkby0XVoXEdyRB6yh6Dkuqzv8fGVQkZCwyFo/45l+mRQBFzQ 6mz8/wXOKs9+Fn69oQBa+p3Xv/XTiwgV/lz6xSwDnE1KltuN2/IoKfB5uq72Y2vZKGC8 DNNQYD58og7eAKyhtFQ5zyF/hWTwE8yg8BC+uLYEWuR9lpYSd8MCkyVgAQYGYyG7C9yk tIVJuNIf3JkN93UYJpJ42s9HdXr/7yelV/HLFMyyZhbPdrAz02Gd5PzueGnla4ROVaH6 ad3Q== MIME-Version: 1.0 X-Received: by 10.224.130.5 with SMTP id q5mr31271851qas.72.1405427075613; Tue, 15 Jul 2014 05:24:35 -0700 (PDT) Received: by 10.140.102.111 with HTTP; Tue, 15 Jul 2014 05:24:35 -0700 (PDT) In-Reply-To: References: <08503591-EFC8-48E6-984E-FFC292C5EA5F@ajf.me> <53C3F70A.7010706@gmail.com> <3aa29e080d70c843713e19a952dfbad6@mail.gmail.com> <20140714170149.GE27743@phcomp.co.uk> <20140714171446.GG27743@phcomp.co.uk> Date: Tue, 15 Jul 2014 14:24:35 +0200 Message-ID: To: Kristopher Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a11c2c452998bc704fe3a7d53 Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: tyra3l@gmail.com (Ferenc Kovacs) --001a11c2c452998bc704fe3a7d53 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Tue, Jul 15, 2014 at 2:19 PM, Ferenc Kovacs wrote: > > > > On Mon, Jul 14, 2014 at 7:19 PM, Kristopher > wrote: > >> On Mon, Jul 14, 2014 at 1:14 PM, Alain Williams >> wrote: >> >> > On Mon, Jul 14, 2014 at 06:09:22PM +0100, Andrea Faulds wrote: >> > > >> > > On 14 Jul 2014, at 18:01, Alain Williams wrote: >> > > >> > > > But if you have: >> > > > >> > > > function foo(int $a) { >> > > > ... >> > > > $a =3D 3 / 2; >> > > > } >> > > > >> > > > What do you expect $a to contain ? I would suggest integer 1. >> > > >> > >> I would have expected 1 - since it appears, from the code, that $a shoul= d >> > only contain integers. >> >> >> What about the current type hinting we have, then? >> >> function foo(Bar $a) { >> $a =3D 3 / 2; >> } >> >> Perfectly OK. Why would we treat scalars any different? >> > > this was asked and answered a dozen of times in the past, but let me > repeat once again: > > php does support type juggling/coercion for scalar types, but not for > complex types. > introducing "strict" typehints for complex types was safe, as there are n= o > expectation from the users, if you expect an instance of Foo and you got = a > Bar (which isn't a subclass of Foo) then you know that you screwed up. > (personally I think that the adding the support for array to the strict > typehints was a bad idea, but that is out of scope here.) > But people do have expectations about the scalar types in php, and they > used to not care about the exact types of a variable as long as it can be > coerced into the expected type. > If you think it over, probably 80-90%+ of all incoming data we are workin= g > with coming from a source which delivers everything as a string (anything > coming from HTTP is a string, anything coming from the database which isn= 't > using a binary protocol arrives as string, anything coming from memcached > is a string, redis: same thing). > But php doesn't care, because one of it's distinguished features is that > it can coerce between scalar types. > If we introduce scalar type hints, people will use it, if they will use > it, people have to be aware that they can't pass numeric looking strings > into functions and expect them to work. > It will blow up with a fat E_RECOVERABLE_ERROR. > But this won't hold back the library authors from using it, which means > that the consumers of those libs has to start coding defensively, and mak= e > sure that the variable holding the value 123 is an integer and not a floa= t > or a string, because that will blow up something. > And we can argue whether or not it is a good thing that php has type > juggling, but we have that, and it is not likely to be changed(biggest BC > break ever), so introducing something which completelly negates that won'= t > likely to get support from the devs. > > -- > Ferenc Kov=C3=A1cs > @Tyr43l - http://tyrael.hu > sorry, not neccessary the best place for my reply, I just had to type that out seeing the dozens of mails from the last couple of days. --=20 Ferenc Kov=C3=A1cs @Tyr43l - http://tyrael.hu --001a11c2c452998bc704fe3a7d53--