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

Archived

This topic is now archived and is closed to further replies.

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