reference/misc/functions/defined.xml
36e1d917ef7be36e8b4ff5193b456390061f2e21
...
...
@@ -3,23 +3,28 @@
3
3
<refentry xml:id="function.defined" xmlns="http://docbook.org/ns/docbook">
4
4
<refnamediv>
5
5
<refname>defined</refname>
6
-
<refpurpose>Checks whether a given named constant exists</refpurpose>
6
+
<refpurpose>Checks whether a constant with the given name exists</refpurpose>
7
7
</refnamediv>
8
8
9
9
<refsect1 role="description">
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
12
<type>bool</type><methodname>defined</methodname>
13
-
<methodparam><type>string</type><parameter>name</parameter></methodparam>
13
+
<methodparam><type>string</type><parameter>constant_name</parameter></methodparam>
14
14
</methodsynopsis>
15
15
<para>
16
-
Checks whether the given constant exists and is defined.
16
+
Checks whether a constant with the given <parameter>constant_name</parameter>
17
+
is defined.
18
+
</para>
19
+
<para>
20
+
This function works also with <link
21
+
linkend="language.oop5.constants">class constants</link> and <link
22
+
linkend="language.types.enumerations">enum cases</link>.
17
23
</para>
18
24
<note>
19
25
<para>
20
26
If you want to see if a variable exists, use <function>isset</function>
21
-
as <function>defined</function> only applies to <link
22
-
linkend="language.constants">constants</link>. If you want to see if a
27
+
as <function>defined</function> only applies to <link linkend="language.constants">constants</link>. If you want to see if a
23
28
function exists, use <function>function_exists</function>.
24
29
</para>
25
30
</note>
...
...
@@ -30,7 +35,7 @@
30
35
<para>
31
36
<variablelist>
32
37
<varlistentry>
33
-
<term><parameter>name</parameter></term>
38
+
<term><parameter>constant_name</parameter></term>
34
39
<listitem>
35
40
<para>
36
41
The constant name.
...
...
@@ -44,7 +49,7 @@
44
49
<refsect1 role="returnvalues">
45
50
&reftitle.returnvalues;
46
51
<para>
47
-
Returns &true; if the named constant given by <parameter>name</parameter>
52
+
Returns &true; if the named constant given by <parameter>constant_name</parameter>
48
53
has been defined, &false; otherwise.
49
54
</para>
50
55
</refsect1>
...
...
@@ -57,11 +62,45 @@
57
62
<programlisting role="php">
58
63
<![CDATA[
59
64
<?php
60
-
/* Note the use of quotes, this is important. This example is checking
65
+

66
+
/* Note the use of quotes, this is important. This example is checking
61
67
* if the string 'TEST' is the name of a constant named TEST */
62
68
if (defined('TEST')) {
63
69
echo TEST;
64
70
}
71
+

72
+

73
+
interface bar {
74
+
const test = 'foobar!';
75
+
}
76
+

77
+
class foo {
78
+
const test = 'foobar!';
79
+
}
80
+

81
+
var_dump(defined('bar::test')); // bool(true)
82
+
var_dump(defined('foo::test')); // bool(true)
83
+

84
+
?>
85
+
]]>
86
+
</programlisting>
87
+
</example>
88
+
<example>
89
+
<title>Checking Enum Cases (as of PHP 8.1.0)</title>
90
+
<programlisting role="php">
91
+
<![CDATA[
92
+
<?php
93
+

94
+
enum Suit
95
+
{
96
+
case Hearts;
97
+
case Diamonds;
98
+
case Clubs;
99
+
case Spades;
100
+
}
101
+

102
+
var_dump(defined('Suit::Hearts')); // bool(true)
103
+

65
104
?>
66
105
]]>
67
106
</programlisting>
...
...
@@ -83,7 +122,6 @@ if (defined('TEST')) {
83
122
</refsect1>
84
123

85
124
</refentry>
86
-

87
125
<!-- Keep this comment at the end of the file
88
126
Local variables:
89
127
mode: sgml
90
128