如何从继承的方法获取当前类的路径?如何从继承的方法获取派生类的路径?
我有以下几点:
<?php // file: /parentDir/class.php
class Parent {
protected function getDir() {
return dirname(__FILE__);
}
}
?>
和
<?php // file: /childDir/class.php
class Child extends Parent {
public function __construct() {
echo $this->getDir();
}
}
$tmp = new Child(); // output: '/parentDir'
?>
的__FILE__
常量总是指向它在文件的源文件,而不管继承的。
我想获得派生类的路径的名称。
有没有优雅这样做的方法?
我可以按照$this->getDir(__FILE__);
的方法做一些事情,但这意味着我必须经常重复自己。如果可能的话,我正在寻找一种将所有逻辑放在父类中的方法。
更新:
接受的解决方案(由Palantir):
是的,这是Palantir的答案的逻辑结论。 – Jacco 2010-06-10 13:06:44