PHP:获取当前函数定义的类的名称

时间:2017-03-30 13:24:02

标签: php oop inheritance

在PHP中,我想在函数中找到定义此函数的函数,即使它是由另一个类继承的。

让我举个例子:

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'

修改

刚刚意识到我的代码中有错误。我纠正了它。现在它显示了实际问题。抱歉错误!

1 个答案:

答案 0 :(得分:1)

http://php.net/manual/function.get-class.php我认为你应该尝试这个功能。

get_class($this) 

将返回当前类的名称。

相关问题