file1.php require_once(file2.php); $object = new class2(); PHP: file2.php class class2 { } PHP: If I include file 2 from file 1, how do I get file1's name from file2?
I'm really unclear as to what you are trying to do, but you could try $_SERVER['PHP_SELF'] If you include that variable in your class in file2, and then call it as an include in file1, I think it will return file1...
$_SERVER['PHP_SELF'] returns the location of the executed script. If you execute domain.com/dir/test.php $_SERVER['PHP_SELF'] will return /dir/test.php
So, if class2 in file2.php used the variable $_SERVER['PHP_SELF'], and he called that class from file1.php, it will return the string location of file1.php, which he could then use some string functions on in order to get the filename....right??? I could be wrong....
It would really help if you explain why you're trying to do this... there may be an easier way! If worse comes to worst, you could always use debug_backtrace ( http://ca.php.net/manual/en/function.debug-backtrace.php ) and then track back through the array to determine the parent file.
if you try $file = basename($_SERVER['PHP_SELF']); Not tried it but i would say it should give a correct reply
So, here is what I tried for you: page1.php: include("page2.php"); PHP: page2.php: echo basename($_SERVER['PHP_SELF']); PHP: when you load page1.php it displays page1.php. Hopefully this helps, but like previously mentioned, if you could possibly explain how you're using it and what you're trying to do, we might be able to give a better answer.