Retrieve instance of object name from inside said object

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.

Moderator: General Moderators

Post Reply
bliese
Forum Newbie
Posts: 1
Joined: Fri Jun 23, 2006 8:10 am

Retrieve instance of object name from inside said object

Post by bliese »

Pimptastic | Please use

Code: Select all

,

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

,

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]
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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:

Code: Select all

$class_name = get_class($this);
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.
User avatar
quocbao
Forum Commoner
Posts: 59
Joined: Sat Feb 04, 2006 2:03 am
Location: HCM,Vietnam
Contact:

Post 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__;
      }
}


?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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"
paranoiq
Forum Newbie
Posts: 1
Joined: Tue Jun 27, 2006 2:32 am

generalized

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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...:)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :P
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I quoted the second reply instead of the first, so sue me pedant :P
Post Reply