Home >Backend Development >PHP Tutorial >How to Retrieve Class Name in PHPQuery?
Query:
In PHP, how do we retrieve the name of a class?
Response:
PHP 5.5 and Later:
With PHP 5.5 and subsequent versions, use the ClassName::class syntax. This enables static class name resolution.
<code class="php">namespace Name\Space; class ClassName {} echo ClassName::class;</code>
In class methods, use static::class to access the class name:
<code class="php">namespace Name\Space; class ClassName { public function getNameOfClass() { return static::class; } } $obj = new ClassName(); echo $obj->getNameOfClass();</code>
Pre-PHP 5.5:
For PHP versions prior to 5.5, utilize the get_class() function, which functions with objects:
<code class="php">get_class($object);</code>
Note that get_class() returns the full namespace of the class, while ClassName::class returns sadece the class name itself.
The above is the detailed content of How to Retrieve Class Name in PHPQuery?. For more information, please follow other related articles on the PHP Chinese website!