1

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


}
16
  • 1
    You can't, unless you use something like debug_backtrace(), and there shouldn't really be any need for you to do so... is this a business requirement? or just exploring PHP? Commented Feb 14, 2012 at 15:42
  • I'm trying to create a print_r/var_dump kind of function for my API, and need this name in case the user passes a object and I'm listing the object methods. I want to prepend the variable name to the method names when displaying them, like $object->method... Commented Feb 14, 2012 at 15:44
  • Possibly answered here: stackoverflow.com/questions/255312/… Commented Feb 14, 2012 at 15:45
  • 2
    The var name used in the call has no meaning outside of the calling function... I can call myprintr() using $xyz in one place, $abc in another and "hello world" in a third. Most people use phpdocumentor blocks for API docs Commented Feb 14, 2012 at 15:46
  • 1
    @Alex: xdebug will show you the kind of stuff that you seem to be interested in seeing out of the box. Commented Feb 14, 2012 at 16:15

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

@PiTheNumber: that only works with globals... and if the objects is stored in multiple variables, how would you know which one to pick?
2

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.

1 Comment

I'm using this already as a prefix for static methods. For non-static methods I wanted to display that variable name, but if it's not possible I'' just use the class name ..
1

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

2 Comments

I'm in this to learn as well, so please let me know what's wrong with the answer when you downvote. Especially if there's a problem, that way it can be amended or removed. Thanks, retroactively, for being zero help.
Nothing as far as I can tell.
0

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

6 Comments

what if the object isn't defined in $GLOBALS?
@Alex - only works if the object is defined in $GLOBALS, forget about using it if the object is instantiated in a function or another object method in your code - codepad.org/NlwIdR9N
@PiTheNumber - getDefinedVars within the myprintr() function will only return varibles that are in scope within that function
@PiTheNumber: that only works with globals... and even then, if the objects is stored in multiple variables, how would you know which one to pick?
Knock yourself out with: $some_object = new Test(); $some_other_object = &$some_object; myprintr($some_other_object);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.