How to Get the Function Name Inside a Function in PHP

How do I get the function name inside a function in PHP?

The accurate way is to use the __FUNCTION__ predefined magic constant.

Example:

class Test {
function MethodA(){
echo __FUNCTION__;
}
}

Result: MethodA.

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

Retrieving the name of the current function in PHP

Yes, you can get the function's name with the magic constant __FUNCTION__

class foo
{
function print_func()
{
echo __FUNCTION__;
}
function print_method()
{
echo __METHOD__;
}
}

$obj = new foo();
$obj->print_func(); // Returns: print_func
$obj->print_method(); // Returns: foo::print_method

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

Is it possible to get the name of a function by it's reference?

This has been asked before: How can I get the callee in PHP?

You can get the information you need with debug_backtace. Here is a very clean function I have found:

<?php
/**
* Gets the caller of the function where this function is called from
* @param string what to return? (Leave empty to get all, or specify: "class", "function", "line", "class", etc.) - options see: http://php.net/manual/en/function.debug-backtrace.php
*/
function get_caller($what = NULL)
{
$trace = debug_backtrace();
$previousCall = $trace[2]; // 0 is this call, 1 is call in previous function, 2 is caller of that function

if(isset($what)) {
return $previousCall[$what];
} else {
return $previousCall;
}
}

And you (might) use it like this:

<?php
function foo($full)
{
if ($full) {
return var_export(get_caller(), true);
} else {
return 'foo called from ' . get_caller('function') . PHP_EOL;
}
}

function bar($full = false)
{
return foo($full);
}

echo bar();
echo PHP_EOL;
echo bar(true);

Which returns:

foo called from bar

array (
'file' => '/var/www/sentinel/caller.php',
'line' => 31,
'function' => 'bar',
'args' =>
array (
0 => true,
),
)

How to call a function from a string stored in a variable?

$functionName() or call_user_func($functionName)

How to use builtin function name inside a class as a method name

Nope, PHP doesn't have a way of escaping reserved words so that they can be used as class/method/etc names.

You cannot use any of the following words as constants, class names, function or method names. Using them as variable names is generally OK, but could lead to confusion.

As of PHP 7.0.0 these keywords are allowed as as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.

See http://php.net/manual/en/reserved.keywords.php for a full list.

Demo: https://3v4l.org/WVgvh

Pass a function name as parameter in a function

function test($str, $func){
return call_user_func($func,$str);
}

Use it like this

$asd = test("ASD","strtolower");


Related Topics



Leave a reply



Submit