# [RFC] get_debug_type

- Thread: https://externals.io/message/108597
- Original archive: https://news-web.php.net/php.internals/108597
- Started by Mark Randall on 2020-02-15T14:32:58+00:00
- Messages: 15
- RFC: get_debug_type (Implemented (PHP 8.0)) — https://externals.io/rfc/get_debug_type.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #108597 — Mark Randall — 2020-02-15T14:32:58+00:00

URL: https://externals.io/message/108597#108597

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

---

## #108599 — Nikita Popov — 2020-02-15T15:07:01+00:00

URL: https://externals.io/message/108597#108599 · In reply to: #108597

> 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

---

## #108616 — Mike Schinkel — 2020-02-16T10:16:03+00:00

URL: https://externals.io/message/108597#108616 · In reply to: #108597

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

---

## #108621 — Mark Randall — 2020-02-16T13:15:02+00:00

URL: https://externals.io/message/108597#108621 · In reply to: #108616

> 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

---

## #108625 — Mike Schinkel — 2020-02-16T23:24:56+00:00

URL: https://externals.io/message/108597#108625

> 
> 
> 
> > […]
> 
> I would imagine because it is only really useful in the context of debugging. 


JMTCW but I generally think names should describe what something is, not what it can be used for.  The former can typically be definitive, the latter is based on perspective and thus both multiple names could apply and for some people the "can be used for" name turns out to be confusing. 

For example, when I first saw the name I was wondering if it would return some kind of debugging "mode" and was curious if there were more than one mode for debugging. I was hopeful that there was something new about debugging for me to learn. 


> There is no reason to ever expose such information to userland.

Hmm. Depending on what you envision, I could definitely see using it in userland.  Basically something that merges `gettype()` and `get_class()` so that I can get one definitive type for a value and not have to call two functions.  I can of course write my own, but my own would not be "standard" and so I would see value in having a standard function for this in core.


> The name is up for debate.

Cool.  

Though still not exactly sure where you are headed with it since there are few detailed and no code examples the first name that comes to mind for me would be get_real_type().

-Mike

---

## #108626 — G. P. B. — 2020-02-16T23:28:00+00:00

URL: https://externals.io/message/108597#108626 · In reply to: #108625

> > […]
>
> Cool.
>
> Though still not exactly sure where you are headed with it since there are
> few detailed and no code examples the first name that comes to mind for me
> would be get_real_type().
>
> -Mike
>

Erf pls no, I'd rather have something like `get_canonical_type()`

George P. Banyard

---

## #108629 — Mike Schinkel — 2020-02-17T00:48:32+00:00

URL: https://externals.io/message/108597#108629 · In reply to: #108626

> 
> 
> Erf pls no, I'd rather have something like `get_canonical_type()` 

That works fine too.

-Mike

P.S. But not sure what "Erf" means, though.  '-)

---

## #108631 — tyson andre — 2020-02-17T02:22:46+00:00

URL: https://externals.io/message/108597#108631 · In reply to: #108597

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

---

## #108636 — Tom Gerrits — 2020-02-17T08:10:44+00:00

URL: https://externals.io/message/108597#108636 · In reply to: #108597

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.

---

## #108638 — Nikita Popov — 2020-02-17T08:42:36+00:00

URL: https://externals.io/message/108597#108638 · In reply to: #108599

> 
>
> > […]
>
> 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

---

## #108640 — Mark Randall — 2020-02-17T09:46:44+00:00

URL: https://externals.io/message/108597#108640 · In reply to: #108638

> > […]

Added to RFC

---

## #108642 — Nikita Popov — 2020-02-17T10:05:10+00:00

URL: https://externals.io/message/108597#108642

> 
> > […]
> precise
> > […]
>
> Added to RFC
>

Thanks! I've taken the liberty of reformatting this into a table.

Given the naming discussion, I think there's one important part to
highlight here: For anonymous classes, this returns just "class@anonymous",
not "class@anonymous\0SOME_RANDOM_UNIQUE_STRING_HERE". Thus this
functionality certainly can't be under a ::type construct (which should
return the full name), and even get_canonical_type() seems inappropriate.

This function is specifically useful for including type names in error
messages or similar, thus `get_debug_type()`...

Nikita

---

## #108644 — Kalle Sommer Nielsen — 2020-02-17T11:42:20+00:00

URL: https://externals.io/message/108597#108644 · In reply to: #108625

Den man. 17. feb. 2020 kl. 01.25 skrev Mike Schinkel <mike@newclarity.net>:
> Though still not exactly sure where you are headed with it since there are few detailed and no code examples the first name that comes to mind for me would be get_real_type().

"real" is a type in PHP (tho we are phasing that out, see is_real()
and the (real)-cast), so that name would most likely make it more
confusing to userland.

-- 
regards,

Kalle Sommer Nielsen
kalle@php.net

---

## #108651 — Mike Schinkel — 2020-02-17T22:48:51+00:00

URL: https://externals.io/message/108597#108651 · In reply to: #108644

> 
> 
> Den man. 17. feb. 2020 kl. 01.25 skrev Mike Schinkel <mike@newclarity.net>:
> > […]
> 
> "real" is a type in PHP (tho we are phasing that out, see is_real()
> and the (real)-cast), so that name would most likely make it more
> confusing to userland.

Certainly.  

I picked "real" because of `realpath()`, but that was just the first thing that came to mind. 

My intent was to propose using a name describing function vs. envisioned use-case, and others have made good naming suggestions.

-Mike

---

## #108794 — Mark Randall — 2020-02-29T22:41:03+00:00

URL: https://externals.io/message/108597#108794 · In reply to: #108597

> 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
