gpt4 book ai didi

php如何获取子类的__DIR__

转载 作者:可可西里 更新时间:2023-10-31 22:08:41 25 4
gpt4 key购买 nike

我在不同的文件夹中有两个类

class Parent
{
public $path = null;

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

class Child extends Parent
{

}

所以当我创建一个 Child 实例时:

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

我得到一条通往 Parent 的路径。我真正想要的是获得通往 Child 的路径。我无法修改子类。

这可能吗?

最佳答案

您可以使用reflection得到你要找的东西:

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

关于php如何获取子类的__DIR__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3896384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com