Re: Return Type Hinting for Methods RFC

From: Date: Thu, 22 Dec 2011 19:31:58 +0000
Subject: Re: Return Type Hinting for Methods RFC
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14  Groups: php.internals 
Request: Send a blank email to internals+get-57024@lists.php.net to get a copy of this message
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

« previous php.internals (#57024) next »