utexas_pjm Posted January 9, 2007 Share Posted January 9, 2007 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2007 Share Posted January 9, 2007 echo '$foo'Will print out $foo. Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 9, 2007 Author Share Posted January 9, 2007 Sorry, the idea is I don't know what the name of the variable is to begin with. $foo was just an arbitrary exaple. I'm beginining to think that what I'm trying to do is not possible because of variable scope. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2007 Share Posted January 9, 2007 How could you use a variable and not know it's name? Can you give a better example? Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 9, 2007 Author Share Posted January 9, 2007 Yeah, sorry about the explanation. I want anyone's class to be able send my class object instances like the following:[code]<?phpclass 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2007 Share Posted January 9, 2007 Dunno.Why are you trying to do this, out of curiosity? Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 9, 2007 Author Share Posted January 9, 2007 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2007 Share Posted January 9, 2007 Best of luck Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.