Markus F
Markus F

Reputation: 96

PHP: Get name of the class the current function was defined in

In PHP I'd like to find out in a function in which class this function was defined in, even if it was inherited by another class.

Let me give you an example:

class cParent {
   function getDefinedInClassName() {
      return function_I_am_looking_for();
   }
}

class cChild1 extends cParent {
}

class cChild2 extends cParent {
   function getDefinedInClassName() {
      return parent::getDefinedInClassName();
   }
}

$objParent = new cParent();
$objParent->getDefinedInClassName(); // should return 'cParent'

$objChild1 = new cChild1();
$objChild1->getDefinedInClassName(); // should return 'cParent'

$objChild2 = new cChild2();
$objChild2->getDefinedInClassName(); // should return 'cChild2'

EDIT:

Just realized I had an error in the code. I have corrected it. Now it shows the actual problem. Sorry for the mistake!

Upvotes: 0

Views: 98

Answers (1)

Pavel
Pavel

Reputation: 936

http://php.net/manual/function.get-class.php I think you should try this function.

get_class($this) 

will return the name of the current class.

Upvotes: 1

Related Questions