开发者

php get parent class file path

开发者 https://www.devze.com 2023-03-07 01:50 出处:网络
example index.php //I know where this file is $class = new childclass(); $class->__initComponents(); somefile.php //I know where this file is

example

index.php //I know where this file is

$class = new childclass();
$class->__initComponents();

somefile.php //I know where this file is

Class childclass extends parentclass {

}

someparentfile.php // I don't know where this file is

Class parentclass {
 开发者_运维问答 function __initComponents(){
    //does something
  }
}

I need to find out where someparentfile.php is.

reason:

I am debugging some difficult php code that someone else wrote, I need to find out what file contains the code that defines a class parameter.

I found it as far as a class method calling a function that does this:

$class->__initComponents();//the parameter is defined somewhere in there

The problem is that this function is within a parent class "MyClass" of the above $class, and I have no idea where the parent class is.

is there a way or some debug function by which I can find out the location of this parent class or at least where the parameter was defined?

p.s. downloading the whole application and then using text search would be unreasonable.


You can use Reflection

$object = new ReflectionObject($class);
$method = $object->getMethod('__initComponents');
$declaringClass = $method->getDeclaringClass();
$filename = $declaringClass->getFilename();

For further information, what is possible with the Reflection-API, see the manual

however, for the sake of simplicity, I suggest to download the source and debug it local.


   $object = new ReflectionObject($this); // Replace $this with object of any class.

   echo 'Parent Class Name: <br>';
   echo $object->getParentClass()->getName();

   echo '<br>Parent Class Location: <br>';
   echo $object->getParentClass()->getFileName();
0

精彩评论

暂无评论...
验证码 换一张
取 消