note 92747 added to language.variables
| From: | the_good_technician | Date: | Fri, 07 Aug 2009 04:32:55 +0000 |
| Subject: | note 92747 added to language.variables | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159172@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:
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'
----
Server IP: 209.41.74.194
Probable Submitter: 76.168.206.254
----
Manual Page -- http://www.php.net/manual/en/language.variables.php
Edit -- https://master.php.net/note/edit/92747
Del: integrated -- https://master.php.net/note/delete/92747/integrated
Del: useless -- https://master.php.net/note/delete/92747/useless
Del: bad code -- https://master.php.net/note/delete/92747/bad+code
Del: spam -- https://master.php.net/note/delete/92747/spam
Del: non-english -- https://master.php.net/note/delete/92747/non-english
Del: in docs -- https://master.php.net/note/delete/92747/in+docs
Del: other reasons-- https://master.php.net/note/delete/92747
Reject -- https://master.php.net/note/reject/92747
Search -- https://master.php.net/manage/user-notes.php