GarouDan
GarouDan

Reputation: 3833

PHP. Pass variable by reference vs string. How to works with these two different arguments?

I'm writing my own debug functions and I need some help to fix the code below.

I'm trying to print a variable and its name, the file where the variable and the function was declared and the line of the function call. The first part I did, the variable, the variable name, the file and the line is printed correctly. At the code, a($variable) works good.

The problem is I'd like this function accepts a string too, out of a variable. But PHP returns with a fatal error (PHP Fatal error: Only variables can be passed by reference in ...). At the code, a('text out').

So, how can I fix this code to accept a variable or a string correctly?

code (edited):

function a(&$var){
    $backtrace = debug_backtrace();
    $call = array_shift($backtrace);

    $line = $call['line'];
    $file = $call['file'];

    echo name($var)."<br>".$var."<br>".$line."<br>".$file;
}

$variable='text in';
a($variable);
a('text out');

I need pass the variable by reference to use this function below (the function get the variable name correctly, works with arrays too):

function name(&$var, $scope=false, $prefix='unique', $suffix='value'){
   if($scope) $vals = $scope;
   else      $vals = $GLOBALS;
   $old = $var;
   $var = $new = $prefix.rand().$suffix;
   $vname = FALSE;
   foreach($vals as $key => $val) {
      if($val === $new) $vname = $key;
   }
   $var = $old;
   return $vname;
}

Upvotes: 0

Views: 946

Answers (2)

Mahn
Mahn

Reputation: 16585

The only way to do what you are asking without writing an additional function like @newfurniturey suggests is plain and simply opening and parsing the file where your function was called as text (e.g. with fopen), using the data from debug_backtrace. This will be expensive in terms of performance, but it might be ok if used only for debugging purposes; and using this method you will no longer need a reference in your function, which means you can freely accept a literal as the parameter.

Upvotes: 0

newfurniturey
newfurniturey

Reputation: 38416

The way your code is currently implementing pass by reference is perfect by design, but also by design cannot be changed to have two a() methods - one accepting a variable by reference and the other as a string-literal.

If the desire to pass a string literal instead of assigning it to a variable first is really needed, I would suggest creating a second convenience method named a_str() that actually accepts a string-literal instead of a variable by reference. This method's sole-purpose would be to relay the variable(s) to the original a() method - thereby declaring a variable to pass by reference.

function a_str($var) {
    a($var);
}

The only thing to remember is, use a($variable); when passing by reference and a_str('some text'); when not.

Here is the same convenience-method for your name() function:

function name_str($var, $scope=false, $prefix='unique', $suffix='value'){
    return name($var, $scope, $prefix, $suffix);
}

Upvotes: 1

Related Questions