On 12 May 2015 21:32:35 BST, Lester Caine <les...@lsces.co.uk> wrote:
>On 12/05/15 20:57, Rowan Collins wrote:
>> Type hints are not intended for validating data that is entering or
>leaving PHP from or to other systems, they are only relevant when
>passing data from one part of a PHP system to another. On any given
>install of PHP, two libraries communicating with each other by function
>calls will agree on a definition of "int", because they are in the same
>process, and that is all that is needed for an "int" type hint to be
>meaningful.
>
>I think that perhaps this is a key point that needs to be fully
>documented before PHP7 is released. That type hints are essentially NOT
>to be used for data validation may not be obvious to many who are
>planning to 'convert' to using them?

Perhaps, but this is exactly the same purpose type hints have in other 
languages, AFAIK. Types in different systems have different meanings, and no 
one language can remove those differences.

>However I still don't see how one one can handle data handed over
>internally without also providing a mechanism to ensure the right range
>of value is maintained through the system. If a library produces
>results
>that are out of range of the target channel then simply establishing
>that it's a valid number is of limited use.

It is of limited use in some situations, yes. Knowing that a value is an 
integer as defined by PHP is entirely sufficient if the task at hand is to 
manipulate it mathematically as an integer. Knowing that a value is a string is 
of limited value if the task at hand is to use it as an e-mail address. 
Database input of integers is more similar to the second case than the first.

>It IS the handling of data validation as a central service that is
>needed, and why would one not then use that for the internal data
>handling as well?

It may be that that is what YOU need, but that doesn't make other people's 
needs irrelevant.

It is already possible to pass around, and type hint for, any class of object, 
so you could for instance have a DatabaseID class. PHP's lack of operator 
overloading makes that less appealing, perhaps, but it would work.

In some languages, you can simply declare "range" or "domain" types, which are 
scalar types annotated with upper and lower bounds, or other limits. Ada 
apparently encourages this, for instance. The problem with doing so in PHP is 
that a variable's type is not fixed or declared, so in something as simple as 
$a = $b + 1, should $a have the same range as $b or not?

Of course, if you want "PHP, but with a stricter type system", then you can 
always use Hack. :)

Regards,
-- 
Rowan Collins
[IMSoP]


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to