0

我目前可以做到以下几点:

class SubClass extends SuperClass {
  function __construct() {
    parent::__construct();
  }
}

class SuperClass {
  function __construct() {
    // this echoes "I'm SubClass and I'm extending SuperClass"
    echo 'I\'m '.get_class($this).' and I\'m extending '.__CLASS__;
  }
}

我想对文件名(__FILE__但动态评估)做类似的事情;我想从超类中知道子类所在的文件。有没有可能以任何优雅的方式?

我知道你可以用 做一些事情get_included_files(),但这不是很有效,特别是如果我有很多实例。

4

2 回答 2

2

您可以使用反射。

$ref = new ReflectionObject($this);
$ref->getFileName(); // return the file where the object's class was declared
于 2008-10-14T20:33:39.303 回答
0

呃,不是真的,我能想到的。每个子类都需要有一个显式实现的方法返回__FILE__,这首先完全违背了继承点。

我也很好奇为什么这样的东西会有用。

于 2008-10-14T18:40:09.843 回答