get_debug_type View as Markdown

Implemented (PHP 8.0)
Authors
Mark Randall, marandall@php.net
Date
2020-02-15
Version
1
Source
wiki.php.net/rfc/get_debug_type

Vote

Add get_debug_type as described closed
Yes
42
No
3
45 voters
alcaeus Yes 2020-03-02 09:45
as Yes 2020-03-03 16:40
asgrim Yes 2020-03-02 14:00
ashnazg Yes 2020-03-05 18:52
beberlei Yes 2020-03-01 13:23
carusogabriel Yes 2020-03-08 22:20
daverandom Yes 2020-03-16 11:46
derick Yes 2020-03-06 11:04
diegopires Yes 2020-03-06 14:27
duncan3dc Yes 2020-03-02 21:35
ekin Yes 2020-03-02 11:29
fibbarth Yes 2020-03-02 15:36
galvao Yes 2020-03-02 09:52
gasolwu Yes 2020-03-02 02:17
geekcom Yes 2020-03-02 17:58
girgias Yes 2020-03-02 10:00
googleguy Yes 2020-03-07 18:33
kalle Yes 2020-03-01 14:27
kguest Yes 2020-03-11 16:08
klaussilveira Yes 2020-03-03 19:02
kocsismate Yes 2020-03-01 18:59
lcobucci Yes 2020-03-06 09:54
levim Yes 2020-03-01 16:20
malukenho Yes 2020-03-02 20:14
marandall Yes 2020-03-01 13:21
mariano Yes 2020-03-06 13:49
narf Yes 2020-03-10 09:30
nicolasgrekas Yes 2020-03-02 09:05
nikic Yes 2020-03-02 08:52
ocramius Yes 2020-03-01 13:34
peehaa Yes 2020-03-01 13:31
pmjones Yes 2020-03-02 14:42
pollita No 2020-03-11 14:18
ramsey Yes 2020-03-10 16:57
rasmus Yes 2020-03-04 02:54
reywob Yes 2020-03-02 18:01
ruudboon Yes 2020-03-04 06:45
salathe No 2020-03-02 00:01
sergey Yes 2020-03-02 08:47
stas No 2020-03-01 19:57
tandre Yes 2020-03-04 14:08
tiffany Yes 2020-03-02 21:14
trowski Yes 2020-03-01 15:32
yunosh Yes 2020-03-01 17:32
zimt Yes 2020-03-02 09:41

Discussions on the mailing list

[RFC] get_debug_type
by Mark Randall · 15 messages
[VOTE] get_debug_type
by Mark Randall · 2 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

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.

$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