Php 如何从被调用的方法中获取类名?
我有一系列使用大量方法重载的继承类,我需要一种方法来找出调用特定方法时使用的类 简单的例子:Php 如何从被调用的方法中获取类名?,php,oop,Php,Oop,我有一系列使用大量方法重载的继承类,我需要一种方法来找出调用特定方法时使用的类 简单的例子: class child extends parent public function _process() { $this->_otherrun(); } public function _run() class parent extends grandparent public function _run() publi
class child extends parent
public function _process()
{
$this->_otherrun();
}
public function _run()
class parent extends grandparent
public function _run()
public function _otherrun()
{
// I need to find out which class the version of _run is called in
$this->_run();
}
与我正在查看的示例相比,这个示例非常简单,因此我需要一种方法来查看在哪个类中处理函数。这可能吗
大概是这样的:
get_class(array($this, 'run'));
您可以使用函数或函数:
get\u调用\u class()
示例:
class foo {
static public function test() {
var_dump(get_called_class());
}
}
debug\u backtrace()
示例(代码部分):
另外,使用搜索。结果之一:
更新(注释):使用函数
就你而言:
class parent extends grandparent
public function _run()
public function _otherrun()
{
// I need to find out which class the version of _run is called in
$this->_run();
var_dump(debug_backtrace());
}
唯一的问题是,我不知道该函数调用的是哪个类,否则我可能已经知道该类了。@FrancisLewis再次更新了它。这些例子都是来源。如果你需要,你可以做得更好。现在要睡觉了!再见:)祝你工作顺利。这不只是一个修改版本吗:很明显,你不需要在构造中使用echo,而“get_table_name_protected”将更改为“_run”。但是是的。。。看起来像你的目的,除非我搞砸了什么。
Hi: friend
array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}
class parent extends grandparent
public function _run()
public function _otherrun()
{
// I need to find out which class the version of _run is called in
$this->_run();
var_dump(debug_backtrace());
}