CodeIgniter Forums
Get the name of the function ?? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Get the name of the function ?? (/showthread.php?tid=31052)



Get the name of the function ?? - El Forum - 06-04-2010

[eluser]chefnelone[/eluser]
Hello

It might sound extrange but I need to dinamically grab the function name in a controller.
Something like:

Code:
function hello(){

$data['function_name'] =  ..... //here I need to dinamically grab the name of this function (which is 'hello')
}



Get the name of the function ?? - El Forum - 06-04-2010

[eluser]falkencreative[/eluser]
Technically, I'm not sure if that is possible...

However, if that function name is part of the URL (yoursite.com/controller/function/variables), you could grab that using the URI class's segment() function:

http://ellislab.com/codeigniter/user-guide/libraries/uri.html

Will that work? Otherwise, one other option is to pass in the name of the function as an argument:

function hello($function_name) {}

and to call the function:

function("hello");


Get the name of the function ?? - El Forum - 06-04-2010

[eluser]WanWizard[/eluser]
Try:
Code:
// get the backtrace
$_dbbtr = debug_backtrace();
// dum info about the current environment
var_dump($_dbbtr[0]);
see http://php.net/manual/en/function.debug-backtrace.php


Get the name of the function ?? - El Forum - 06-04-2010

[eluser]falkencreative[/eluser]
Hmm, didn't know about that function. In that case, this works for me:

Code:
<?php

function test()
{
    $_dbbtr = debug_backtrace();
    echo "function called: " . $_dbbtr[0]['function'];
}

test();

?>



Get the name of the function ?? - El Forum - 06-04-2010

[eluser]danmontgomery[/eluser]
debug_backtrace() is a lot of overhead for a function name...

http://php.net/manual/en/language.constants.predefined.php

Quote:__FUNCTION__ The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.



Get the name of the function ?? - El Forum - 06-04-2010

[eluser]WanWizard[/eluser]
Yup, very good point.

I only use it in my error handlers. to figure out where the error occured.
Especially with database errors, I know it was caused by line something in a database library, it's more interesting to know what code called the database library.

In that case, the error is terminal, so I'm not to worried about the overhead at that point... Wink


Get the name of the function ?? - El Forum - 06-05-2010

[eluser]flaky[/eluser]
[quote author="chefnelone" date="1275683603"]Hello

It might sound extrange but I need to dinamically grab the function name in a controller.
Something like:

Code:
function hello(){

$data['function_name'] =  ..... //here I need to dinamically grab the name of this function (which is 'hello')
}
[/quote]

Code:
$this->router->fetch_method();