Skip to content

Commit 02d204d

Browse files
author
cmb
committed
Fix #77100: Inconsistent behavior between constants and magic constants
git-svn-id: http://svn.php.net/repository/phpdoc/en@345954 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent 9b431c7 commit 02d204d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

trunk/language/constants.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
expression, it would be expressed thusly:
1919
<code>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</code>
2020
</para>
21+
<para>
22+
It is possible to <function>define</function> constants with reserved or even
23+
invalid names, whose value can (only) be retrieved with
24+
<function>constant</function>. However, doing so is not recommended.
25+
</para>
2126
&tip.userlandnaming;
2227
<para>
2328
<example>

trunk/reference/misc/functions/define.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
<para>
3030
The name of the constant.
3131
</para>
32+
<note>
33+
<para>
34+
It is possible to <function>define</function> constants with reserved or
35+
even invalid names, whose value can (only) be retrieved with
36+
<function>constant</function>. However, doing so is not recommended.
37+
</para>
38+
</note>
3239
</listitem>
3340
</varlistentry>
3441
<varlistentry>
@@ -139,6 +146,37 @@ echo ANIMALS[1]; // outputs "cat"
139146
</programlisting>
140147
</example>
141148
</para>
149+
<para>
150+
<example>
151+
<title>Constants with Reserved Names</title>
152+
<para>
153+
This example illustrates the <emphasis>possibility</emphasis> to define a
154+
constant with the same name as a
155+
<link linkend="language.constants.predefined">magic constant</link>.
156+
Since the resulting behavior is obviously confusing, it is not recommended
157+
to do this in practise, though.
158+
</para>
159+
<programlisting role="php">
160+
<![CDATA[
161+
<?php
162+
var_dump(defined('__LINE__'));
163+
var_dump(define('__LINE__', 'test'));
164+
var_dump(constant('__LINE__'));
165+
var_dump(__LINE__);
166+
?>
167+
]]>
168+
</programlisting>
169+
&example.outputs;
170+
<screen>
171+
<![CDATA[
172+
bool(false)
173+
bool(true)
174+
string(4) "test"
175+
int(5)
176+
]]>
177+
</screen>
178+
</example>
179+
</para>
142180
</refsect1>
143181

144182
<refsect1 role="seealso">

0 commit comments

Comments
 (0)