I've seen several threads where people ask how to get the name of a class or an object in PHP. However, I can't see anywhere the difference between the various possibilities explained. I hope somebody here can help me.
So in order to get the class name of the called class, I know two possibilities:
get_called_class()
static::class
(get_class($this)
for non-static classes)
And for getting the class name of the class in which you put the code, I know these three possibilities:
get_class()
__CLASS__
self::class
Are there any differences which I may to overlook right now? What are potential adventages and disadvantages of one way over the other?
::class ¶ The class keyword is also used for class name resolution. To obtain the fully qualified name of a class ClassName use ClassName::class . This is particularly useful with namespaced classes.
PHP | get_class() Function Return Value: This function returns the class name of which object is an instance. It returns FALSE if the object is not an object. If the object is omitted when inside the class then the class name is returned. $obj = new GFG();
Differences between
get_class()
returns the name of the class of an object
It returns a classname including its qualified namespace for the current class (without parameter) or for any specified object instance, when you pass the object instance pointer as the first and only parameter.
__CLASS__
A magic constant that returns the qualified namespace and the current class name. Here you can not test for class names of other objects. As per PHP 5.4 it works in traits. That is, when the trait is used in a class it will return the namespace and name of that class.
::class
Only available since PHP 5.5. It uses class name and namespace resolution to obtain the info, as a result it does not need the class to be instantiated beforehand. Also note:
The class name resolution using ::class is a compile time transformation. That means at the time the class name string is created no autoloading has happened yet. As a consequence, class names are expanded even if the class does not exist. No error is issued in that case.
Tests
<?php
namespace nTest;
trait tTest {
function __toString() {return get_class();}
function className() {return __CLASS__;} // per PHP 5.4
function traitName() {return __TRAIT__;}
}
class cTest {
use tTest;
function usedTraitName() {return __TRAIT__;}
}
class cClassWithoutObject {}
$oTest = new cTest;
header('Content-type: text/plain');
print // Output:
$oTest . PHP_EOL // 'nTest::cTest'
. get_class($oTest) . PHP_EOL // 'nTest::cTest'
. $oTest->className() . PHP_EOL // 'nTest::cTest'
. $oTest->traitName() . PHP_EOL // 'nTest::tTest' (trait!)
. $oTest->usedTraitName() . PHP_EOL // '' (no trait!)
. cTest::class . PHP_EOL // 'nTest::cTest'
. cClassWithoutObject::class; // 'nTest::cTestNotInstantiated'
One difference between __CLASS__
and get_class($this)
that's important if you're using inheritance is that __CLASS__
will give you the name of the class containing that code (which could be a parent class), whereas get_class($this)
will give you the name of the concrete class of $this
at runtime.
Example:
class Animal {
function printCompileTimeType() {
echo __CLASS__;
}
function printOverridableCompileTimeType() {
echo __CLASS__;
}
function printType() {
echo get_class($this);
}
}
class Dog extends Animal {
function printOverridableCompileTimeType() {
echo __CLASS__;
}
}
$animal = new Animal();
$dog = new Dog();
$animal->printCompileTimeType(); // Prints 'Animal'.
$animal->printOverridableCompileTimeType(); // Prints 'Animal'.
$animal->printType(); // Prints 'Animal'.
$dog->printCompileTimeType(); // Prints 'Animal'.
$dog->printOverridableCompileTimeType(); // Prints 'Dog'.
$dog->printType(); // Prints 'Dog'.
I'd say get_class($this)
is generally more useful because it will work predictably no matter where you use it from, without needing to write repetitive override code like printOverridableCompileTimeType
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With