Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get parent class file path

Tags:

php

class

parent

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.

like image 712
Timo Huovinen Avatar asked May 20 '11 11:05

Timo Huovinen


People also ask

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.

What is dirname (__ FILE __) in PHP?

dirname(__FILE__) allows you to get an absolute path (and thus avoid an include path search) without relying on the working directory being the directory in which bootstrap. php resides. (Note: since PHP 5.3, you can use __DIR__ in place of dirname(__FILE__) .)

How do I go back to a parent directory in PHP?

If you are using PHP 7.0 and above then the best way to navigate from your current directory or file path is to use dirname(). This function will return the parent directory of whatever path we pass to it.

How do I include a file in a parent directory in PHP?

For the current folder of the current file, use $current = dirname(__FILE__); . For a parent folder of the current folder, simply use $parent = dirname(__DIR__); . Save this answer.


1 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.

like image 146
KingCrunch Avatar answered Sep 20 '22 11:09

KingCrunch