PHP RFC: get_debug_type


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

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


Proposed PHP Version(s)

PHP 8.0


Add get_debug_type as described
Real name Yes No
alcaeus (alcaeus)  
as (as)  
asgrim (asgrim)  
ashnazg (ashnazg)  
beberlei (beberlei)  
carusogabriel (carusogabriel)  
daverandom (daverandom)  
derick (derick)  
diegopires (diegopires)  
duncan3dc (duncan3dc)  
ekin (ekin)  
fibbarth (fibbarth)  
galvao (galvao)  
gasolwu (gasolwu)  
geekcom (geekcom)  
girgias (girgias)  
googleguy (googleguy)  
kalle (kalle)  
kguest (kguest)  
klaussilveira (klaussilveira)  
kocsismate (kocsismate)  
lcobucci (lcobucci)  
levim (levim)  
malukenho (malukenho)  
marandall (marandall)  
mariano (mariano)  
narf (narf)  
nicolasgrekas (nicolasgrekas)  
nikic (nikic)  
ocramius (ocramius)  
peehaa (peehaa)  
pmjones (pmjones)  
pollita (pollita)  
ramsey (ramsey)  
rasmus (rasmus)  
reywob (reywob)  
ruudboon (ruudboon)  
salathe (salathe)  
sergey (sergey)  
stas (stas)  
tandre (tandre)  
tiffany (tiffany)  
trowski (trowski)  
yunosh (yunosh)  
zimt (zimt)  
Final result: 42 3
This poll has been closed.



rfc/get_debug_type.txt · Last modified: 2020/08/01 23:41 by carusogabriel