代码之家  ›  专栏  ›  技术社区  ›  Pim Jager

如何获取包含在PHP中的类的文件名

  •  2
  • Pim Jager  · 技术社区  · 16 年前

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

    测试:PHP:

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

    包含PHP

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

    class/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;
      }
    ?>
    

    magic文件常量不正确,它返回path/to/myclass.php

    3 回复  |  直到 14 年前
        1
  •  4
  •   Ivan    16 年前

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

        2
  •  2
  •   Pim Jager    16 年前

    我最终使用了:

    $file = basename(strtolower($_SERVER['SCRIPT_NAME']));
    
        3
  •  2
  •   Stan    14 年前

    我正在使用

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

    这样就不需要修改包含文件的文件。调试回溯可能会产生一些速度问题。