Home » Php » php get parent class file path

php get parent class file path

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

   $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();