开发者

php how to get __DIR__ of child class

I have two classes in separate folders

class Parent
{
    public $path = null;

    function  __construct()
    {
        $this->path开发者_C百科 = __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());
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜