How do I get the name of the variable used in the function argument?

4
  • Script1.php

Here script1.php calls the function that is in script2:

sendData($variavel);
  • Script2.php

The function receives the value and includes the script 3 to receive the values of the arguments:

sendData(){
    $getFirstArgumentValue = func_get_arg(0);
    // $getVariableName = ...
    sendVariableNameAndValue($getFirstArgument);
    // global $variavel;
    include('script3.php');
}
  • Script3.php

In script3.php it is declared the variable called in script1.php, and it is notorious that an error message appears:

echo($variavel);

The error message appears in script3 warning that $ variable has not been declared.

The question is:

How do I declare in script3, the same variable that was declared in script1, passing through the function declared in script2 and receiving this value in script3?

In this case would the sendData function have to get the value of the argument and also the name of the variable used to send this value?

Would it be possible to send the value and name of the variable?

    
asked by anonymous 16.08.2014 / 18:21

5 answers

1

With the PHP 5.6 update, the " Variadic functions via was released. .. "
I was able to create a clean and clear code:

  • Script1.php

    $variavel = 'foo';
    sendData($variavel);
    
  • Script2.php

    public function sendData(...$variables){
        foreach($variables as $variableValue){
            foreach($GLOBALS as $globalName => $globalValue){
                if($globalValue === $variableValue){
                    global $$globalName;
                }
             }
        }
    
        include('script3.php');
    }
    
  • Script3.php

    echo($variavel);
    

For this code to work, the function must be called with the variable in a global scope. For example: if the function is called inside another function this code does not work, unless you declare the variable sent as global. eCode:

sampleMethod(){
    $variavel = 'bar';
    global $variavel;
    sendData($variavel);
}

There is another method to get the name of the variable with debug_backtrace () , but I think the question has already been answered.

    
30.09.2014 / 14:14
0

In script3 declare the variable with the same global name, whether or not that class will make access in script3 will work!

    
16.08.2014 / 19:56
0

I think it is not possible, the only alternative that occurs to me, would be to pass the name of the variable as a string in the parameter:

$variavel = 'Olá mundo!';

sendData('variavel');

function sendData(){
    $getFirstArgumentValue = func_get_arg(0);
    global $$getFirstArgumentValue;
    include('script3.php');
}

script3.php

<?php

echo($variavel);

?>

But anyway you will be limited to the variable always have this name: $variavel and I think your problem is that you need to apply the sendData() function to other variables, so you should use something like this:

$outra_variavel = 'Olá mundo!';

sendData($outra_variavel);

function sendData($data){
    include('script3.php');
}

script3.php

<?php

echo($data);

?>

Note that in this example it is not necessary to set the $data variable to global, because doing include('script3.php'); within the sendData function will be in the same scope, ie it is the same as

$outra_variavel = 'Olá mundo!';

sendData($outra_variavel);

function sendData($data){
    echo($data);
}

    
16.08.2014 / 19:24
0

What you probably want to do is to have a script3.php template and load the required information through the sendData function, if this is the case you can do as follows.

function sendData(array $data = array()) {
    extract($data);
    include 'script3.php';
}

You'll have to set a% of style%:

$myData = array(
    'var1' => 'valor',
    'var2' => 'xpto',
);

So you can call the function array

So you can use the variables you need in script3.php , eg:

echo $var1;

if (isset($var2)) {
    echo 'var2 também existe com o seguinte valor: ' . $var2;
}

PS: If you want the sendData($myData); function to return the result generated by sendData as a string, you can do the following:

/**
 * Sends some data
 * @param  array  $data Variáveis a utilizar
 * @return string       Resultado
 */
function sendData(array $data = array()) {
    ob_start();
    extract($data);
    include 'script3.php';
    return ob_get_clean();
}
    
18.08.2014 / 12:04
0

Try this:

script1.php

$data['variavel'] = 1;
getData($data);

or

getData(array('variavel'=>1));

script2.php

function getData($arr){
     extract($arr);
     include 'script3.php';
}

script3.php

echo $variavel;

Any index you set in the array that goes as a parameter to the getData() function will be created by the extract() command in the script3.php file

Documentation: link

    
18.08.2014 / 19:26