Name of the variable passed in the function argument

-1

Situation

I'm trying to develop this function

function ve($var, $dieAfter = false){

    $nomeDaVar = ????; 

    echo '<pre>';
    echo '$'.$nomeDaVar." = ";
    var_export($var);
    echo '</pre>';

    if ($dieAfter){
        exit();
    }
}

In $nomeDaVar I want to get the name of the variable passed in the argument $var .

Example

function pessoa(){

    $dadosPessoa = array(
        'nome' => 'Guilherme',
        'sobrenome' => 'Lautert'
    );
    ve($dadosPessoa, 1);
}

Desired result

$dadosPessoa = array(
    'nome' => 'Guilherme',
    'sobrenome' => 'Lautert'
)

Tests

debug_backtrace()   // Não me retorna o nome da variável;
func_get_arg(0)     // Não me retorna o nome da variável, apenas o array;
get_defined_vars()  // Me retorna o nome 'var' nao 'dadosPessoa';
$GLOBALS            // Não possui a variável;

Links

  • This question has a similar heading but does not apply:
  • This question has the same intent but did not have the same result: link
    • The second answer resolves, but does not seem practical.
asked by anonymous 02.04.2015 / 17:03

2 answers

3

You can not do this and there is no reason to do so.

Actually it could even have some form. I do not know one specifically but since PHP is basically a language interpreted, it is possible that there is an API that allows you to access this information, or in the latter case you could try to load the source of your script , analyze it and find to inform. But it's not something simple and it would be ridiculous to do this.

If it's just curiosity, she's satisfied. If you really want to use this for something real, rethink your design , something very wrong is being done.

What you want to do is easy to solve, just create a parameter with the name of the last variable. I know you want to avoid this but there is a reason the variables are scoped and what you are trying to do is just get over it. Functions were made to be independent, do not need to know anything of what was used to call it.

And this has another problem, you are wanting information that may not even exist. I have already said in some places that people get confused with variables and expressions. An argument from a function call requests an expression, not a variable. If this expression is just a variable, this is just a coincidence. What is the name of the variable if you call this:

function pessoa(){
    ve(array(
        'nome' => 'Guilherme',
        'sobrenome' => 'Lautert'
    ), 1);
}

That is, you want information that is not even the argument of the function. You have to go further to get it. And how far should it go?

The simplest thing you can get is the name of the parameter, but you already know how to get it. And you do not really need it, you already know what it is when it's encoding. It is not run-time variable.

Difference between argument and parameter .

Another solution is to make the variable be global, but please do not do this under any circumstances.

    
02.04.2015 / 17:19
0

The best you can do is to dynamically give the variable a name.

$var1 = "nome";
$var2 = "idade";

$$var1 = "Rafael";
$$var2 = "25";

So you can access $nome and $idade that they exist. Take the test:

if ($nome == "Rafael")
{
    echo $var1." = ".$nome;
}

if ($idade == "Rafael")
{
    echo $var2." = ".$idade;
}

This way you will have access to the variable names. I do not see any way beyond that, sorry ...

    
02.04.2015 / 17:55