Hi all,
PHP 8.5 has deprecated the following type names:
- integer
- double
- boolean
- binary
But the gettype() function still returns integer, double and boolean
instead of int, float and bool.
This inconsistency is quite confusing for new developers (and even older
ones).
I understand that changing the return of gettype might break a lot of
existing code, so maybe it's not a good idea (yet).
But maybe we could add a parameter to gettype, for example:
gettype(mixed $value, bool $strict = false): string
If true, then it would return the "new" canonical names. And then in
the next version emit a depreciation notice if $strict is not supplied.
And then in PHP 9 maybe change the default return of gettype, but still
allow to pass $strict = false, so that upgrading the existing code base
would just be a matter of doing a regexp replace of gettype(...) to
gettype(..., false).
Adding a depreciation notice if $strict is not supplied would make
things just painful for everyone I think though.
It's a small thing but it seems like having the same canonical names
everywhere would make things more consistent.
Or the cheap solution would be to just add the $strict parameter
and update the gettype documentation to say that the types returned
by default are not the "new" scalar and passing $strict is needed in
order to match what "new" PHP is using.
Cheers everyone.
BohwaZ php@bohwaz.net hat am 01.12.2025 21:12 CET geschrieben:
Hi all,
PHP 8.5 has deprecated the following type names:
- integer
- double
- boolean
- binary
But the
gettype()function still returns integer, double and boolean
instead of int, float and bool.This inconsistency is quite confusing for new developers (and even older
ones).I understand that changing the return of gettype might break a lot of
existing code, so maybe it's not a good idea (yet).But maybe we could add a parameter to gettype, for example:
gettype(mixed $value, bool $strict = false): string
If true, then it would return the "new" canonical names. And then in
the next version emit a depreciation notice if $strict is not supplied.And then in PHP 9 maybe change the default return of gettype, but still
allow to pass $strict = false, so that upgrading the existing code base
would just be a matter of doing a regexp replace of gettype(...) to
gettype(..., false).Adding a depreciation notice if $strict is not supplied would make
things just painful for everyone I think though.It's a small thing but it seems like having the same canonical names
everywhere would make things more consistent.Or the cheap solution would be to just add the $strict parameter
and update the gettype documentation to say that the types returned
by default are not the "new" scalar and passing $strict is needed in
order to match what "new" PHP is using.Cheers everyone.
Hello,
thank you! To me it makes sense and improves consistency.
Instead of returning a String it would be also possible to return an Enum but that would more likely be a new function.
There were previous rfcs for new functions:
https://wiki.php.net/rfc/var_type
https://wiki.php.net/rfc/var_info
Best Regards
Thomas
Le 01/12/2025 à 21:12, BohwaZ a écrit :
Hi all,
PHP 8.5 has deprecated the following type names:
- integer
- double
- boolean
- binary
But the
gettype()function still returns integer, double and boolean
instead of int, float and bool.This inconsistency is quite confusing for new developers (and even older
ones).I understand that changing the return of gettype might break a lot of
existing code, so maybe it's not a good idea (yet).
And if I remember correctly, that's exactly why get_debug_type() was
introduced: no BC break, and it does the job.
You should simply use get_debug_type() everywhere and never use
gettype() ever again. This is the most sensible move you can make, it
really is the function you should use in most case.
--
Pierre
And if I remember correctly, that's exactly why
get_debug_type()was
introduced: no BC break, and it does the job.You should simply use
get_debug_type()everywhere and never use
gettype()ever again. This is the most sensible move you can make, it
really is the function you should use in most case.
Ah! I've never heard of this function before in my last 25 years of
PHP ^^
I'll open a PR to change the doc about gettype to make it clearer in
the gettype page that get_debug_type should be used in priority.
Thanks.