PHP查找当前对象($this)和方法的祖先

PHP查找当前对象($this)和方法的祖先,php,class,oop,Php,Class,Oop,在OOP中的任何给定上下文中,$this引用当前类。但是,如果当前类扩展了前一个类,而前一个类又扩展了前一个类等等,那么跟踪它可能会有点困难。我这么说是因为我用的是Magento,它是一个大脑担架 我的目标是追踪这些信息,然后有希望得到这些信息 a) 每个父类在系统中的位置,以及 b) 每个类的方法 我该怎么做?我的想法是,我也可以使用get_included_files()并在核心类中创建一个方法,如果调用该方法,将告诉我任何给定的方法在哪个文件中的位置(权威性地)以及扩展它的“最后一个方法实

在OOP中的任何给定上下文中,
$this
引用当前类。但是,如果当前类扩展了前一个类,而前一个类又扩展了前一个类等等,那么跟踪它可能会有点困难。我这么说是因为我用的是Magento,它是一个大脑担架

我的目标是追踪这些信息,然后有希望得到这些信息 a) 每个父类在系统中的位置,以及 b) 每个类的方法

我该怎么做?我的想法是,我也可以使用get_included_files()并在核心类中创建一个方法,如果调用该方法,将告诉我任何给定的方法在哪个文件中的位置(权威性地)以及扩展它的“最后一个方法实例”

我将很高兴发布在这个线程上创建的工具

所以如果你有这个密码

function myFirstMethod(){
    //call this tool
    Mage::log($tools->trace('someOtherMethod'));
    //this was already here
    $this->someOtherMethod();  //where is it? has it been extended? That's what I want to determine
}

以下代码段使用和向上移动实例的继承树:

<?php

class A {}
class B extends A {}
class C extends B {
  function trace() {
    $cls = get_class($this);

    while ($cls !== FALSE) {
      echo $cls. "\n\t with methods: " . implode(', ', get_class_methods($cls))  . "\n";
      $reflectionClass = new ReflectionClass($cls);
      echo "\t" . $reflectionClass->getFileName() . "\n";

      $cls = get_parent_class($cls);
    }
  }
}

(new C())->trace();

感谢这里涉及的所有人,我希望这能为一个好的跟踪工具(如果尚未开发)提供种子:

函数跟踪(){
//你可以修改这个,这是硬编码的
$this->_root='/home/vagrant/commoncart/';
$cls=get_类($this);
$i=0;
$methodMap=array();
$classMap=array();
//我们正在向上提升
而($cls!==FALSE){
$i++;
$reflectionClass=新的reflectionClass($cls);
$file=$reflectionClass->getFileName();
$classMap[$cls]=$file;
if($a=get_class_methods($cls))foreach($a as$method){
如果(!empty($methodMap[$method])){
$methodMap[$method]['override']=true;
$methodMap[$method]['Overrided'][]=$cls//
}否则{
$methodMap[$method]=数组(
“类”=>$cls,
“文件”=>str\u replace($this->\u root,,$file),
'line'=>'undefined',//如果有,那就太好了,可能需要读取文件并对其进行一次解析。。
);
}
}
$cls=获取父类($cls);
如果($i>5)中断;
}
回声';
打印($classMap);
打印(方法图);
退出(“完成”);
}

您可以使用
获取父类()
吗?只需使用
\uuuuuu类
魔法常量即可找到当前类。谢谢Maximus,是的,但我需要获取所有父类及其所有方法。基于这一点,我可以确定say->myMethod()的哪个实例优于它,这将帮助我浏览代码。这也是基于这样的假设:在Magento中,至少有一个文件包含一个类,我可以这样映射它。我想创建一个工具,它将创建一个GUI,上面写着“下面的方法(myMethod)位于/app/code/core/blah/blah.php中,在第738行的Mage_Etc_Etc_Etc_Etc(行#将是蛋糕上的糖霜)中,您可以在循环中调用get_parent_class(),再次输入每次回调的结果,直到返回一个false这就是我要寻找的,现在如果我可以指定每个类的方法,我就可以确定扩展类的“最后一个实例”。结合get_included_files(),我相信我可以创建一个跟踪器,正如我在Magento中发现的那样,这将非常有帮助:)我已经更新了答案以包含更多信息。希望这有帮助。
C
 with methods: trace
/tmp/stuff.php
B
 with methods: 
 /tmp/stuff.php
A
 with methods: 
 /tmp/stuff.php
function trace() {
    //you can modify this, this is hard coded
    $this->_root='/home/vagrant/commoncart/';

    $cls = get_class($this);
    $i=0;
    $methodMap=array();
    $classMap=array();
    //we are ascending UPwards
    while ($cls !== FALSE) {

        $i++;
        $reflectionClass = new ReflectionClass($cls);
        $file=$reflectionClass->getFileName();

        $classMap[$cls]=$file;

        if($a=get_class_methods($cls)) foreach($a as $method){
            if(!empty($methodMap[$method])){
                $methodMap[$method]['override']=true;
                $methodMap[$method]['overridden'][]=$cls; //
            }else{
                $methodMap[$method]=array(
                    'class'=>$cls,
                    'file'=>str_replace($this->_root,'',$file),
                    'line'=>'undefined', //this would be great to have, probably need to read the file and parse it once..
                );
            }
        }

        $cls = get_parent_class($cls);
        if($i>5)break;
    }
    echo '<pre>';
    print_r($classMap);
    print_r($methodMap);
    exit('done');
}