get_class() Or ::class?

../_images/get_class_or_class.png

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