Hi all,

External data can have any form of numbers.
Current PHP can handle them as "string". However PHP7's type hint cannot
handle numeric data well because it only has "int" and "float" hints.

http://3v4l.org/6J0bZ

There are cases that programmer want/need to handle any numeric values.
e.g. JSON numeric, Database numeric, PHP's array key beyonds INT min/max,
Values for BCMath/GMP, etc. It's common for 32 bit platforms. For example,
we cannot do query database safely with "int" type hint.

e.g.
function get_some_db_record(int $id) {}

Most databases uses 64 bit signed int for database IDs, but this code limits
$id to 32 bit signed int for 32 bit platforms. There are databases that
allow
unsigned 64 bit int ID.

To avoid this problem, users must use "string" type hint and have to
validate
parameter by themselves. This ruins benefits of type hint. Most PHP will not
use "string" type hint even if apps need large numbers.

How about have "numeric" type hint that accepts any format/class(GMP)
of numeric values?

function foo(numeric $var) { // do something useful with numeric value }

To be honest, I would like to have StrictSTH RFC behavior for weak mode
int/float type hints to make sure "int"/"float" has integer/float
form/value always,
but "numeric" type hint may do the job. One function with "int"/"float"
type hint
could break app with current type hint implementation, though. i.e. Working
app suddenly emits fatal error and exits when database/json returns value
beyond int/float.

http://3v4l.org/6J0bZ (See how it works with large string integer value)

Any comments?

Regards,

--
Yasuo Ohgaki
yohg...@ohgaki.net

Reply via email to