是否有可能获得PHP文件的位置,在运行时进行评估?我正在寻找类似于魔术常量的东西__DIR__,但在运行时进行评估,作为后期绑定.类似的差异self和static:
__DIR__ ~ self
??? ~ static
Run Code Online (Sandbox Code Playgroud)
我的目标是在抽象类中定义一个方法,使用__DIR__它来分别为每个继承类评估.例:
abstract class Parent {
protected function getDir() {
// return __DIR__; // does not work
return <<I need this>>; //
}
}
class Heir extends Parent {
public function doSomething() {
$heirDirectory = $this->getDir();
doStuff($heirDirectory);
}
}
Run Code Online (Sandbox Code Playgroud)
显然,只有当出现此问题Parent,并Heir在不同的目录.请考虑到这一点.而且,getDir在各种继承人类中反复定义似乎并不是选项,这就是我们继承的原因......