Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hello,
What I would like to be able to do is get the name of the the instance of an object from inside the object. For example:
Class foo
{
var $somevar;
foo($somevar)
{
$this->$somevar = somevar;
}
function getMyInitializedName()
{
//this is what I need help with
}
}
$objName = new foo($somevar);
print($objName->getMyInitializedName());
//obviously this won't print anything. What I need the getMyInitializedName function to return though is "objName"
Does anyone have any ideas? Ofcourse I could always pass the object it's own name at initialization but it seems a little redundant if there is a built in way to get the name of the pointer.
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Be aware class names are case sensitive in PHP5, but that PHP4 usually returns the class name in lowercase (uppercase characters are converted to lowercase). If using PHP4 know the difference, and adjust for it if possible. I usually manipulate the return under PHP4 to put the uppercase back on certain characters with ucfirst(), and this is far easier if you use certain naming conventions.
$objectName = new Object();
$defined_vars = get_defined_vars(); // return local scope variables
// get array of variable names (these are the keys in returned value)
$variable_name_array = array_keys($defined_vars);
// easy way is to count local variables. We had only 1 prior to get_defined_vars() call.
// this corresponds to array index 0
$object_variable_name = $variable_name_array[0]; // "objectName"
The only way I can think of involves using debug_backtrace() to find the file then literally loading the source file on that line and parsing it. If your function needs to know the name of the variable passed to it, give it the name. Plain and simple.
Honestly, I don't see much use in knowing the specific name of the variable used for any actually helpful reason.
Maugrim_The_Reaper wrote:You're right - looking for the object variable name...
No, he is looking for the Object name, not the class name. Object != class. Objects are derived from Classes, but they are most definately not the same thing.
No, he is looking for the Object name, not the class name. Object != class. Objects are derived from Classes, but they are most definately not the same thing.
Does that mean my original response answered the question? This is way too confused for me to deal with - I'm on vacation and just about to have lunch. This is not a time for brain teasers...
Your second reply was closer (the example was pretty much as spot on as any can be) but the terminology was a bit off in both your first and second replies (where you refer to a Class as an Object)
Jenk wrote:Your second reply was closer (the example was pretty much as spot on as any can be) but the terminology was a bit off in both your first and second replies (where you refer to a Class as an Object)
I'm just being pedantic, that is all
Maugrim didn't refer to a class as an object in his second explanation. His first one wasn't what he belived was the answer to the question neither... he misread the question initially.
Object variable name == Name of Object (no mention of classes).