相关疑难解决方法(0)

PHP __DIR__评估运行时(后期绑定)?

是否有可能获得PHP文件的位置,在运行时进行评估?我正在寻找类似于魔术常量的东西__DIR__,但在运行时进行评估,作为后期绑定.类似的差异selfstatic:

__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在各种继承人类中反复定义似乎并不是选项,这就是我们继承的原因......

php inheritance late-binding dir

6
推荐指数
1
解决办法
550
查看次数

标签 统计

dir ×1

inheritance ×1

late-binding ×1

php ×1