Skip to content

Instantly share code, notes, and snippets.

@catchamonkey
Created August 31, 2011 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catchamonkey/1183365 to your computer and use it in GitHub Desktop.
Save catchamonkey/1183365 to your computer and use it in GitHub Desktop.
Get the name of a variable in PHP
<?php
class VarName {
function get(&$var, $scope = FALSE, $prefix = 'unique', $suffix = 'value')
{
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
}
$routeName = 'my/api/route';
$varNamer = new VarName();
echo $varNamer->get($routeName)."\n";
@catchamonkey
Copy link
Author

... yes it's nasty
... no it isn't used, just POC...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment