note 88074 added to language.variables
| From: | mccarreatuwindsordotcanada at osu1 dot php dot net | Date: | Thu, 08 Jan 2009 20:19:12 +0000 |
| Subject: | note 88074 added to language.variables | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148968@lists.php.net to get a copy of this message | ||
I needed a form of refelction for my work that still uses 4.2.0 that gets a variables name in
context with the application.
For example
[code]class someclass{
var $field1
function getFieldsName(){
print getVarName($this->field1);
}
}
myObject = new someclass();
myObject->getFieldsName();
//This will return "myObject->field1".
[/code]
If you want to change the format you can do so with the code, my code isn't fully complete for
features, but it does the basics of what is necessary for getting a variables name and it's
owners.
[code]
function getVarName(&$var)
{
$old = $var; //store the value of the var so we don't lose it.
$var = "__random__" . rand() . "temp"; //assign a random string to distinguish
this var from others.
//search for the random string to find the variable in $GLOBALS
//Note: this is an iterative deepening depth first search.
$key = IDDF_search(array_reverse($GLOBALS), $var); //we reverse the global array first to make it
faster,
//because variables appear at the bottom of hte globals array list.
$var = $old;
return $key;
}
function IDDF_search($array, $var, $depth=100)
{
for($i=0; $i<$depth; $i++)
if($temp = RestrictedDF_search($array, $var, $i))
return $temp;
}
function RestrictedDF_search($array, $var, $depth=100, $inArray=false)
{
if($depth < 0) //we've gone to our maximum depth, so return.
return false;
$depth--;
$possibleCloseBracket = ($inArray) ? "]" : "";
foreach($array as $key => $value) {
if(is_string($value) && $value === $var)
return $key . $possibleCloseBracket;
//else
//print $key . ": the value != " . $var . "<br />";
if(is_array($value))
if($temp = RestrictedDF_search($value, $var, $depth, true))
return $key . $possibleCloseBracket . "[" . $temp;
//PHP 4.2.0 =)
if(is_object($value))
if($temp = RestrictedDF_search(get_object_vars($value), $var, $depth))
return $key . $possibleCloseBracket . "->" . $temp;
}
return false;
}
[/code]
This was built of a function noted here at an earlier date. I think he actually named his function
getVarName. Mine takes it a step further combining the recursive search defined in the array_search
notes as well, so a user can get the name of a variable no matter where it is in GLOBALS.
If only I had php 5 to work with at work. =P
----
Server IP: 69.147.83.197
Probable Submitter: 142.46.80.82
----
Manual Page -- http://www.php.net/manual/en/language.variables.php
Edit -- https://master.php.net/note/edit/88074
Del: integrated -- https://master.php.net/note/delete/88074/integrated
Del: useless -- https://master.php.net/note/delete/88074/useless
Del: bad code -- https://master.php.net/note/delete/88074/bad+code
Del: spam -- https://master.php.net/note/delete/88074/spam
Del: non-english -- https://master.php.net/note/delete/88074/non-english
Del: in docs -- https://master.php.net/note/delete/88074/in+docs
Del: other reasons-- https://master.php.net/note/delete/88074
Reject -- https://master.php.net/note/reject/88074
Search -- https://master.php.net/manage/user-notes.php