note 92747 modified in language.variables by danbrown

From: Date: Fri, 07 Aug 2009 05:48:05 +0000
Subject: note 92747 modified in language.variables by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159175@lists.php.net to get a copy of this message
Sometimes it's necessary to retrieve the value of $this from within an object. This solution dedicates a public var $ID to store a unique identifier for the object: <?php class Someclass { public $ID; public function what_is_this() { return '$this is: ' . $this->get_varname(); } private function get_varname() { $this->ID = uniqid(); foreach($GLOBALS as $key => $val) { if (is_a($val,'Someclass')) { if ($val->ID === $this->ID) { return $key; break; } } } } } // example: $my_var_instance = new Someclass; echo $my_var_instance->what_is_this(); // returns '$this is: my_var_instance' ?> --was-- // Sometimes it's necessary to retrieve the value of $this from within an object. // This solution dedicates a public var $ID to store a unique identifier for the object: class Someclass { public $ID; public function what_is_this() { return '$this is: ' . $this->get_varname(); } private function get_varname() { $this->ID = uniqid(); foreach($GLOBALS as $key => $val) { if (is_a($val,'Someclass')) { if ($val->ID === $this->ID) { return $key; break; } } } } } // example: $my_var_instance = new Someclass; echo $my_var_instance->what_is_this(); // returns '$this is: my_var_instance' http://php.net/manual/en/language.variables.php

« previous php.notes (#159175) next »