2

我知道这个问题很难理解,我不知道如何更好地问它,所以我将使用这个代码示例让事情更清楚:
如果我有以下文件:

测试.php:

<?php
 include('include.php');
 echo myClass::myStaticFunction();
?>

包含.php

<?php
 __autoload($classname){
  include_once("class/".$classname.".php"); //normally checking of included file would happen
 }
?>

类/myClass.php

<?php
 class myClass{
  public static function myStaticFunction(){
   //I want this to return test.php, or whatever the filename is of the file that is using this class
   return SOMETHING;
  }
?>

神奇的 FILE 常量不正确,它返回 path/to/myClass.php

4

3 回答 3

4

如果您需要获取“test.php”,请参阅$_SERVER['SCRIPT_NAME']

于 2009-01-14T12:37:41.227 回答
2

我最终使用:

$file = basename(strtolower($_SERVER['SCRIPT_NAME']));
于 2009-01-14T15:23:08.473 回答
2

我在用

$arr = @debug_backtrace(false);
if (isset($arr))
foreach ($arr as $data)
{
 if (isset($data['file']))
 echo $data['file'];
 // change it to needed depth
}

这样您就不需要修改包含您的文件的文件。debug_backtrace 可能有一些速度方面的问题。

于 2010-09-26T21:53:22.847 回答