从被调用函数中获取变量名称

发布于 2025-01-05 01:46:40 字数 274 浏览 2 评论 0原文

我可以使用 ReflectionClass 来做到这一点吗?

myprintr($some_object);


function myprintr(){
  foreach(func_get_args() as $key => $arg){

    // here I want to get the name of the passed variable, "some_object"
    // $key seems to be numeric...
  }


}

Can I do this, maybe using ReflectionClass ?

myprintr($some_object);


function myprintr(){
  foreach(func_get_args() as $key => $arg){

    // here I want to get the name of the passed variable, "some_object"
    // $key seems to be numeric...
  }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深居我梦 2025-01-12 01:46:40

您无法获取“变量”的名称,因为没有变量。

例如:

myprintr("test");
myprintr(myotherfun());

注意:我不确定你想做什么,但我只是觉得非常错误..函数和对象的全部要点是创建障碍,并且调用者的上下文中的内容并不重要..

You cannot get the name of the "variable", as there is no variable.

eg:

myprintr("test");
myprintr(myotherfun());

Note: I'm not sure what you are trying to do, but I just feels terrifyingly wrong.. the whole point of functions and objects is to create barriers, and it shouldn't matter what is in the caller's context..

迷爱 2025-01-12 01:46:40

如果用户将一个对象传递给 myprintr(),那么您可以使用它

if (is_object($arg)) {
    $className = get_class($arg);
}

来获取已传递的对象类型的名称,然后您可以将其提供给反射,

但反射构造函数将接受类名 或 < /em> 一个对象作为参数,所以你甚至不需要类名来实例化反射类

编辑

只是为了玩一下这个概念(并且不创建任何对全局变量),或者参数是否是变量、返回值来自函数、字符串等:

class Test{};

function myTest() {
    $some_object = new Test();
    myprintr($some_object);
}

function myprintr(){
    $callStack = debug_backtrace();
    $calledAt = $callStack[0];

    $callingFile = file($calledAt['file'],FILE_IGNORE_NEW_LINES);
    $callingLine = $callingFile[$calledAt['line']-1];
    $callingLine = substr($callingLine,strpos($callingLine,__METHOD__));
    $calledWithArgNames = trim(substr($matches[0],1,-1));

    var_dump($calledWithArgNames);

    $args = func_get_args();

    foreach($args as $arg) {
        var_dump($arg);
    }
}

myTest();

$some_object = new Test();
$some_other_object = &$some_object;

$t = 2;
$gazebo = "summer house";
$visigoth = pi() / 2; myprintr($some_other_object,pi(), atan2(pi(),$t), $visigoth, "Hello $t World", $gazebo); $t = log($t/$visigoth);

这会检索 $calledWithArgNames 中调用函数传递的所有参数,因此对于第一个调用,您有:

'$some_object'

对于第二个调用:

'$some_other_object,pi(), atan2(pi(),$t), $visigoth, "Hello $t World", $gazebo'

这仍然需要拆分为各个参数(逗号上的 preg_split,除非它们在大括号内),但肯定离您实际要求更近了一步。

If the user passes an object to myprintr(), then you can use

if (is_object($arg)) {
    $className = get_class($arg);
}

to get the name of the object type that has been passed, which you can then feed to reflection

but the reflection constructor will accept either a class name or an object as an argument, so you don't even need the class name to instantiate a reflection class

EDIT

Just for the sake of playing a bit with this concept (and not creating any dependency on globals), or on whether the arguments are variables, values returned from functions, strings, etc:

class Test{};

function myTest() {
    $some_object = new Test();
    myprintr($some_object);
}

function myprintr(){
    $callStack = debug_backtrace();
    $calledAt = $callStack[0];

    $callingFile = file($calledAt['file'],FILE_IGNORE_NEW_LINES);
    $callingLine = $callingFile[$calledAt['line']-1];
    $callingLine = substr($callingLine,strpos($callingLine,__METHOD__));
    $calledWithArgNames = trim(substr($matches[0],1,-1));

    var_dump($calledWithArgNames);

    $args = func_get_args();

    foreach($args as $arg) {
        var_dump($arg);
    }
}

myTest();

$some_object = new Test();
$some_other_object = &$some_object;

$t = 2;
$gazebo = "summer house";
$visigoth = pi() / 2; myprintr($some_other_object,pi(), atan2(pi(),$t), $visigoth, "Hello $t World", $gazebo); $t = log($t/$visigoth);

This retrieves all the arguments passed by the calling function in $calledWithArgNames, so for the first call you have:

'$some_object'

and for the second call:

'$some_other_object,pi(), atan2(pi(),$t), $visigoth, "Hello $t World", $gazebo'

This still requires splitting down into the individual arguments (a preg_split on commas, except where they're inside braces), but is certainly a step closer to what you're actually asking for.

弱骨蛰伏 2025-01-12 01:46:40

您无法访问不存在的参数名称:myprintr 未指定任何变量名称,并且 func_get_args() 将仅返回数字索引数组。

假设您可以添加文档块注释并通过反射访问它们,但这对于您很可能根本不需要的功能来说似乎是一个巨大的开销。使用反射函数的参数本身不会为您做任何事情,因为您没有在函数的参数签名中指定任何参数。

PHP 函数参数是有序的。它们不像关联数组那样可以引用。如果您想要访问函数或方法参数的“关联”类型键名称,则必须指定一个数组参数并传递一个带有所需关联键的值,如下所示:

myfunc(array $args=[])
{
  $key1 = isset($args['key1']) ? $args['key1'] : NULL;
  $key2 = isset($args['key2']) ? $args['key2'] : NULL;
}

You can't access argument names that don't exist: myprintr doesn't specify any variable names, and func_get_args() will only ever return a numerically indexed array.

I suppose you could add docblock comments and access them with reflection, but this seems like an extraordinary amount of overhead for functionality that you most likely don't need anyway. Using reflection on the function's arguments itself won't do anything for you because, again, you didn't specify any arguments in the function's argument signature.

PHP function arguments are ordered. They aren't something you can reference like an associative array. If you want access to "associative" type key names for a function or method's arguments, you'll have to specify an array argument and pass a value with the associative keys you want, like this:

myfunc(array $args=[])
{
  $key1 = isset($args['key1']) ? $args['key1'] : NULL;
  $key2 = isset($args['key2']) ? $args['key2'] : NULL;
}
不再让梦枯萎 2025-01-12 01:46:40

如果它是一个对象,您可以使用 func_get_args() 和 < a href="http://php.net/manual/en/function.spl-object-hash.php" rel="nofollow">spl_object_hash() 来识别对象,然后在$全局变量。在那里你可以找到这个名字。

class Test{};
$some_object = new Test();
myprintr($some_object);

function myprintr(){
  $args = func_get_args();
  $id = spl_object_hash($args[0]);
  foreach($GLOBALS as $name => $value)
  {
     if (is_object($value) && spl_object_hash($value) == $id) echo $name;
  }
}

http://codepad.org/gLAmI511

If it is an object you can use func_get_args() and spl_object_hash() to identify the object and then search it in $GLOBALS. There you find the name.

class Test{};
$some_object = new Test();
myprintr($some_object);

function myprintr(){
  $args = func_get_args();
  $id = spl_object_hash($args[0]);
  foreach($GLOBALS as $name => $value)
  {
     if (is_object($value) && spl_object_hash($value) == $id) echo $name;
  }
}

http://codepad.org/gLAmI511

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文