Welcome Guest, Not a member yet? Register   Sign In
Get the name of the function ??
#1

[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')
}
#2

[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-gui...s/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");
#3

[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
#4

[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();

?>
#5

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

http://php.net/manual/en/language.consta...efined.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.
#6

[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
#7

[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();




Theme © iAndrew 2016 - Forum software by © MyBB