Document the ::class constant on objects

Also, improve ::class description based on comments.

Closes GH-239.


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@351711 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Christoph Michael Becker 2020-11-27 23:40:12 +00:00
parent 74abd95c4e
commit 370bb99d28

View file

@ -413,7 +413,7 @@ a default value
<title>::class</title>
<para>
Since PHP 5.5, the <literal>class</literal> keyword is also used for class
The <literal>class</literal> keyword is also used for class
name resolution. You can get a string containing the fully qualified name
of the <literal>ClassName</literal> class by using
<literal>ClassName::class</literal>. This is particularly useful with
@ -449,7 +449,49 @@ NS\ClassName
are expanded even if the class does not exist. No error is issued in
that case.
</para>
<example xml:id="language.oop5.basic.class.class.fail">
<title>Missing class name resolution</title>
<programlisting role="php">
<![CDATA[
<?php
print Some\Class\DoesNot\Exist::class;
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Some\Class\Does\Not\Exist
]]>
</screen>
</example>
</note>
<para>
As of PHP 8.0.0, the <literal>::class</literal> constant may also be used on
objects. This resolution happens at runtime, not compile time. Its effect is
the same as calling <function>get_class</function> on the object.
</para>
<example xml:id="language.oop5.basic.class.class.object">
<title>Object name resolution</title>
<programlisting role="php">
<![CDATA[
<?php
namespace NS {
class ClassName {
}
}
$c = new ClassName();
print $c::class;
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
NS\ClassName
]]>
</screen>
</example>
</sect2>
</sect1>