haku Posted May 14, 2009 Share Posted May 14, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158080-get-variable-name-of-object-from-within-the-object/ Share on other sites More sharing options...
Ken2k7 Posted May 14, 2009 Share Posted May 14, 2009 What do you mean by "recover"? Edit - shouldn't var persons_name be var $persons_name? Quote Link to comment https://forums.phpfreaks.com/topic/158080-get-variable-name-of-object-from-within-the-object/#findComment-833863 Share on other sites More sharing options...
haku Posted May 18, 2009 Author Share Posted May 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158080-get-variable-name-of-object-from-within-the-object/#findComment-836143 Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158080-get-variable-name-of-object-from-within-the-object/#findComment-836173 Share on other sites More sharing options...
Daniel0 Posted May 18, 2009 Share Posted May 18, 2009 No, you cannot. You'll have to pass the name you want to the object manually. Quote Link to comment https://forums.phpfreaks.com/topic/158080-get-variable-name-of-object-from-within-the-object/#findComment-836248 Share on other sites More sharing options...
Mchl Posted May 18, 2009 Share Posted May 18, 2009 Don't mix variable identifiers and application data. Quote Link to comment https://forums.phpfreaks.com/topic/158080-get-variable-name-of-object-from-within-the-object/#findComment-836257 Share on other sites More sharing options...
Masna Posted May 18, 2009 Share Posted May 18, 2009 Store itself in itself... Sounds like you're trying to cause the universe to implode. Quote Link to comment https://forums.phpfreaks.com/topic/158080-get-variable-name-of-object-from-within-the-object/#findComment-836264 Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158080-get-variable-name-of-object-from-within-the-object/#findComment-836559 Share on other sites More sharing options...
Daniel0 Posted May 18, 2009 Share Posted May 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/158080-get-variable-name-of-object-from-within-the-object/#findComment-836562 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.