Jump to content

Get the name of a PHP variable as a string


utexas_pjm

Recommended Posts

I'm trying to get the name of a php instance variable passed to a function, like the following:

[code]
<?php
$foo = newSomeObject();

echo getVarName(&$foo);

// Prints foo
?>
[/code]

I've found some soluitions that involve traversing the $GLOBALS array and checking the values for equality but seems like an inefficent hack.  Maybe someone could give me a hand?

Best,

Patrick
Link to comment
Share on other sites

Yeah, sorry about the explanation.  I want anyone's class to be able send my class object instances like the following:

[code]
<?php
class foo
{
  someFunction()
{
    $bar = new bar();
    $z = new someArbitraryObject();
    $bar->register($bar);
}
}



class bar
{
  var $registry = array();
  function register(&$y)
  {
    $this->registry[$this->getName($y)] =& $y;
  }

  function getName(&$z)
  {
    // This is the code I'm looking for.
  }
}
?>
[/code]

Like I stated earlier I'm begininng to question what I'm trying to do here is possible within the confines of the variable scope.  Hope this helps to clarify.
Link to comment
Share on other sites

I'm extending an AJAX class that allows you to call methods from an existing class.  The problem is that their implentation breaks down if you try register methods from within a class because they use the global keyword in order to gain access to the object instance.  I was going to try to work around this by passing the instance itself by reference and grabbing the name of the orignial instance variable such that the user could make ajax requests on the instance iteself.  I think I'm going to end up needing to pass a label for the instance and the instance to the class.  I appreciate your time.

Best,

Patrick
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.