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.


Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytk...@ultralogistics.com
Web: www.ultralogistics.com

"A Top 100 Logistics I.T. Provider in 2011"


-----Original Message-----
From: Rasmus Lerdorf [mailto:ras...@lerdorf.com] 
Sent: Thursday, December 22, 2011 2:12 PM
To: Sebastian Bergmann
Cc: internals@lists.php.net; Dmitri Snytkine
Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC

On 12/22/2011 10:51 AM, Sebastian Bergmann wrote:
> Am 22.12.2011 19:41, schrieb Rasmus Lerdorf:
>> This is not a step forward. If the author of age_check() really doesn't
>> want to accept type-juggled arguments, then it is easy enough to do a
>> strict type check in the function itself. This puts the effort in the
>> correct place and doesn't encourage this type of coding.
> 
>  Putting such code into the "correct" place does not change the problem
>  that you and Stas describe
> 
>      function age_check($age)
>      {
>          if (!is_int($age)) {
>              throw new InvalidArgumentException;
>          }
>      }
> 
>  With the above code, the caller needs to cast and the writer of the
>  age_check() function has to copy/paste/adapt these checks to all the
>  correct places ...
> 
>  I am not advocating type hints for scalars, I am just saying that this
>  argument is not really a good one against it.

But people don't really write code like this in PHP. Which is also why
it makes very little sense to add strong typing of scalars. Why would
anyone want to add a feature that lets them do something they would
never do?

The above code is much more likely to do an is_numeric() check and/or a
hard typecast to an int. But throwing an exception when it receives "21"
instead of 21 just isn't something people tend to do.

And Dmitri, in the Mongo case you mentioned, parameters to Mongo need to
be json-wrapped, so you are going to have access functions doing this.
Why would you not want to typecast in the access function there as well
as opposed to throwing hard to catch exceptions going into the access
functions themselves?

The only way I see something like this ever happening in PHP is if we
came up with an intelligent approach that actually was type *hinting*
and not strong typing. As in:

function ((int)$age) {
  return ($age > 21) ?: false;
}

that would gracefully handle interchangeable types. But it gets tricky
once we start thinking about non-numeric strings being passed in there.

-Rasmus

-- 
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

Reply via email to