简体   繁体   English

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

[英]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. 在PHP中,我想找出在哪个函数中定义了该函数的函数,即使该函数是由另一个类继承的也是如此。

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! 对不起,错误!

http://php.net/manual/function.get-class.php I think you should try this function. http://php.net/manual/function.get-class.php我认为您应该尝试此功能。

get_class($this) 

will return the name of the current class. 将返回当前类的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM