2017-01-07 09:30:22 +00:00
|
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:17:58 +00:00
|
|
|
|
<!-- $Revision$ -->
|
2015-05-14 08:47:12 +00:00
|
|
|
|
<sect1 xml:id="language.oop5.constants" xmlns="http://docbook.org/ns/docbook">
|
|
|
|
|
<title>Class Constants</title>
|
|
|
|
|
<para>
|
2020-12-15 01:40:52 +00:00
|
|
|
|
It is possible to define <link linkend="language.constants">constants</link>
|
|
|
|
|
on a per-class basis remaining the same and unchangeable.
|
2016-08-09 11:13:18 +00:00
|
|
|
|
The default visibility of class constants is <literal>public</literal>.
|
2015-05-14 08:47:12 +00:00
|
|
|
|
</para>
|
2020-12-15 01:40:52 +00:00
|
|
|
|
<note>
|
|
|
|
|
<para>
|
|
|
|
|
Class constants can be redefined by a child class.
|
|
|
|
|
</para>
|
|
|
|
|
</note>
|
2015-05-14 08:47:12 +00:00
|
|
|
|
<para>
|
2020-12-15 01:40:52 +00:00
|
|
|
|
It's also possible for interfaces to have <literal>constants</literal>. Look
|
|
|
|
|
at the <link linkend="language.oop5.interfaces">interface documentation</link>
|
|
|
|
|
for examples.
|
2015-05-14 08:47:12 +00:00
|
|
|
|
</para>
|
|
|
|
|
<para>
|
2020-12-15 01:40:52 +00:00
|
|
|
|
It's possible to reference the class using a variable.
|
2015-05-14 08:47:12 +00:00
|
|
|
|
The variable's value can not be a keyword (e.g. <literal>self</literal>,
|
|
|
|
|
<literal>parent</literal> and <literal>static</literal>).
|
|
|
|
|
</para>
|
2016-08-09 11:32:38 +00:00
|
|
|
|
<para>
|
|
|
|
|
Note that class constants are allocated once per class, and not for each
|
|
|
|
|
class instance.
|
|
|
|
|
</para>
|
2015-05-14 08:47:12 +00:00
|
|
|
|
<example>
|
|
|
|
|
<title>Defining and using a constant</title>
|
|
|
|
|
<programlisting role="php">
|
2004-07-19 04:52:41 +00:00
|
|
|
|
<![CDATA[
|
|
|
|
|
<?php
|
2004-10-02 09:40:52 +00:00
|
|
|
|
class MyClass
|
|
|
|
|
{
|
2013-07-14 17:20:52 +00:00
|
|
|
|
const CONSTANT = 'constant value';
|
2004-07-19 04:52:41 +00:00
|
|
|
|
|
2004-10-02 09:40:52 +00:00
|
|
|
|
function showConstant() {
|
2013-07-14 17:20:52 +00:00
|
|
|
|
echo self::CONSTANT . "\n";
|
2004-10-02 09:40:52 +00:00
|
|
|
|
}
|
2004-07-19 04:52:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-14 17:20:52 +00:00
|
|
|
|
echo MyClass::CONSTANT . "\n";
|
2004-07-19 04:52:41 +00:00
|
|
|
|
|
2007-10-21 15:57:09 +00:00
|
|
|
|
$classname = "MyClass";
|
2020-12-15 01:40:52 +00:00
|
|
|
|
echo $classname::CONSTANT . "\n";
|
2007-10-21 15:57:09 +00:00
|
|
|
|
|
2004-07-19 04:52:41 +00:00
|
|
|
|
$class = new MyClass();
|
|
|
|
|
$class->showConstant();
|
2007-10-21 15:57:09 +00:00
|
|
|
|
|
2020-12-15 01:40:52 +00:00
|
|
|
|
echo $class::CONSTANT."\n";
|
2004-07-19 04:52:41 +00:00
|
|
|
|
?>
|
|
|
|
|
]]>
|
2015-05-14 08:47:12 +00:00
|
|
|
|
</programlisting>
|
|
|
|
|
</example>
|
2017-01-07 09:30:22 +00:00
|
|
|
|
<para>
|
2020-12-15 01:40:52 +00:00
|
|
|
|
The special <constant>::class</constant> constant allows
|
|
|
|
|
for fully qualified class name resolution at compile time,
|
|
|
|
|
this is useful for namespaced classes:
|
2017-01-07 09:30:22 +00:00
|
|
|
|
</para>
|
|
|
|
|
<example>
|
2017-01-08 15:53:43 +00:00
|
|
|
|
<title>Namespaced ::class example</title>
|
2017-01-07 09:30:22 +00:00
|
|
|
|
<programlisting role="php">
|
|
|
|
|
<![CDATA[
|
|
|
|
|
<?php
|
|
|
|
|
namespace foo {
|
|
|
|
|
class bar {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo bar::class; // foo\bar
|
|
|
|
|
}
|
|
|
|
|
?>
|
|
|
|
|
]]>
|
|
|
|
|
</programlisting>
|
|
|
|
|
</example>
|
2020-12-15 01:40:52 +00:00
|
|
|
|
|
2015-05-14 08:47:12 +00:00
|
|
|
|
<example>
|
2020-12-15 01:40:52 +00:00
|
|
|
|
<title>Class constant expression example</title>
|
2015-05-14 08:27:16 +00:00
|
|
|
|
<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;
|
|
|
|
|
}
|
|
|
|
|
?>
|
|
|
|
|
]]>
|
2015-05-14 08:47:12 +00:00
|
|
|
|
</programlisting>
|
|
|
|
|
</example>
|
2016-08-09 11:13:18 +00:00
|
|
|
|
|
|
|
|
|
<example>
|
2020-12-15 01:40:52 +00:00
|
|
|
|
<title>Class constant visibility modifiers, as of PHP 7.1.0</title>
|
2016-08-09 11:13:18 +00:00
|
|
|
|
<programlisting role="php">
|
|
|
|
|
<![CDATA[
|
|
|
|
|
<?php
|
|
|
|
|
class Foo {
|
|
|
|
|
public const BAR = 'bar';
|
|
|
|
|
private const BAZ = 'baz';
|
|
|
|
|
}
|
|
|
|
|
echo Foo::BAR, PHP_EOL;
|
2016-08-31 17:09:50 +00:00
|
|
|
|
echo Foo::BAZ, PHP_EOL;
|
2016-08-09 11:13:18 +00:00
|
|
|
|
?>
|
|
|
|
|
]]>
|
|
|
|
|
</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>
|
2015-05-14 08:47:12 +00:00
|
|
|
|
</sect1>
|
2004-07-10 19:30:37 +00:00
|
|
|
|
<!-- 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
|
2009-09-25 07:04:39 +00:00
|
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2004-07-10 19:30:37 +00:00
|
|
|
|
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
|
|
|
|
|
-->
|