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
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="language.oop5.constants" xmlns="http://docbook.org/ns/docbook">
<title>Class Constants</title>
<para>
It is possible to define <link linkend="language.constants">constants</link>
on a per-class basis remaining the same and unchangeable.
The default visibility of class constants is <literal>public</literal>.
</para>
<note>
<para>
Class constants can be redefined by a child class.
As of PHP 8.1.0, class constants cannot be redefined by a child class
if it is defined as <link linkend="language.oop5.final">final</link>.
</para>
</note>
<para>
It's also possible for interfaces to have constants. Look
at the <link linkend="language.oop5.interfaces">interface documentation</link>
for examples.
</para>
<para>
It's possible to reference the class using a variable.
The variable's value can not be a keyword (e.g. <literal>self</literal>,
<literal>parent</literal> and <literal>static</literal>).
</para>
<para>
Note that class constants are allocated once per class, and not for each
class instance.
</para>
<example>
<title>Defining and using a constant</title>
<programlisting role="php">
<![CDATA[
<?php
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "\n";
}
}
echo MyClass::CONSTANT . "\n";
$classname = "MyClass";
echo $classname::CONSTANT . "\n";
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT."\n";
?>
]]>
</programlisting>
</example>
<para>
The special <constant>::class</constant> constant allows
for fully qualified class name resolution at compile time,
this is useful for namespaced classes:
</para>
<example>
<title>Namespaced ::class example</title>
<programlisting role="php">
<![CDATA[
<?php
namespace foo {
class bar {
}
echo bar::class; // foo\bar
}
?>
]]>
</programlisting>
</example>
<example>
<title>Class constant expression example</title>
<programlisting role="php">
<![CDATA[
<?php
const ONE = 1;
class foo {
const TWO = ONE * 2;
const THREE = ONE + self::TWO;
const SENTENCE = 'The value of THREE is '.self::THREE;
}
?>
]]>
</programlisting>
</example>
<example>
<title>Class constant visibility modifiers, as of PHP 7.1.0</title>
<programlisting role="php">
<![CDATA[
<?php
class Foo {
public const BAR = 'bar';
private const BAZ = 'baz';
}
echo Foo::BAR, PHP_EOL;
echo Foo::BAZ, PHP_EOL;
?>
]]>
</programlisting>
&example.outputs.71;
<screen>
<![CDATA[
bar
Fatal error: Uncaught Error: Cannot access private const Foo::BAZ in …
]]>
</screen>
</example>
<note>
<para>
As of PHP 7.1.0 visibility modifiers are allowed for class constants.
</para>
</note>
<example>
<title>Class constant visibility variance check, as of PHP 8.3.0</title>
<programlisting role="php">
<![CDATA[
<?php
interface MyInterface
{
public const VALUE = 42;
}
class MyClass implements MyInterface
{
protected const VALUE = 42;
}
?>
]]>
</programlisting>
&example.outputs.83;
<screen>
<![CDATA[
Fatal error: Access level to MyClass::VALUE must be public (as in interface MyInterface) …
]]>
</screen>
</example>
<note>
<simpara>
As of PHP 8.3.0 visibility variance is checked more strictly.
Prior to this version, the visibility of a class constant could be different
from the visibility of the constant in the implemented interface.
</simpara>
</note>
<example>
<title>Fetch class constant syntax, as of PHP 8.3.0</title>
<programlisting role="php">
<![CDATA[
<?php
class Foo {
public const BAR = 'bar';
private const BAZ = 'baz';
}
$name = 'BAR';
echo Foo::{$name}, PHP_EOL; // bar
?>
]]>
</programlisting>
</example>
<note>
<para>
As of PHP 8.3.0, class constants can be fetched dynamically using a
variable.
</para>
</note>
</sect1>
<!-- 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
-->
|