Get calling file name from include()
This is actually just a special case of what PHP templating engines do. Consider having this function:
function ScopedInclude($file, $params = array())
{
extract($params);
include $file;
}
Then A.php
can include C.php
like this:<?php
// A.php
ScopedInclude('C.php', array('includerFile' => __FILE__));
Additionally, B.php
can include C.php
the same way without trouble.<?php
// B.php
ScopedInclude('C.php', array('includerFile' => __FILE__));
C.php
can know its includer by looking in the $params array.<?php
// C.php
echo $includerFile;
Get filename of file which ran PHP include
An easy way is to assign a variable in the parent file (before the inclue), then reference that variable in the included file.
Parent File:
$myvar_not_replicated = __FILE__; // Make sure nothing else is going to overwrite
include 'other_file.php';
Included File:if (isset($myvar_not_replicated)) echo "{$myvar_not_replicated} included me";
else echo "Unknown file included me";
You could also mess around with
get_included_files()
or debug_backtrace()
and find the event when and where the file got included, but that can get a little messy and complicated. Finding out the filename that called my function in PHP
A solution might be to use the debug_backtrace
function : in the backtrace, that kind of information should be present.
Or, as Gordon pointed out in a comment, you can also use debug_print_backtrace
if you just want to output that information and not work with it.
For instance, with temp.php
containing this :
<?php
include 'temp-2.php';
my_function();
and with temp-2.php
containing this :<?php
function my_function() {
var_dump(debug_backtrace());
}
Calling
temp.php
(i.e. the first script)
from my browser gets me this output :array
0 =>
array
'file' => string '/.../temp/temp.php' (length=46)
'line' => int 5
'function' => string 'my_function' (length=11)
'args' =>
array
empty
In there, I have the "temp.php
" filename -- which is the one in which the function has been called.
Of course, you'll have to test a bit more (especially in situations where the function is not in the "first level" included file, but in a file included by another one -- not sure debug_backtrace
will help much, there...) ; but this might help you get a first idea...
how to get the caller's filename, method name in python
You can use the inspect
module to achieve this:
frame = inspect.stack()[1]
module = inspect.getmodule(frame[0])
filename = module.__file__
How to get the file name from a full path using JavaScript?
var filename = fullPath.replace(/^.*[\\\/]/, '')
This will handle both \ OR / in paths Node.js: How to get filename where function was required from within a module?
Getting Parent Module
You can do this by usingmodule.parent
, then resolving the filename
property like so:module.js
module.exports(function() {
return {
print : function(message) {
console.log(module.parent.filename + ' ' + message);
};
}
});
app.js
var module = require('./module')();
module.print('hello');
Output:
/path/to/app.js hello
Which is almost what you're asking for.Qualifying The Solution
This solution provides you with a fully qualified path, but you get the filename by splitting the path by its delimiter.var parentModFilename = module.parent.filename.split(/\\|\//).pop()
Which would then give you "/app.js"
in parentModFilename
. Get name of calling function, line number and file name in c++
When I need a fast "printf" logging, I use this marco for message logging that is branded with filename and line:
#define _MSG(msg) do{ std::cerr << __FILE__ << "(@" << __LINE__ << "): " << msg << '\n'; } while( false )
The above macro will inject a msg
into a std::cerr
pipeline. You can take out parts you need or modify it for your purposes. It hinges on __FILE__
and __LINE__
macros, which are defined by standard:Function names are not so easy to get, and I don't think there is a nice way to get it.
__FILE__
The presumed name of the current source file (a character string literal).
__LINE__
The presumed line number (within the current source file) of the current source line (an integer
constant).
If you want logging through functions I would define some macro, or make function that would take int
and char*
for line and file respectively. Something like log(int line, char* source_file, string message)
.
Related Topics
How to Check Is Timezone Identifier Valid from Code
What Is the Correct Format for a Blowfish Salt Using PHP's Crypt
PHP Uploading Files - Image Only Checking
Best Way to Identify a User Uniquely
PHP - Display a 404 Error Without Redirecting to Another Page
Check Image Dimensions (Height and Width) Before Uploading Image Using PHP
Php: Fastest Way to Check for Invalid Characters (All But A-Z, A-Z, 0-9, #, -, ., $)
Converting PHP Array of Arrays into Single Array
Cannot Set Date.Timezone in PHP.Ini File
Maximum Function Nesting Level of '100' Reached, Aborting After Upgrading to Laravel 5.1
Setting PHP Enviromental Variable While Running Command Line Script
How to Add Item to the JSON File Formatted Array
How to Sort an Array by Similarity in Relation to an Inputted Word
How to Avoid Request Entity Too Large 413 Error