get_class() Or ::class?

<?php

namespace abc;

class def {}

$x = new \ABC\DEF();
print get_class($x);
print PHP_EOL;
print $x::class;
print PHP_EOL;
print DEF::class;
print PHP_EOL;

Both get_class() and ::class do the same: they report the name of the class of the provided object. The nuances are in the details.

get_class() needs an object as argument, and emits a TypeError if not provided one. ::class works both on objects and class names. The latter are, basically, strings, so ::class work on strings.

get_class() needs the class to be available, while ::class merely formats the name of the class with the internal case: this works on an object, but not if the class name is hardcoded.

And who hardcodes its class names with strings?

See Also

PHP Features

Last updated: 14 July 2026