[RFC] get_debug_type

php.internals

Mark Randall

6 years ago
Greetings, I offer a short RFC to add a new function, get_debug_type, which would return the type name of a variable, automatically resolving the class name in the event of it being an object. Unlike gettype, this function will return the true name of scalar types, rather than their aliases, thus get_debug_type will return "int" rather than "integer" etc. https://wiki.php.net/rfc/get_debug_type Mark Randall marandall@php.net

Nikita Popov

6 years ago
On Sat, Feb 15, 2020 at 3:33 PM Mark Randall <markyr@gmail.com> wrote:
> Greetings, > > I offer a short RFC to add a new function, get_debug_type, which would > return the type name of a variable, automatically resolving the class > name in the event of it being an object. > > Unlike gettype, this function will return the true name of scalar > types, rather than their aliases, thus get_debug_type will return > "int" rather than "integer" etc. > > https://wiki.php.net/rfc/get_debug_type > > Mark Randall > marandall@php.net >
Can you please add some examples for the behavior? Preferably the precise output for all primitive types, for classes and for anonymous classes. Regards, Nikita

Nikita Popov

6 years ago
On Sat, Feb 15, 2020 at 4:07 PM Nikita Popov <nikita.ppv@gmail.com> wrote:
> On Sat, Feb 15, 2020 at 3:33 PM Mark Randall <markyr@gmail.com> wrote: > >> Greetings, >> >> I offer a short RFC to add a new function, get_debug_type, which would >> return the type name of a variable, automatically resolving the class >> name in the event of it being an object. >> >> Unlike gettype, this function will return the true name of scalar >> types, rather than their aliases, thus get_debug_type will return >> "int" rather than "integer" etc. >> >> https://wiki.php.net/rfc/get_debug_type >> >> Mark Randall >> marandall@php.net >> > > Can you please add some examples for the behavior? Preferably the precise > output for all primitive types, for classes and for anonymous classes. > > Regards, > Nikita >
ping

Mark Randall

6 years ago
On 17/02/2020 08:42, Nikita Popov wrote:
>> Can you please add some examples for the behavior? Preferably the precise >> output for all primitive types, for classes and for anonymous classes.
Added to RFC

Mike Schinkel

6 years ago
> On Feb 15, 2020, at 9:32 AM, Mark Randall <markyr@gmail.com> wrote: > > Greetings, > > I offer a short RFC to add a new function, get_debug_type, which would > return the type name of a variable, automatically resolving the class > name in the event of it being an object. > > Unlike gettype, this function will return the true name of scalar > types, rather than their aliases, thus get_debug_type will return > "int" rather than "integer" etc. > > https://wiki.php.net/rfc/get_debug_type >
Why "debug" type? -Mike

Mark Randall

6 years ago
On 16/02/2020 10:16, Mike Schinkel wrote:
> Why "debug" type?
I would imagine because it is only really useful in the context of debugging. There is no reason to ever expose such information to userland. The name is up for debate.
-- Mark Randall marandall@php.net

tyson andre

6 years ago
I'd definitely agree on returning `int` instead of `integer`. PHP allows both `class integer` and `class resource`, so `int` (plus documenting exact return values for examples, as Nikita mentioned) would be useful to prevent ambiguity. Also, it'd be nice to have `null` return the lowercase string null instead of uppercase. get_canonical_type() is my weak preference. I'd also considered suggesting get_class_or_type(), but that seemed misleading since the value of get_class_or_type(2) would be different from gettype(2). This would save some time and make it easier to write better messages debugging, syslogging, throwing exceptions for unexpected values, etc. - Tyson

Tom Gerrits

6 years ago
Hi Mark Thanks for this proposal. After the proposal by Nikita to allow ::class on object variables, would it be opportune to add ::type for any variable, with this behavior? It could behave like ::class on objects and return a string indicator of the type for other types, such as scalar ones. This could even include allowing ::type on actual types, i.e. int::type, MyClass::type, $myVar::type, GenericTypeInTheFuture<T>::type, and so on.

Mark Randall

6 years ago
On 15/02/2020 14:32, Mark Randall wrote:
> Greetings, > https://wiki.php.net/rfc/get_debug_type
Just a heads up that I will be putting this to the vote tomorrow. As this seems fairly uncontroversial, and in the absence of any well-supported suggestions, the vote will be on adding this as get_debug_type. Unless someone chimes in with a major objection, I do not plan on having a separate vote on the "resource (type)" style and will include it by default.
-- Mark Randall marandall@php.net