How to get name of calling function/method in PHP?
The debug_backtrace()
function is the only way to know this, if you're lazy it's one more reason you should code the GetCallingMethodName()
yourself. Fight the laziness! :D
Determine what namespace the function was called in
What you are looking for is : ReflectionFunctionAbstract::getNamespaceName
If you want to know where you're coming from debug_backtrace() is your friend.
The following should solve your puzzle:
function backtrace_namespace()
{
$trace = array();
$functions = array_map(
function ($v) {
return $v['function'];
},
debug_backtrace()
);
foreach ($functions as $func) {
$f = new ReflectionFunction($func);
$trace[] = array(
'function' => $func,
'namespace' => $f->getNamespaceName()
);
}
return $trace;
}
Just call it from anywhere to see the backtrace.
I modified your "procedural" code file as follows:
namespace Foo;
function bar ()
{
var_export(backtrace_namespace());
}
/** The unasked question: We need to use the fully qualified name currently. */
function go()
{
\Site\Action\add('hookname', 'Foo\\bar');
}
go();
The Result from including this file will be the following on stdout:
array (
0 =>
array (
'function' => 'backtrace_namespace',
'namespace' => '',
),
1 =>
array (
'function' => 'Foo\\bar',
'namespace' => 'Foo',
),
2 =>
array (
'function' => 'call_user_func',
'namespace' => '',
),
3 =>
array (
'function' => 'Site\\Action\\add',
'namespace' => 'Site\\Action',
),
4 =>
array (
'function' => 'Foo\\go',
'namespace' => 'Foo',
),
)
Now for bonus points the answer to the hidden question:
How do I resolve the calling namespace to avoid using fully qualified function name as argument?
The following will allow you to call the function as you intended:
Site\Action\add('hookname', 'bar');
Without getting the dreaded:
Warning: call_user_func() expects parameter 1 to be a valid callback, function 'bar' not found or invalid function name
So before you redesign try this on for size:
namespace Site\Action;
function add($hook, $function)
{
$trace = backtrace_namespace();
$prev = (object) end($trace);
$function = "$prev->namespace\\$function";
if (is_callable($function))
call_user_func($function);
}
I see no reason why debug_backtrace
should not be used, this is what it is there for.
nJoy!
How to find out where a function is defined?
You could also do this in PHP itself:
$reflFunc = new ReflectionFunction('function_name');
print $reflFunc->getFileName() . ':' . $reflFunc->getStartLine();
How to find where a function is being called from in laravel?
Throw exception inside that function and run it if you can do it by clicking on web app (or call request in other way) - if no then try to find in project source code by its name (multifile full text search).
throw new Exception('Test exception to get stacktrace');
or new \Exception(...)
. This will show you stacktrace as request result and/or you will find it in storage/logs/laravel.log
file
Determine where a function has been called with PHP
From within the function debug_backtrace()
is the only way to determine the caller, unless of course you pass that information as parameter.
Note, that you cannot use default values to do this. E.g.:
function f($param1, $param2, $caller=__FUNCTION__) ...
__FUNCTION__
will be evaluated at parse time, so it'll always have the same value. The function in which scope f
is declared.
Related Topics
Replace Values in a Uri Query String
Azure Active Directory Sso with Laravel
Random Float Between 0 and 1 in PHP
Default as First Option in Switch Statement
Detect If Cookies Are Enabled in PHP
PHP Remember File Field Contents
Php, Merging Arrays with Common Keys
Using Gcm to Send Notifications on App, Returns Invalidregistration Error
Is There a Limit When Using PHP Mail Function
How to Decode a JSON String in PHP
How to Extract HTML Comments and All HTML Contained by Node
Differencebetween Switch-Case and If-Else in PHP
PHP Copying Array Elements by Value, Not by Reference
Laravel Gmail Not Working, "Username and Password Not Accepted. Learn More..."