Created
August 31, 2011 11:46
-
-
Save catchamonkey/1183365 to your computer and use it in GitHub Desktop.
Get the name of a variable in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
... yes it's nasty
... no it isn't used, just POC...