简体   繁体   中英

php how to get __DIR__ of child class

I have two classes in separate folders

class Parent
{
    public $path = null;

    function  __construct()
    {
        $this->path = __DIR__;
    }
}

and

class Child extends Parent
{

}

So when I create an instance of Child:

$child = new Child();
echo $child->path

I get a path to Parent. What I actually want is to get path to Child. I cannot modify Child class.

Is it possible?

You can get use reflection to get what you're looking for:

$child = new Child();
$class_info = new ReflectionClass($child);
echo dirname($class_info->getFileName());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM