views:

918

answers:

3

I understand that the question is rather hard to understand, I didn't know how to ask it better, so I'll use this code example to make things more clear:
If I have the following files:

test.php:

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

include.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;
  }
?>

the magic FILE constant is not the correct one, it returns path/to/myClass.php

+3  A: 

in case you need to get "test.php" see $_SERVER['SCRIPT_NAME']

Ivan
or basename(__FILE__)
Deniss Kozlovs
read the question again:"the magic FILE constant is not the correct one, it returns path/to/myClass.php"
Ivan
A: 

I ended up using:

$file = basename(strtolower($_SERVER['SCRIPT_NAME']));
Pim Jager
So, what if you now use script C, which includes a file that has your first example (A), and then your example includes the second file from autoloading, file (B)You would have the wrong filename.
SchizoDuckie
Yes, ok, that's true. However in my case not a problem since I'll always need the first file (C)
Pim Jager
A: 

I am using

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

This way you don't need to modify the file from which your file is included. debug_backtrace might have some speed consenquencies.

stAn