How to Retrieve Class Name in PHPQuery?

Barbara Streisand
Release: 2024-10-19 21:20:30
Original
179 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!