7

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.

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I knew of reflectionobjects, but I had no idea they could do all this. You saved me countless hours of debugging! :D I found the method I was looking for, but I still don't know where MyClass is, it looks like MyClass is also an extended class
$declaringClass in my example is the class, the implements the method. $filename is the filename, where the class is declared. It doesn't care, if MyClass is extended too, if you want to know, where __initComponents() is defined. You can use ReflectionClass::getParentClass() to get the Parent-class, if you assume, that this will help you.
It helped me find the class that I needed (the outermost parent), not the class that I thought I needed (the direct parent "MyClass"), now with getParentClass I even found "MyClass" too (which I didn't need, but was fun to find), so I fixed a ton of bugs, and everything is fine now :)
3
   $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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.