Re: $_SERVER['PHP_SELF'] in a included file

From: Date: Mon, 26 Feb 2007 15:00:26 +0000
Subject: Re: $_SERVER['PHP_SELF'] in a included file
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-249450@lists.php.net to get a copy of this message
clive wrote:
Thanks Vincent,Stut and Olaf. Thats __file___ is exactly what I needed :) now for another brain teaser for your collective brains, How does a function know what file it was called from. a.php includes functions.php in a.php we call function test(); which is declared in functions.php Is there anyway for test(); to echo "called from a.php" with out passing 'a.php' or ___FILE___ as a parameter to the function? Im busy googling now for a answer.
Note that the following function has not been extensively tested, and should be treated as such. I wrote this function a long time ago to get the calling function from a common error routine. You should be able to modify this to get what you want... function GetCaller($offset = 0) { $trace = debug_backtrace(); $retval = ''; if (is_array($trace)) {
    foreach ($trace as $call)
    {
      if ($offset > 0)
      {
        $offset--;
      }
      else
      {
        if (strlen($retval) == 0)
        {
          $retval = str_replace($GLOBALS['rootdir'], '',
                                $call['file']).':'.$call['line'].' ';
        }
        elseif (isset($call['function']))
        {
          return $retval.(isset($call['class'])
                          ?
                         $call['class'].'::'
                          :
                         '').$call['function'];
        }
      }
    }
} if (strlen($retval) == 0)
    return 'unknown';
return $retval.'unknown function'; } Hope it helps. -Stut

« previous php.general (#249450) next »