I apologize in advance if the question will seem to someone stupid and meaningless, but for me it makes sense. A function is needed which, for example, receives at the input $ var and at the output it produces the string 'var' ie the name of the filed variable in string form. Are there any ways to do this?

  • reflection ..... - Aleksey Shimanskyj
  • @ Alexey Shimansky Please write a minimal example or give a link to a resource that can answer the question. If not difficult of course - alexoander
  • $func = function ( $param1, $param2 ) { /* some code */ }; $refFunc = new ReflectionFunction($func); foreach ($refFunc->getParameters() as $refParameter) { echo $refParameter->getName(), '<br />'; } $func = function ( $param1, $param2 ) { /* some code */ }; $refFunc = new ReflectionFunction($func); foreach ($refFunc->getParameters() as $refParameter) { echo $refParameter->getName(), '<br />'; } ......... resource ..... uh ..... stackoverflow oddly enough)) stackoverflow.com/questions/2692481 and stackoverflow.com/questions/17455043 and stackoverflow.com/questions/ 5854976 , etc. - Alexey Shimansky
  • one
    Hmm ... weird .... and did the answer from And suit you? But this is a banal transfer of an argument to a function. With the same success you could just pass a string in the parameter - Alexey Shimansky

2 answers 2

The only way out is:

 echo substr('$var',1); 

That is, if you do not pass the variable in single quotes, that is, in a simple string, then nothing happens. In double quotes, the variable will execute and return a value.

 function variable($var) { return substr($var, 1); } $var1 = '$var'; echo variable($var1); // var 

Only now I do not know what this may be suitable. You can also try the get_defined_vars function get_defined_vars but it spits out everything.

UPD:

 function variable($variable, array $vars) { $variable = substr($variable, 1); $vars = isset($vars[$variable]) ? $vars[$variable] : 'null'; return $variable . ' = '. $vars; } echo variable('$var', get_defined_vars()); // var = 666 

With the exception that get_defined_vars only works with the scope where it was defined, it means that if we define it in a function, it will not see the variable beyond the scope of the function. I could see $GLOBALS , but registr_globals was deleted in 5.4, now it does not see.

  • and how to take value from a variable then? For example, if he wants to pass $ var with a value of 666? - Alexey Shimansky

We can get a call stack, subtract a line from the code, get a variable.

The method is clearly slow with a bunch of restrictions, but as an example ..

 function getVariableName($variable) { $backtrace = debug_backtrace()[0]; $file = file($backtrace['file']); $callLine = $file[$backtrace['line']-1]; $result = preg_match('/' . __FUNCTION__ . ' *\([^$]*(?P<varName>\$[^ ,)]+) *\)/ui', $callLine, $matches); if (!$result) { throw new Exception('Fix regexp'); } return $matches['varName']; } $foo = 'bar'; $baz = ['Hello', 'World']; echo getVariableName($foo); echo PHP_EOL; echo getVariableName($baz); // $foo // $baz 

Based on the answer to en.SO