Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:57024 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 52384 invoked from network); 22 Dec 2011 19:32:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Dec 2011 19:32:03 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@lerdorf.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@lerdorf.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lerdorf.com from 209.85.161.170 cause and error) X-PHP-List-Original-Sender: rasmus@lerdorf.com X-Host-Fingerprint: 209.85.161.170 mail-gx0-f170.google.com Received: from [209.85.161.170] ([209.85.161.170:46295] helo=mail-gx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F0/1F-12618-3B583FE4 for ; Thu, 22 Dec 2011 14:32:03 -0500 Received: by ggnv1 with SMTP id v1so7014586ggn.29 for ; Thu, 22 Dec 2011 11:32:00 -0800 (PST) Received: by 10.50.188.132 with SMTP id ga4mr12118086igc.4.1324582320589; Thu, 22 Dec 2011 11:32:00 -0800 (PST) Received: from [192.168.200.5] (c-50-131-44-225.hsd1.ca.comcast.net. [50.131.44.225]) by mx.google.com with ESMTPS id h9sm32045080ibh.11.2011.12.22.11.31.59 (version=SSLv3 cipher=OTHER); Thu, 22 Dec 2011 11:31:59 -0800 (PST) Message-ID: <4EF385AE.8000103@lerdorf.com> Date: Thu, 22 Dec 2011 11:31:58 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111124 Thunderbird/8.0 MIME-Version: 1.0 To: Dmitri Snytkine CC: 'Sebastian Bergmann' , internals@lists.php.net References: <2095305E-D4E3-4D7E-8218-32EE99688E0C@GMAIL.COM> <2C90FB94-38C4-4270-8C6A-B89304BA8ED8@gmail.com> <159A7CA2-8561-40DA-9434-CAAE12304DDB@gmail.com> <005701ccc0b3$58c8dee0$0a5a9ca0$@alliantinternet.com> <20111222145159.GY25857@alliantinternet.com> <006101ccc0ba$46b81160$d4283420$@alliantinternet.com> <4EF379D8.9000206@lerdorf.com> <4EF37C29.1010206@php.net> <4EF3811A.7080100@alliantinternet.com> <007f01ccc0de$b578d750$206a85f0$@alliantinternet.com> In-Reply-To: <007f01ccc0de$b578d750$206a85f0$@alliantinternet.com> X-Enigmail-Version: 1.4a1pre Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC From: rasmus@lerdorf.com (Rasmus Lerdorf) On 12/22/2011 11:20 AM, Dmitri Snytkine wrote: > Not sure what you mean by json wrapped. > In mongo you do $coll->find(array('age' => $age); > > if $age is a string '21' your will not get any erros but neither will you > get any results. It is json underneath, but in your find() example, obviously the right approach here is to do: $coll->find(array('age' => (int)$age); How is that hard? You wouldn't use a strong int type in the find() function prototype here at all since by definition the find() function needs to take all sorts of types. This is why I mentioned access functions related to Mongo. You might write something like: function age_lookup($age) { return $coll->find(array('age' => (int)$age); } but again here, doing a strong type check on the parameter isn't making your life easier. It simply pushes the responsibility to the caller and introduces a tricky unrecoverable error that will drive you crazy unless you have 100% regression test coverage (which is kind of impossible since the number of inputs is infinite) or great static analysis tools. PHP is not a compiled language, so you end up not catching these until runtime which is obviously sub-optimal. -Rasmus