0

I'm writing a method to generate detailed data for logging functions and arguments in debug mode. I previously reproduced the same code in multiple places (bad), but it works just fine (good):

function validate_date($date)
{

    if ($condition)
    {
        $php_function       = __FUNCTION__;
        $php_function_args  = implode(', ',get_func_argNames($php_function));
        foreach (get_func_argNames($php_function) as $arg)
        {
            $txt.= "$arg: ${$arg}<br>";
        }
     }
 }

So I wrote a new method to make this easier to maintain:

$_debug_txt = return_debug_header(__FUNCTION__);

function return_debug_header($php_function)
{

    // returns debug string to debug handler
    $arr_args = get_func_argNames($php_function);
    $php_function_args  = implode(', ',$arr_args);
    if (is_array($arr_args)) {
        foreach ($arr_args as $arg)
        {
            // $arg shows the right variable NAME, but ${$arg} is null.
            $txt.= "$arg: ${$arg}<br>";
        }
    } else {
        $txt = 'No arguments passed.';
    }

It might be used like this

function validate_date($date)
{

    if ($condition)
    {
        // generate debug header only if debug is true.
        $_debug_txt = return_debug_header(__FUNCTION__);
        // do something with txt...
    }
}

The problem is that variable variables does not appear to work with data retrieved from get_func_argNames. Variable names exist (I can print them to the screen), but the corresponding value appears blank.

PHP warns that variable variables do not work with superglobals, however it is not clear whether data returned from get_func_argNames is considered "superglobal".

Is anyone seeing anything else that could be causing variable variables not to work inside this function?

a coder
  • 7,530
  • 20
  • 84
  • 131
  • This won't work because of variable scopes. The `$date` variable only exists within the `validate_date()` function, not within `return_debug_header()`. – Barmar May 20 '13 at 21:47
  • $date is derived from the array `get_func_argNames(__FUNCTION__)`. I'm not sure I follow. – a coder May 20 '13 at 21:49
  • The variable variable is being evaluated inside `return_debug_header()`, so it can only be used to access variables in its scope, not the scope of other functions. – Barmar May 20 '13 at 21:51
  • Why am I able to print the variable name then? If $date is not available in `return_debug_header()`, shouldn't it not exist, period? I can make a fiddle to demo what I'm talking about, unless you follow. – a coder May 20 '13 at 21:53
  • The variable name is just a string returned by `get_func_argNames()`, so why wouldn't you be able to print it? – Barmar May 20 '13 at 21:55
  • It prints just fine. The corresponding value is null. Not what usually happens with variable variables. – a coder May 20 '13 at 21:56
  • Do you understand what "scope" means? – Barmar May 20 '13 at 21:56
  • Yes, I do. I realize that variables used inside a function only exist inside that function unless passed as an argument or made global. – a coder May 20 '13 at 21:58
  • Well, the same is true for variable variables. Since you can't access `$date` inside `return_debug_header`, you can't do `$arg = "date"; echo $$arg;` either. – Barmar May 20 '13 at 22:07

2 Answers2

1

You can't access local variables in one function from another function. Variable variables only operate within the local scope. So you need to pass the arguments as an array to the debug function.

function return_debug_header($php_function, $args)
{

    // returns debug string to debug handler
    $arr_args = get_func_argNames($php_function);
    if (is_array($arr_args)) {
        foreach ($arr_args as $i => $arg)
        {
            $txt.= "$arg: {$args[$i]}<br>";
        }
    } else {
        $txt = 'No arguments passed.';
    }
}

Use this as:

$_debug_text = return_debug_header(__FUNCTION__, func_get_args());
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

To further what you can get from this, research "scope" in programming. It is essential to understanding the workings of variable access and programming in general. http://en.wikipedia.org/wiki/Scope_(computer_science)

dudewad
  • 13,215
  • 6
  • 37
  • 46
  • I knew about scope (see comment above), but the way I was accessing the variable (and scope) was obscured. I see the light now. – a coder May 20 '13 at 22:34