::class On Object¶
The ::class operator provides the fully qualified name of the identifier or object. It used to be working only on identifier or names, but it also works on objects, via variables and properties: then, it provides the fully qualified name of the underlying class.
This is very convenient when the code needs to get a hold on the class, and only the object is provided.
PHP code¶
<?php
$a = new stdclass;
echo $a::class;
Before¶
PHP Fatal error: Cannot use ::class with dynamic class name in /codes/ClassOnIdentifier.php on line 4
Fatal error: Cannot use ::class with dynamic class name in /codes/ClassOnIdentifier.php on line 4
After¶
stdClass
PHP version change¶
This behavior changed in 8.0