1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="language.constants" xmlns="http://docbook.org/ns/docbook">
<title>Constants</title>
<simpara>
A constant is an identifier (name) for a simple value. As the name
suggests, that value cannot change during the execution of the
script (except for <link linkend="language.constants.magic">
magic constants</link>, which aren't actually constants).
Constants are case-sensitive. By convention, constant
identifiers are always uppercase.
</simpara>
<note>
<para>
Prior to PHP 8.0.0, constants defined using the <function>define</function>
function may be case-insensitive.
</para>
</note>
<para>
The name of a constant follows the same rules as any label in PHP. A
valid constant name starts with a letter or underscore, followed
by any number of letters, numbers, or underscores. As a regular
expression, it would be expressed thusly:
<code>^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$</code>
</para>
<para>
It is possible to <function>define</function> constants with reserved or even
invalid names, whose value can only be retrieved with the
<function>constant</function> function. However, doing so is not recommended.
</para>
&tip.userlandnaming;
<para>
<!-- TODO Move into syntax section? -->
<example>
<title>Valid and invalid constant names</title>
<programlisting role="php">
<![CDATA[
<?php
// Valid constant names
define("FOO", "something");
define("FOO2", "something else");
define("FOO_BAR", "something more");
// Invalid constant names
define("2FOO", "something");
// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__", "something");
?>
]]>
</programlisting>
</example>
</para>
<note>
<simpara>
For our purposes here, a letter is a-z, A-Z, and the ASCII
characters from 128 through 255 (0x80-0xff).
</simpara>
</note>
<simpara>
Like &link.superglobals;, the scope of a constant is global.
Constants can be accessed from anywhere in a script without regard to scope.
For more information on scope, read the manual section on
<link linkend="language.variables.scope">variable scope</link>.
</simpara>
<note>
<simpara>
As of PHP 7.1.0, class constant may declare a visibility of protected
or private, making them only available in the hierarchical scope of the
class in which it is defined.
</simpara>
</note>
<sect1 xml:id="language.constants.syntax">
<title>Syntax</title>
<simpara>
Constants can be defined using the <literal>const</literal> keyword,
or by using the <function>define</function>-function.
While <function>define</function> allows a constant to be
defined to an arbitrary expression, the <literal>const</literal> keyword has
restrictions as outlined in the next paragraph.
Once a constant is defined, it can never be
changed or undefined.
</simpara>
<simpara>
When using the <literal>const</literal> keyword,
only scalar (<type>bool</type>, <type>int</type>,
<type>float</type> and <type>string</type>) expressions and constant
<type>array</type>s containing only scalar expressions are accepted.
It is possible to define constants as a <type>resource</type>,
but it should be avoided, as it can cause unexpected results.
</simpara>
<simpara>
The value of a constant is accessed simply by specifying its name.
Unlike variables, a constant is <emphasis>not</emphasis> prepended
with a <literal>$</literal>.
It is also possible to use the <function>constant</function> function to
read a constant's value if the constant's name is obtained dynamically.
Use <function>get_defined_constants</function> to get a list of
all defined constants.
</simpara>
<note>
<simpara>
Constants and (global) variables are in a different namespace.
This implies that for example &true; and
<varname>$TRUE</varname> are generally different.
</simpara>
</note>
<simpara>
If an undefined constant is used an <classname>Error</classname> is thrown.
Prior to PHP 8.0.0, undefined constants would be interpreted as a bare
word <type>string</type>, i.e. (CONSTANT vs "CONSTANT").
This fallback is deprecated as of PHP 7.2.0, and an error of level
<constant>E_WARNING</constant> is issued when it happens.
Prior to PHP 7.2.0, an error of level
<link linkend="ref.errorfunc">E_NOTICE</link> has been issued instead.
See also the manual entry on why
<link linkend="language.types.array.foo-bar">$foo[bar]</link> is
wrong (unless <literal>bar</literal> is a constant).
This does not apply to <link
linkend="language.namespaces.rules">(fully) qualified constants</link>,
which will always raise a <classname>Error</classname> if undefined.
</simpara>
<note>
<simpara>
To check if a constant is set, use the <function>defined</function> function.
</simpara>
</note>
<para>
These are the differences between constants and variables:
<itemizedlist>
<listitem>
<simpara>
Constants do not have a dollar sign (<literal>$</literal>)
before them;
</simpara>
</listitem>
<listitem>
<simpara>
Constants may be defined and accessed anywhere without regard
to variable scoping rules;
</simpara>
</listitem>
<listitem>
<simpara>
Constants may not be redefined or undefined once they have been
set; and
</simpara>
</listitem>
<listitem>
<simpara>
Constants may only evaluate to scalar values or arrays.
</simpara>
</listitem>
</itemizedlist>
</para>
<para>
<example>
<title>Defining Constants</title>
<programlisting role="php">
<![CDATA[
<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // Emits an Error: Undefined constant "Constant"
// Prior to PHP 8.0.0, outputs "Constant" and issues a warning.
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Defining Constants using the <literal>const</literal> keyword</title>
<programlisting role="php">
<![CDATA[
<?php
// Simple scalar value
const CONSTANT = 'Hello World';
echo CONSTANT;
// Scalar expression
const ANOTHER_CONST = CONSTANT.'; Goodbye World';
echo ANOTHER_CONST;
const ANIMALS = array('dog', 'cat', 'bird');
echo ANIMALS[1]; // outputs "cat"
// Constant arrays
define('ANIMALS', array(
'dog',
'cat',
'bird'
));
echo ANIMALS[1]; // outputs "cat"
?>
]]>
</programlisting>
</example>
</para>
<note>
<para>
As opposed to defining constants using <function>define</function>,
constants defined using the <literal>const</literal> keyword must be
declared at the top-level scope because they are defined at compile-time.
This means that they cannot be declared inside functions, loops,
<literal>if</literal> statements or
<literal>try</literal>/<literal>catch</literal> blocks.
</para>
</note>
<sect2 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.oop5.constants">Class Constants</link></member>
</simplelist>
</para>
</sect2>
</sect1>
<sect1 xml:id="language.constants.predefined">
<title>Predefined constants</title>
<simpara>
PHP provides a large number of <link
linkend="reserved.constants">predefined constants</link> to any script
which it runs. Many of these constants, however, are created by
various extensions, and will only be present when those extensions
are available, either via dynamic loading or because they have
been compiled in.
</simpara>
</sect1>
<sect1 xml:id="language.constants.magic">
<title>Magic constants</title>
<para>
There are a few magical constants that change depending on
where they are used. For example, the value of
<constant>__LINE__</constant> depends on the line that it's
used on in a script. All these "magical" constants are resolved
at compile time, unlike regular constants, which are resolved at runtime.
These special constants are case-insensitive and are as follows:
</para>
<para>
<table>
<title>PHP's magic constants</title>
<tgroup cols="2">
<thead>
<row>
<entry>&Name;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row xml:id="constant.line">
<entry><constant>__LINE__</constant></entry>
<entry>
The current line number of the file.
</entry>
</row>
<row xml:id="constant.file">
<entry><constant>__FILE__</constant></entry>
<entry>
The full path and filename of the file with symlinks resolved. If used inside an include,
the name of the included file is returned.
</entry>
</row>
<row xml:id="constant.dir">
<entry><constant>__DIR__</constant></entry>
<entry>
The directory of the file. If used inside an include,
the directory of the included file is returned. This is equivalent
to <literal>dirname(__FILE__)</literal>. This directory name
does not have a trailing slash unless it is the root directory.
</entry>
</row>
<row xml:id="constant.function">
<entry><constant>__FUNCTION__</constant></entry>
<entry>
The function name, or <literal>{closure}</literal> for anonymous functions.
</entry>
</row>
<row xml:id="constant.class">
<entry><constant>__CLASS__</constant></entry>
<entry>
The class name. The class name includes the namespace
it was declared in (e.g. <literal>Foo\Bar</literal>).
When used inside a trait method,
<constant>__CLASS__</constant> is the name of the class the trait
is used in.
</entry>
</row>
<row xml:id="constant.trait">
<entry><constant>__TRAIT__</constant></entry>
<entry>
The trait name. The trait name includes the namespace
it was declared in (e.g. <literal>Foo\Bar</literal>).
</entry>
</row>
<row xml:id="constant.method">
<entry><constant>__METHOD__</constant></entry>
<entry>
The class method name.
</entry>
</row>
<row xml:id="constant.property">
<entry><constant>__PROPERTY__</constant></entry>
<entry>
Only valid inside a
<link linkend="language.oop5.property-hooks">property hook</link>.
It is equal to the name of the property.
</entry>
</row>
<row xml:id="constant.namespace">
<entry><constant>__NAMESPACE__</constant></entry>
<entry>
The name of the current namespace.
</entry>
</row>
<row xml:id="constant.coloncolonclass">
<entry><constant><replaceable>ClassName</replaceable>::class</constant></entry>
<entry>
The fully qualified class name.
</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<sect2 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.oop5.basic.class.class">::class</link></member>
<member><function>get_class</function></member>
<member><function>get_object_vars</function></member>
<member><function>file_exists</function></member>
<member><function>function_exists</function></member>
</simplelist>
</para>
</sect2>
</sect1>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|