Get object name from within

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Get object name from within

Post by Ward »

I don't know if this is possible, but I'm hoping it is. I'm trying to get an objects run-time variable name from within itself. For example, if I have this code:

Code: Select all

class foo
{
     public $firstName = "Billy";
     public $lastName = "Bob";
     
     function __construct()
     {
          // how can i get the variable name 'myFooInstance' from here?
     }
}

$myFooInstance = new foo();
The only solution I can think of is to pass each class it's unique variable name, like this:

Code: Select all

$myFooInstance = new foo('myFooInstance');
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i don't know if it spossiable, but out of curiosity why do you want to do this? and if your working in your class why cant you reefer to it as $this?
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post by Ward »

I'm trying to create loadViewState() and saveViewState() methods that would apply to every object. These would actually be called in each objects __construct() and __destruct() methods. However, I need each instance of an object to have a unique ID for this to work. I was hoping to make it automatic, but I may have to manually assign each object a unique ID.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

its possible, using debug backtrace.
possible doesn't mean sensible though.

stick with the string in the constructor there is nothing wrong with that or if you want to create an internal id that the user of the class is unaware of use a static property like this:

Code: Select all

class Foo 
{
    private static $instanceIndex = 0;
    private $id;
    public function __construct()
    {
        $this->id = self::$instanceIndex++;
    }
    public function getId()
    {
        return $this->id;
    }
}

$a = new Foo();
$b = new Foo();
$c = new Foo();

echo $a->getId(); // 0
echo $c->getId(); // 2
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ole wrote:or if you want to create an internal id that the user of the class is unaware of use a static property like this:

Code: Select all

class Foo 
{
    private static $instanceIndex = 0;
    private $id;
    public function __construct()
    {
        $this->id = self::$instanceIndex++;
    }
    public function getId()
    {
        return $this->id;
    }
}

$a = new Foo();
$b = new Foo();
$c = new Foo();

echo $a->getId(); // 0
echo $c->getId(); // 2
Ha! That's genius! Not sure where I'd ever need to use that though :)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I found a use for it the other day. I'm writing a form management package and I wanted to generate ids attribute for all the fields, this was perfect.
I ditched it in the end in favor of forcing the user of the package to specify their own
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A thread on this from a few days ago: viewtopic.php?t=51311
Post Reply