Page 1 of 1
Retrieve instance of object name from inside said object
Posted: Fri Jun 23, 2006 8:31 am
by bliese
Pimptastic | Please use Code: Select all
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:
Code: Select all
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.
Thanks in advance
Pimptastic | Please use Code: Select all
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]
Posted: Fri Jun 23, 2006 8:42 am
by Maugrim_The_Reaper
Please use PHP tags around PHP code
get_class()
This will return the object name, you use a call in your method as:
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.
Posted: Sun Jun 25, 2006 7:07 pm
by quocbao
Maugrim_The_Reaper has answered the question clearly , but i think you might want to know about __CLASS__ constant
Code: Select all
<?php
class MyClass
{
public function getClassName()
{
return __CLASS__;
}
}
?>
Posted: Mon Jun 26, 2006 1:27 am
by Chris Corbyn
Actually I think the OP wanted something that returns "$objName" as opposed to the name of the class. Object, not class.
Maybe a backtrace? I can't remember what information you can get from a backtrace.
Posted: Mon Jun 26, 2006 2:50 am
by Maugrim_The_Reaper
You're right - looking for the object
variable name...
debug_backtrace() should do this. Alternatively, it's possible to get the name of any variable within the local scope by using a mix of
get_defined_vars() and
array_keys(). e.g.
Code: Select all
$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"
generalized
Posted: Tue Jun 27, 2006 2:45 am
by paranoiq
is there any way to
obtain name of variable from reference to it? (maybe an object too - this is what bliese means).
here is what i mean:
Code: Select all
function foo(&$var){
//... returns the name
}
$bar = 'some value';
echo foo($bar); //prints 'bar'
[/b]
Posted: Tue Jun 27, 2006 8:18 am
by feyd
nope.
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.
Posted: Wed Jun 28, 2006 6:03 am
by Jenk
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.
Clash of terminology

Posted: Wed Jun 28, 2006 6:22 am
by dbevfat
I don't think this makes sense.
Code: Select all
$a = new MyObject();
$b = $a;
echo $b->getVariableName();
What should that output?
It can be done the way feyd wrote, but this is IMO not the right way. You should redesign so you don't need the variable name.
Posted: Wed Jun 28, 2006 8:47 am
by Maugrim_The_Reaper
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...

Posted: Wed Jun 28, 2006 8:52 am
by Jenk
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

Posted: Wed Jun 28, 2006 8:58 am
by Chris Corbyn
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).
Just being pedantic

Posted: Wed Jun 28, 2006 9:09 am
by Jenk
I quoted the second reply instead of the first, so sue me pedant
