Home  >  Article  >  Backend Development  >  How to Retrieve Class Name in PHPQuery?

How to Retrieve Class Name in PHPQuery?

Barbara Streisand
Barbara StreisandOriginal
2024-10-19 21:20:30180browse

How to Retrieve Class Name in PHPQuery?

Class Name Retrieval in PHP

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn