# RFC: get_debug_type

- Status: Implemented (PHP 8.0)
- Authors: Mark Randall, marandall@php.net
- Date: 2020-02-15
- Version: 1
- Original: https://wiki.php.net/rfc/get_debug_type
- Page: https://externals.io/rfc/get_debug_type
- 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

## Votes

### Add get_debug_type as described (closed)

Yes: 42 · No: 3

Voters: alcaeus (Yes), as (Yes), asgrim (Yes), ashnazg (Yes), beberlei (Yes), carusogabriel (Yes), daverandom (Yes), derick (Yes), diegopires (Yes), duncan3dc (Yes), ekin (Yes), fibbarth (Yes), galvao (Yes), gasolwu (Yes), geekcom (Yes), girgias (Yes), googleguy (Yes), kalle (Yes), kguest (Yes), klaussilveira (Yes), kocsismate (Yes), lcobucci (Yes), levim (Yes), malukenho (Yes), marandall (Yes), mariano (Yes), narf (Yes), nicolasgrekas (Yes), nikic (Yes), ocramius (Yes), peehaa (Yes), pmjones (Yes), pollita (No), ramsey (Yes), rasmus (Yes), reywob (Yes), ruudboon (Yes), salathe (No), sergey (Yes), stas (No), tandre (Yes), tiffany (Yes), trowski (Yes), yunosh (Yes), zimt (Yes)

## Discussions on the mailing list

- [\[RFC\] get_debug_type](https://externals.io/message/108597.md) — started on 2020-02-15, 15 messages
- [\[VOTE\] get_debug_type](https://externals.io/message/108795.md) — started on 2020-03-01, 2 messages

---

## Proposal

This RFC proposes to add a new function get\_debug\_type that will return the given type of a variable.

This is to replace the following pattern, which may be used to generate debugging messages when dealing with types which cannot be handled by existing PHP runtime checking based on parameter types, such as those coming from an array.

```php
$bar = $arr['key'];
if (!($bar instanceof Foo)) { 
  // this shows the most simple of patterns, to get the real type an assoc array
  // must be present to convert long-form "integer" into int etc.
  throw new TypeError('Expected ' . Foo::class . ' got ' . (is_object($bar) ? get_class($bar) : gettype($bar)));
}
 
// would become
if (!($bar instanceof Foo)) { 
  throw new TypeError('Expected ' . Foo::class . ' got ' . get_debug_type($bar));
}
 
$bar->someFooMethod();
```

This function would differ from gettype in that it would return native type names, e.g. “int” rather than “integer” and would automatically resolve class names. The following table shows what `get_debug_type()` returns for different values, and what `gettype()` returns for the same value (if it is different):

| Value | get\_debug\_type() | gettype() |
|---|---|---|
| 0 | int | integer |
| 0.1 | float | double |
| true | bool | boolean |
| false | bool | boolean |
| “hello” | string |  |
| \[\] | array |  |
| null | null | NULL |
| A class with name “Foo\\Bar” | Foo\\Bar | object |
| An anonymous class | class@anonymous | object |
| A resource | resource (xxx) | resource |
| A closed resource | resource (closed) |  |

## Backward Incompatible Changes

None

## Proposed PHP Version(s)

PHP 8.0

## Voting

**Vote: Add get\_debug\_type as described** — Yes: 42 · No: 3 (closed)

## Implementation

<https://github.com/php/php-src/pull/5143>

## References

Initial discussion @ <https://github.com/php/php-src/pull/5143>
