rfc:get_debug_type

This is an old revision of the document!


PHP RFC: get_debug_type

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::class)) { 
  throw new TypeError('Expected ' . Foo::class . ' got ' . (is_object($bar) ? get_class($bar) : gettype($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

Proposed Voting Choices

Yes / no vote on adding the new function.

Yes / no on resource names be resolved - Would appear as resource(resource_type).

(Optional) A vote on the name, if there is not a clear majority consensus during RFC discussion.

Implementation

References

rfc/get_debug_type.1582536314.txt.gz · Last modified: 2020/02/24 09:25 by marandall