Skip to content

Commit 51aee00

Browse files
authored
Improve get_debug_type() docs
Closes GH-591.
1 parent de8b838 commit 51aee00

File tree

2 files changed

+213
-1
lines changed

2 files changed

+213
-1
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- $Revision$ -->
3+
<refentry xml:id="function.get-debug-type" xmlns="http://docbook.org/ns/docbook">
4+
<refnamediv>
5+
<refname>get_debug_type</refname>
6+
<refpurpose>Gets the type name of a variable in a way that is suitable for debugging</refpurpose>
7+
</refnamediv>
8+
9+
<refsect1 role="description">
10+
&reftitle.description;
11+
<methodsynopsis>
12+
<type>string</type><methodname>get_debug_type</methodname>
13+
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
14+
</methodsynopsis>
15+
<para>
16+
Returns the resolved name of the PHP variable <parameter>value</parameter>.
17+
This function will resolve objects to their class name, resources to their
18+
resource type name, and scalar values to their common name as would be used in type
19+
declarations.
20+
</para>
21+
<para>
22+
This function differs from <function>gettype</function> in that it returns type names
23+
that are more consistent with actual usage, rather than those present for historical reasons.
24+
</para>
25+
</refsect1>
26+
27+
<refsect1 role="parameters">
28+
&reftitle.parameters;
29+
<para>
30+
<variablelist>
31+
<varlistentry>
32+
<term><parameter>value</parameter></term>
33+
<listitem>
34+
<para>
35+
The variable being type checked.
36+
</para>
37+
</listitem>
38+
</varlistentry>
39+
</variablelist>
40+
</para>
41+
</refsect1>
42+
43+
<refsect1 role="returnvalues">
44+
&reftitle.returnvalues;
45+
<para>
46+
Possible values for the returned string are:
47+
48+
<informaltable>
49+
<tgroup cols="2">
50+
<thead>
51+
<row>
52+
<entry>Type + State</entry>
53+
<entry>Return Value</entry>
54+
<entry>Notes</entry>
55+
</row>
56+
</thead>
57+
<tbody>
58+
<row>
59+
<entry>null</entry>
60+
<entry>
61+
<literal>"null"</literal>
62+
</entry>
63+
<entry>-</entry>
64+
</row>
65+
<row>
66+
<entry>Booleans (true or false)</entry>
67+
<entry>
68+
<literal>"bool"</literal>
69+
</entry>
70+
<entry>-</entry>
71+
</row>
72+
<row>
73+
<entry>Integers</entry>
74+
<entry>
75+
<literal>"int"</literal>
76+
</entry>
77+
<entry>-</entry>
78+
</row>
79+
<row>
80+
<entry>Floats</entry>
81+
<entry>
82+
<literal>"float"</literal>
83+
</entry>
84+
<entry>-</entry>
85+
</row>
86+
<row>
87+
<entry>Strings</entry>
88+
<entry>
89+
<literal>"string"</literal>
90+
</entry>
91+
<entry>-</entry>
92+
</row>
93+
<row>
94+
<entry>Arrays</entry>
95+
<entry>
96+
<literal>"array"</literal>
97+
</entry>
98+
<entry>-</entry>
99+
</row>
100+
<row>
101+
<entry>Resources</entry>
102+
<entry>
103+
<literal>"resource (resourcename)"</literal>
104+
</entry>
105+
<entry>-</entry>
106+
</row>
107+
<row>
108+
<entry>Resources (Closed)</entry>
109+
<entry>
110+
<literal>"resource (closed)"</literal>
111+
</entry>
112+
<entry>Example: A file stream after being closed with fclose.</entry>
113+
</row>
114+
<row>
115+
<entry>Objects from Named Classes</entry>
116+
<entry>
117+
The full name of the class including its namespace e.g. <literal>Foo\Bar</literal>
118+
</entry>
119+
<entry>-</entry>
120+
</row>
121+
<row>
122+
<entry>Objects from Anonymous Classes</entry>
123+
<entry>
124+
<literal>"class@anonymous"</literal>
125+
</entry>
126+
<entry>
127+
Anonymous classes are those created through the $x = new class { ... } syntax
128+
</entry>
129+
</row>
130+
</tbody>
131+
</tgroup>
132+
</informaltable>
133+
</para>
134+
</refsect1>
135+
136+
137+
<refsect1 role="examples">
138+
&reftitle.examples;
139+
<para>
140+
<example>
141+
<title><function>gettype</function> example</title>
142+
<programlisting role="php">
143+
<![CDATA[
144+
<?php
145+
echo get_debug_type(null) . PHP_EOL;
146+
echo get_debug_type(true) . PHP_EOL;
147+
echo get_debug_type(1) . PHP_EOL;
148+
echo get_debug_type(0.1) . PHP_EOL;
149+
echo get_debug_type("foo") . PHP_EOL;
150+
echo get_debug_type([]) . PHP_EOL;
151+
152+
$fp = fopen(__FILE__, 'rb');
153+
echo get_debug_type($fp) . PHP_EOL;
154+
155+
fclose($fp);
156+
echo get_debug_type($fp) . PHP_EOL;
157+
158+
echo get_debug_type(new stdClass) . PHP_EOL;
159+
echo get_debug_type(new class {}) . PHP_EOL;
160+
?>
161+
]]>
162+
</programlisting>
163+
&example.outputs.similar;
164+
<screen>
165+
<![CDATA[
166+
null
167+
bool
168+
int
169+
float
170+
string
171+
array
172+
resource (stream)
173+
resource (closed)
174+
stdClass
175+
class@anonymous
176+
]]>
177+
</screen>
178+
</example>
179+
</para>
180+
</refsect1>
181+
182+
<refsect1 role="seealso">
183+
&reftitle.seealso;
184+
<para>
185+
<simplelist>
186+
<member><function>gettype</function></member>
187+
<member><function>get_class</function></member>
188+
</simplelist>
189+
</para>
190+
</refsect1>
191+
</refentry>
192+
<!-- Keep this comment at the end of the file
193+
Local variables:
194+
mode: sgml
195+
sgml-omittag:t
196+
sgml-shorttag:t
197+
sgml-minimize-attributes:nil
198+
sgml-always-quote-attributes:t
199+
sgml-indent-step:1
200+
sgml-indent-data:t
201+
indent-tabs-mode:nil
202+
sgml-parent-document:nil
203+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
204+
sgml-exposed-tags:nil
205+
sgml-local-catalogs:nil
206+
sgml-local-ecat-files:nil
207+
End:
208+
vim600: syn=xml fen fdm=syntax fdl=2 si
209+
vim: et tw=78 syn=sgml
210+
vi: ts=1 sw=1
211+
-->

reference/var/functions/gettype.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<row>
9191
<entry>7.2.0</entry>
9292
<entry>
93-
Closed resources are now reported as <literal>'resource (closed)'</literal>.
93+
Closed resources are now reported as <literal>'resource (closed)'</literal>.
9494
Previously the returned value for closed resources were <literal>'unknown type'</literal>.
9595
</entry>
9696
</row>
@@ -136,6 +136,7 @@ string
136136
&reftitle.seealso;
137137
<para>
138138
<simplelist>
139+
<member><function>get_debug_type</function></member>
139140
<member><function>settype</function></member>
140141
<member><function>get_class</function></member>
141142
<member><function>is_array</function></member>

0 commit comments

Comments
 (0)