Is it possible to get the name of the current class from an uninstalled object in PHP?

I know that you can use get_class ($ this) normally, but I need to get the class name in a static function where the object was not created.

See the following code:

class ExampleClass
{
    static function getClassName()
    {
        echo get_class($this); // doesn't work unless the object is instantiated.
    }
}

$test1 = new ExampleClass();
$test1->getClassName(); // works

ExampleClass::getClassName(); // doesn't work
+5
source share
3 answers

I think you are looking for the get_called_class () function if you want to get the class name from a static method.

See the get_called_class documentation for more details .

+8
source

I realized that you can use __CLASS__ to get the class name. Example:

class ExampleClass
{
    static function getClassName()
    {
        echo __CLASS__;
    }
}
+3
source

: , ?

, , :

ExampleClass::getClassName(); //Hard Coded - the class name is hard and visible
$class = "ExampleClass";
$class::getClassName();       //Soft Coded - the class name is the value of $class

, , , ?

0

All Articles