Alex
Alex

Reputation: 68084

Get variable name from within the called function

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


}

Upvotes: 1

Views: 944

Answers (4)

Mark Baker
Mark Baker

Reputation: 212522

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.

Upvotes: 2

Karoly Horvath
Karoly Horvath

Reputation: 96366

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..

Upvotes: 6

PiTheNumber
PiTheNumber

Reputation: 23563

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

Upvotes: 0

user895378
user895378

Reputation:

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

Upvotes: 1

Related Questions