php-doc-en/language/oop5/constants.xml

142 lines
3.1 KiB
XML
Raw Normal View History

<?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.
</para>
</note>
<para>
It's also possible for interfaces to have <literal>constants</literal>. 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>
</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
-->