mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-18 18:08:54 +00:00

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@257841 c90b9560-bf6c-de11-be94-00142212c4b1
95 lines
2.1 KiB
XML
95 lines
2.1 KiB
XML
<?xml version="1.0" encoding="iso-8859-1"?>
|
|
<!-- $Revision: 1.16 $ -->
|
|
<sect1 xml:id="language.oop5.constants" xmlns="http://docbook.org/ns/docbook">
|
|
<title>Class Constants</title>
|
|
<para>
|
|
It is possible to define constant values on a per-class basis remaining the
|
|
same and unchangeable. Constants differ from normal variables in that you
|
|
don't use the <varname>$</varname> symbol to declare or use them.
|
|
</para>
|
|
<para>
|
|
The value must be a constant expression, not (for example) a variable, a
|
|
class member, result of a mathematical operation or a function call.
|
|
</para>
|
|
|
|
<para>
|
|
As of PHP 5.3.0, 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>
|
|
|
|
<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"; // As of PHP 5.3.0
|
|
|
|
$class = new MyClass();
|
|
$class->showConstant();
|
|
|
|
echo $class::constant."\n"; // As of PHP 5.3.0
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
|
|
<example>
|
|
<title>Static data example</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class foo {
|
|
// As of PHP 5.3.0
|
|
const bar = <<<'EOT'
|
|
bar
|
|
EOT;
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
Unlike heredocs, nowdocs can be used in any static data context.
|
|
</para>
|
|
</example>
|
|
|
|
<note>
|
|
<para>
|
|
Nowdoc support was added in PHP 5.3.0.
|
|
</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:"../../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
|
|
-->
|