2011-10-10 85 views

回答

6

使用反射,特別是ReflectionClass::getFileName()。您如何實例化ReflectionClass完全取決於當前的範圍,如

// globally 
$reflectionClass = new ReflectionClass('SubClassName'); 

// within the sub class 
$reflectionClass = new ReflectionClass(__CLASS__); 

// within either sub or parent class in a static method 
$reflectionClass = new ReflectionClass(get_called_class()); 

// within either sub or parent class, provided the instance is a sub class 
$reflectionClass = new RelfectionObject($this); 

// filename 
$fn = $reflectionClass->getFileName(); 

// what I assume you mean by "path" 
$path = dirname($fn); 
+0

我需要的路徑不是一個文件名,它必須是子類,而不是當前的類路徑: –

+0

@EmanuilRusev我已經更新了我的答案。你能更具體地說明你的「路徑」是什麼意思嗎? – Phil

+0

感謝您的編輯!根據方法的名稱,我錯誤地認爲'getFileName()'返回一個文件名而不是路徑。 –