Variable name passed in function argument

Asked

Viewed 291 times

-1

Situation

I’m willing 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 take 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'
)

Testing

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 title but does not apply : link
  • This question has the same purpose but has not had the same result : link
    • The second answer solves, but it doesn’t seem practical.
  • Again I do not understand the -1 of the question.

2 answers

3


You can’t do this and there’s no reason to do it.

Actually, there might be a way. I don’t know one specifically but since PHP is a basically interpreted language, it is possible that there is an API that allows you to access this information, or in the last case you could try to load your own source script, analyze it and find to inform. But it is 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 variable passed. I know you want to avoid this, but there’s a reason variables have scope and what you’re trying to do is just go over it. Functions were made to be independent, not need to know anything of what was used to call it.

And this has another problem, you’re looking for information that may not even exist. I’ve talked in some places that people get confused with variables and expressions. An argument from a function call asks for an expression and not a variable. If this expression is only a variable, this is just a coincidence. What is the variable name if you call it:

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

I put in the Github for future reference.

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 you have to go?

The simplest you can get is the name of the parameter, but you already know how to get it. And actually it doesn’t need to, you already know what it is when you’re coding. It’s not variable at runtime.

Difference between argument and parameter.

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

  • In fact @bigown, I forgot the question of scope, and I thought only of practicality, the reason for the development of this function is tests, so you will have no problem passing the second parameter with variable name. Thank you for the critical, explanatory sense.

0

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

$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 variables' names. I see no other way than this, sorry...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.