PHP __CLASS__ and get_class

Recently I was trying to test a small piece of PHP code and run into the magic constant __CLASS__ . I quote from the PHP manual (1)

The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. FooBar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.

So I thought that when I use __CLASS__ it will return me the class of the object that executes the code that includes the echo of __CLASS__. But as it turns out this is not true if __CLASS__ was echoed from within a method defined in a base class and inherited from a child class. As it turns out in that case __CLASS__ returns the name of the class that contains the defined method and not the name of the class that inherits the method and executes it. Consider the following example:

abstract class base {
function __construct() {}

function init() {
echo 'Base class (__CLASS__): ' . __CLASS__ . PHP_EOL;
echo 'Base class (get_class()): ' . get_class($this) . PHP_EOL;
echo PHP_EOL;
}
}

class derived extends base {
function init2() {
echo 'Derived class (__CLASS__): ' . __CLASS__ . PHP_EOL;
echo 'Derived class (get_class()): ' . get_class($this) . PHP_EOL;
echo PHP_EOL;
}

function init3() {
parent::init();
}
}

$a = new derived();

$a->init();
$a->init2();
$a->init3();

We have defined two classes one called Base and another called Derived that extends Base. The classes are used to demonstrate the use of the __CLASS__ magic constant and the get_class() php function. For the get_class() function quoting again from the php manual (2) we have

get_class — Returns the name of the class of an object

So, we implement three methods:

  1. one in the parent class called init() which prints the output of echoing both __CLASS__ and get_class() results from within the inherited method
  2. one in the child class called init2() which prints the output of echoing both __CLASS__ and get_class() results from within the child method
  3. one in the child class called init3() which specifically calls the parent init method from within the child method we just defined

Then we create an object from the child class (the parent class is defined as abstract and therefore cannot be used to instanciate any objects) and we call the three init functions on the same object. I originally expected that all three functions would return the same results that is derived and derived but as it turns out the __CLASS__ magic constant returns the name of the class the calling method was defined in no matter the calling object. The resuls are shown below

Base class (__CLASS__): base
Base class (get_class()): derived

Derived class (__CLASS__): derived
Derived class (get_class()): derived

Base class (__CLASS__): base
Base class (get_class()): derived

So, if at any point you wish to know the name of the class of your object then the get_class() function is the way to go.

(1) http://php.net/manual/en/language.constants.predefined.php

(2) http://php.net/manual/en/function.get-class.php

5 thoughts on “PHP __CLASS__ and get_class

  1. I was having the exact same issue with __CLASS__ returning the parent class name with namespaces. I needed to get the derived class’ name without namespaces, and your post helped me. Here is what I did:

    $classNameWithoutNS = join('', array_slice(explode('\', get_class($this)), -1))

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s