Jump to content

Get variable name of object from within the object


haku

Recommended Posts

I'm wondering if it's possible to recover the name of the variable that has been created as an object, from within that object.

 

For example, lets say I have a class named 'person'. I create a new instance of person named 'haku':

 

$haku = new person;

 

I want to create a method inside of the person class to recover the name 'haku'. It would be something like:

 

<?php
class person
{
  var persons_name;
  function __construct()
  {
    $this->persons_name = ____________; // this is where I want to store the name 'haku'
  }
}
?>

 

I kind of suspect it's not possible, but I'm wondering if anyone has any ideas on whether it can be done.

Link to comment
Share on other sites

By 'recover' I mean 'get'. Not sure why I chose that as a word :)

 

And yes, you are right about the variable, but I was just typing that out as an example, my actual class is a different name and significantly more complicated, so I just wanted to throw up a simple example.

Link to comment
Share on other sites

I don't think you can haku because in the construct method, the variable $haku has yet to be defined. Perhaps if you were to have an empty constructor an an init or set method that takes the variable $haku after it's been defined.

 

Like

class Person {
     private $name;
     public function __construct () {
     }
     public function setName ($var) {
          // define it here. Use get_defined_vars() with spl_object_hash() to check
          // if $var is equal to $this. If so, get the key, which would be 'haku'.
     }
}

$haku = new Person;
$haku->setName($haku);

 

Kind of redundant. I'm not sure how else to do it. I know $haku has to be defined before you can use it.

Link to comment
Share on other sites

Well, it's a bad way of doing it, but you can do this -

 

class Person {
     private $name = '';
     
     public function __construct () {}
     
     public function setName ($person, $all_vars) {
          $this_hash = spl_object_hash($this);
          foreach ($all_vars as $key => $val) {
               if ($val instanceof Person) {
                    $hash = spl_object_hash($val);
                    if ($hash === $this_hash) {
                         $this->name = $key;
                         break;
                    }
               }
          }
     }
     
     public function getName () { return $this-> name; }
}

$haku = new Person;
$Ken2k7 = new Person;

$all_vars = get_defined_vars();
$haku->setName($haku, $all_vars);
$Ken2k7->setName($Ken2k7, $all_vars);

echo $haku->getName();
echo $Ken2k7->getName();

 

That should work. You may want to make it less ... tacky, but it's going to be tacky regardless.

Link to comment
Share on other sites

I'm working on this project where we are required to use a particular CMS as base for the code. The developers of this CMS though it would be a mighty clever idea to screw over PHP's object model. I can assure you it isn't clever, but just incredibly retarded and frustrating, or as one of the people I work with said, "the developers should be shot on sight". Just leave things to work like they normally do, and how you would expect them to work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.