This morning, Ed Griffiths asked on the CFCDev mailing list
if it was "possible to interrogate some method or metadata associated with [an object] in order to find out what variable name it has been stored within."
I first pointed out that Per Djurner (contributor to CF on Wheels
) and myself were chatting over IM about something similar - but with functions. His solution, which I thought was "awesomely creative" (to quote myself on the CFCDev list), was basically to do this: Throw an exception and catch it. Find the line number you are looking for, read the line number, and figure out the variable name (or in this case, function).
Of course, we both hope to find a better solution. Anyway, back to Ed's problem. From my understanding of his question, he just wanted to know the name of a variable who was holding a reference to a CFC. Responses ranged from things like "why would you want to do that?" to "there is probably a way," to "it's not possible because it may have many references" (these aren't true quotes, but paraphrases really). Even Hal Helms chimed in (and I am a fan of his, albeit just recently).
After I posted a function which I thought solves the problem, Teddy Payne mentioned "this looks academic," as in he couldn't find a use for it. I have to agree with Teddy, in that I can't find a use for it either. But, when some one has a tough problem that I can't see immediately how to find an answer to, I like to find an answer. That compulsion gets stronger as the number of "this can't be done" replies increases. =) (I said).
For Per, the problem is more than academic (and this doesn't quite solve it -- I'll investigate that tomorrow). We were discussing ways to dynamically decide what a function should do based on its name.
I started out searching through all the Java stuff to find a solution. When I didn't find one, I thought, why not just search through each scope and compare if the memory address is the same? Well, that didn't work well, but the function below did. I got the idea to insert a UUID key into the CFC and compare the variables in the scope to
from looking through Paul Kenney's cfcUnit
In any case, if you ever have the need, here's how you can find all the references to an object, but you need to provide the scope to search. This was done because I didn't take the time to figure out if there was a way to reference the "global" variables scope from within a CFC. Now, of course you may want to make it return a list of all the names, or to search all the scopes available, but you get the idea from this.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
<cfset this.testkey = createUUID()>
<cfif isStruct(variablesScope[name]) and structKeyExists(variablesScope[name],
"testKey") and variablesScope[name].testKey eq this.testKey>
Leave a comment
There are no comments for this entry yet.
Leave a comment