[doc-en] master: Document ::class on values that are not objects and on string literals (#6069)

From: Date: Thu, 12 Nov 2026 15:14:03 +0000
Subject: [doc-en] master: Document ::class on values that are not objects and on string literals (#6069)
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-23944@lists.php.net to get a copy of this message
Author: Asish Kumar (officialasishkumar) Committer: GitHub (web-flow) Pusher: lacatoire Date: 2026-11-12T16:14:00+01:00 Commit: https://github.com/php/doc-en/commit/f8ecb73c5ebab866fb4b67d7873709612a518827 Raw diff: https://github.com/php/doc-en/commit/f8ecb73c5ebab866fb4b67d7873709612a518827.diff Document ::class on values that are not objects and on string literals (#6069) * Document ::class on values that are not objects and on string literals The ::class section says that, as of PHP 8.0.0, ::class may be used on objects, but not what happens with anything else. $name::class throws a TypeError ("Cannot use "::class" on string") when $name holds a class name, while 'Name'::class works, which looks inconsistent (#1646). - Say that ::class on a value that is not an object throws a TypeError, including on a string holding a class name, and add an example. - Add a note that ::class on a string literal is resolved at compile time (zend_try_compile_const_expr_resolve_class_name()) and evaluates to the name in the string as a fully qualified name: it is not resolved against the current namespace, and a leading backslash is removed. Checked on PHP 8.3 and 8.4. Fixes #1646 * Show the pre-8.3 TypeError message in the ::class example Changed paths: M language/oop5/basic.xml Diff: diff --git a/language/oop5/basic.xml b/language/oop5/basic.xml index bdf64c7d676c..5ff70c006efa 100644 --- a/language/oop5/basic.xml +++ b/language/oop5/basic.xml @@ -789,6 +789,49 @@ NS\ClassName ]]> </screen> </example> + <simpara> + Using <literal>::class</literal> on a value that is not an object throws a + <exceptionname>TypeError</exceptionname>. This includes a + <type>string</type> holding a class name, which can be used directly + instead. + </simpara> + <example xml:id="language.oop5.basic.class.class.non-object"> + <title>Using <literal>::class</literal> on a string</title> + <programlisting role="php"> +<![CDATA[ +<?php +$className = 'stdClass'; + +try { + echo $className::class; +} catch (TypeError $e) { + echo $e->getMessage(); +} +?> +]]> + </programlisting> + &example.outputs.83; + <screen> +<![CDATA[ +Cannot use "::class" on string +]]> + </screen> + &example.outputs.82; + <screen> +<![CDATA[ +Cannot use "::class" on value of type string +]]> + </screen> + </example> + <note> + <simpara> + When used on a string literal, as in + <literal>'ClassName'::class</literal>, <literal>::class</literal> is + resolved at compile time, and evaluates to the class name in the string + taken as a fully qualified name: it is not resolved against the current + namespace, and a leading backslash is removed. + </simpara> + </note> </sect2> <sect2 xml:id="language.oop5.basic.nullsafe"> <title>Nullsafe methods and properties</title>

« previous php.doc.cvs (#23944) next »