Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rid3638's avatar

How to get class name?

I have a class like this

$answer = Answer::create([ ... .. ]);

I want to get the name of this class as string.

0 likes
2 replies
JussiMannisto's avatar

These return the qualified class name:

$className = Answer::class;
$className = get_class($answer);

If you just want the name of the class without the namespace part, you can either strip the namespace manually or use reflection:

$shortClassName = (new ReflectionClass($answer))->getShortName();
2 likes

Please or to participate in this conversation.