PHP.通过引用与字符串传递变量.如何处理这两个不同的论点? [英] PHP. Pass variable by reference vs string. How to works with these two different arguments?

查看:53
本文介绍了PHP.通过引用与字符串传递变量.如何处理这两个不同的论点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自己的调试函数,我需要一些帮助来修复下面的代码.

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

我正在尝试打印变量及其名称、声明变量和函数的文件以及函数调用的行.我做的第一部分,变量,变量名,文件和行打印正确.在代码中,a($variable) 效果很好.

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.

问题是我希望这个函数也接受一个字符串,而不是一个变量.但是 PHP 返回一个致命错误(PHP 致命错误:只有变量可以在 ... 中通过引用传递).在代码处,a('text out').

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?

代码(已编辑):

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;
}

推荐答案

您的代码当前实现的方式 pass by reference 是完美的设计,但设计不能更改为有两个 a() 方法 - 一个接受变量通过引用,另一个作为字符串文字.

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.

如果确实需要传递字符串文字而不是首先将其分配给变量,我建议创建第二个名为 a_str() 的便捷方法,该方法实际上接受字符串文字一个变量的引用.此方法的唯一目的是将变量传递给原始的 a() 方法 - 从而声明要通过引用传递的变量.

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);
}

唯一要记住的是,在通过引用传递时使用a($variable);,否则使用a_str('some text');.

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

对于您的 name() 函数,这里有相同的便捷方法:

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);
}

这篇关于PHP.通过引用与字符串传递变量.如何处理这两个不同的论点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆