[DOC-CVS] [doc-en] master: Add information about type declarations to Class Constants (#4545)
[email protected] (Kevin Boyd via GitHub)
| Newsgroups | php.doc.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: Kevin Boyd (beryllium)
Committer: GitHub (web-flow)
Pusher: tiffany-taylor
Date: 2025-04-15T22:54:39-05:00
Commit: https://github.com/php/doc-en/commit/922b4b5aeb327d78ea1bb4b932e5db2e9a03ffc5
Raw diff: https://github.com/php/doc-en/commit/922b4b5aeb327d78ea1bb4b932e5db2e9a03ffc5.diff
Add information about type declarations to Class Constants (#4545)
* Add information about type declarations to Class Constants
* Update constants.xml to add missing screen closing tag
Changed paths:
M language/oop5/constants.xml
Diff:
diff --git a/language/oop5/constants.xml b/language/oop5/constants.xml
index 1b3fff078e58..cf4ac7b47e41 100644
--- a/language/oop5/constants.xml
+++ b/language/oop5/constants.xml
@@ -28,6 +28,12 @@
Note that class constants are allocated once per class, and not for each
class instance.
</para>
+ <para>
+ As of PHP 8.3.0, class constants can have a scalar type such as <literal>bool</literal>,
+ <literal>int</literal>, <literal>float</literal>, <literal>string</literal>, or even
+ <literal>array</literal>. When using <literal>array</literal>, the contents can only be
+ other scalar types.
+ </para>
<example>
<title>Defining and using a constant</title>
<programlisting role="php">
@@ -174,6 +180,48 @@ echo Foo::{$name}, PHP_EOL; // bar
variable.
</para>
</note>
+ <example>
+ <title>Assigning types to class constants, as of PHP 8.3.0</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+
+class MyClass {
+ public const bool MY_BOOL = true;
+ public const int MY_INT = 1;
+ public const float MY_FLOAT = 1.01;
+ public const string MY_STRING = 'one';
+ public const array MY_ARRAY = [self::MY_BOOL, self::MY_INT, self::MY_FLOAT, self::MY_STRING];
+}
+
+var_dump(MyClass::MY_BOOL);
+var_dump(MyClass::MY_INT);
+var_dump(MyClass::MY_FLOAT);
+var_dump(MyClass::MY_STRING);
+var_dump(MyClass::MY_ARRAY);
+?>
+]]>
+ </programlisting>
+ &example.outputs.83;
+ <screen>
+<![CDATA[
+bool(true)
+int(1)
+float(1.01)
+string(3) "one"
+array(4) {
+ [0]=>
+ bool(true)
+ [1]=>
+ int(1)
+ [2]=>
+ float(1.01)
+ [3]=>
+ string(3) "one"
+}
+ ]]>
+ </screen>
+ </example>
</sect1>
<!-- Keep this comment at the end of the file
Local variables: