How to Get Name of Calling Function/Method in PHP

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

How do I get the caller's method name

I think debug_backtrace could do the trick.

It gives you a backtrace of a function call. Observe this result then you'll have to figure out how to grab the function name you want. But the information is there.

<?php 
class father {
public function db_select(){
echo debug_backtrace()[1]['function'];
print_r(debug_backtrace());
}
}

class plugin extends father {
public function select_plugin(){
$query = $this->db_select();
}
}

?>

How can I get the name of the script that called a function?

Depending on the calling environment, you should have a look at debug_backtrace, and $_SERVER['PHP_SELF']

debug_backtrace() will give you a stack trace of the includes and function calls so far, and $_SERVER['PHP_SELF'] will tell you the currently executing script, which is easier and might work just as well for what you want. $_SERVER['PHP_SELF'] will pretty much always be the script that was called from the browser, for example, if you had blah.com/admin.php and blah.com/articles.php that both called /pages.php to get a list of the pages saved in a blog or something, and something went wrong -- the log would tell you whether admin.php or articles.php was calling the script. But if pages.php included functions.php which included libs.php and libs.php failed, and you wanted to know that functions.php included it, this wouldn't work -- the log would still show the script that started the including (admin.php or articles.php). In this case you would use debug_backtrace().

Get caller function arguments

Use debug_backtrace function.

It generates a PHP backtrace, returning an array of associative arrays. One of the keys in those associative arrays is 'args'. If called inside a function, this key basically contains the functions arguments list (as an array). If this is used inside an included file, this lists the included file name(s).

For eg (from PHP docs):

function a_test($str)
{
echo "\nHi: $str";
var_dump(debug_backtrace());
}

a_test('friend');

It will output the following:

array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}

get function name that called function without arguments

No matter,

For my requirement this functionality is useful rather that 'bad coding'.

Anyway the answer is:

$e = new Exception();
$trace = $e -> getTrace();
$caller = $trace[1]["function"];

This will get the function name of the caller.

Can you get a method name from within a method in PHP?

Sure, you want the magic constants.

function myFunction() { print __FUNCTION__." in ".__FILE__." at ".__LINE__."\n"; }

Find out more from the php manual



Related Topics



Leave a reply



Submit