在PHP中,如何将参数名称转换为字符串

时间:2009-05-29 18:32:44

标签: php string arguments

我的目标是回应传递给函数的参数。例如,如何做到这一点?

$contact_name = 'foo';

function do_something($some_argument){
// echo 'contact_name'  .... How???
}

do_something($contact_name);

9 个答案:

答案 0 :(得分:4)

你做不到。如果你想这样做,你也需要传递名称,例如:

$contact_name = 'foo';
$contact_phone = '555-1234';

function do_something($args = array()) {
    foreach ($args as $name => $value) {
        echo "$name: $value<br />";
    }
}

do_something(compact('contact_name', 'contact_phone'));

答案 1 :(得分:1)

直接离开PHP.net variables页面:

<?php
  function vname(&$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;
  }
?>

答案 2 :(得分:1)

跟踪变量名称您可以抛出并捕获异常。 在倒数第二行跟踪中是有关此函数名称和变量名称的信息。

示例如何使用此:http://blog.heintze.pl/2012/01/12/lepszy-var_dump-czyli-przyjemniejsze-debugowanie-php/创建更好的var转储(链接到代码:http://blog.heintze.pl/wp-content/uploads/2012/01/bigWeb.zip

不幸的是 - 这篇文章目前仅在波兰语中。

答案 3 :(得分:0)

不可能。

答案 4 :(得分:0)

变量只是解决内存中值或区域的方法。您无法获取已将值传递给函数的变量名称。

答案 5 :(得分:0)

免责声明:如果您将变量传递给函数而不是值,这将会起作用,并且仅当您不在函数或类中时它才有效。因此,只有GLOBAL范围有效:)

Good funct($var)
Bad funct(1)

你可以做到这一点实际上与流行相信相反^ _ ^。但它涉及一些带有$ GLOBALS变量的查找技巧。

你是这样做的:

$variable_name = "some value, better if its unique";

function funct($var) {
   foreach ($GLOBALS as $name => $value) {
      if ($value == $var) {
         echo $name; // will echo variable_name
         break;
      }
   }
}

这种方法不是万无一失的。因为如果两个变量具有相同的值,则该函数将获得它找到的第一个变量的名称。不是你想要的人:P 如果你想要变量名的准确性,那么最好让变量值在手边变得独一无二

另一种方法是使用引用如此准确

$variable_name = 123;

function funct(&$var) {

   $old = $var;
   $var = $checksum = md5(time());  // give it unique value

   foreach ($GLOBALS as $name => $value) {
      if ($value == $var) {
         echo $name; // will echo variable_name
         $var = $old; // reassign old value
         break;
      }
   }
}

所以完全有可能:)

答案 6 :(得分:0)

基于PITBULL(最绝对正确)的答案,我提出了一种更具可读性(至少我认为是这样)的方法:

/**
 * returns the name of the variable posted as the first parameter.
 * If not called from global scope, pass in get_defined_vars() as the second parameter
 *
 * behind the scenes:
 *
 *   this function only works because we are passing the first argument by reference.
 *   1. we store the old value in a known variable
 *   2. we overwrite the argument with a known randomized hash value
 *   3. we loop through the scope's symbol table until we find the known value
 *   4. we restore the arguments original value and
 *   5. we return the name of the symbol we found in the table
 */
function variable_name( & $var, array $scope = null )
{
    if ( $scope == null )
    {
        $scope = $GLOBALS;
    }

    $__variable_name_original_value = $var;
    $__variable_name_temporary_value = md5( number_format( microtime( true ), 10, '', '' ).rand() );
    $var = $__variable_name_temporary_value;

    foreach( $scope as $variable => $value )
    {
        if ( $value == $__variable_name_temporary_value && $variable != '__variable_name_original_value' )
        {
            $var = $__variable_name_original_value;
            return $variable;
        }
    }
    return null;
}

// prove that it works:

$test = 1;
$hello = 1;
$world = 2;
$foo = 100;
$bar = 10;
$awesome = 1;

function test_from_local_scope()
{
    $local_test = 1;
    $local_hello = 1;
    $local_world = 2;
    $local_foo = 100;
    $local_bar = 10;
    $local_awesome = 1;

    return variable_name( $local_awesome, get_defined_vars() );
}
printf( "%s\n", variable_name( $awesome, get_defined_vars() ) ); // will echo 'awesome'
printf( "%s\n", test_from_local_scope() ); // will also echo awesome;

答案 7 :(得分:0)

桑德有正确的答案,但这是我一直在寻找的东西:

$contact_name = 'foo';

function do_something($args = array(), $another_arg) {
    foreach ($args as $name => $value) {
        echo $name;
        echo '<br>'.$another_arg;
    }
}

do_something(compact(contact_name),'bar');

答案 8 :(得分:-3)

class Someone{
  protected $name='';
  public function __construct($name){
    $this->name=$name;
  }

  public function doSomthing($arg){
     echo "My name is: {$this->name} and I do {$arg}";
  }
}

//in main
$Me=new Someone('Itay Moav');
$Me->doSomething('test');