-1

func(CONST_A) should return 'CONST_A', func($name) should return $name

How to implement this func in PHP?

compile-fan
  • 16,885
  • 22
  • 59
  • 73
  • possible duplicate of [How to get a variable name as a string in PHP?](http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php) – Marc B Apr 09 '11 at 03:39
  • @Marc, it works with variables,but not for CONSTANTS... – compile-fan Apr 09 '11 at 03:44
  • Set up another function and work off the data returned from `get_defined_constants()`. As far as I know, there is no way for a function tell if an argument it's given came from a variable or a constant. It only sees the contents of the var/constant, and nothing of the source. YOu'd have to set up two functions two do what you want. one for vars, one for consts. – Marc B Apr 09 '11 at 03:48
  • And that solution won't work at all if the thing passed is a literal instead of a variable or constant. Can you tell us *why* you want to do this? – Charles Apr 09 '11 at 04:07

3 Answers3

0

Try this:

<?php
function getVarConst($var)
{
    if (isset($GLOBALS[$var]) // check if there is a variable by the name of $var
    {
        return $GLOBALS[$var]; // return the variable, as it exists
    }
    else if (defined($var)) // the variable didn't exist, check if there's a constant called $var
    {
        return constant($var); // return the constant, as it exists
    }
    else
    {
        return false; // return false, as neither a constant nor a variable by the name of $var exists
    }
}
?>
  • It's not working for const, try `define('CONST_A', 'test');echo getVarConst(CONST_A);` – compile-fan Apr 09 '11 at 04:05
  • The names must be passed as strings. If you pass them as variables or constants, the function receives the value of the constant / variable. As far as I know, it's impossible to get the name of a constant or a variable from it's value, unless you loop through both $GLOBALS and get_defined_constants() looking for the values, and that'd be very stupid - PHP is executed on every request (parsed too, if you don't use an opcode cache). It'd make your app / script perform so bad, it wouldn't be worth it. – ShotgunToothpaste Apr 09 '11 at 04:09
  • To be honest, you shouldn't need this code unless your app needs to be able to get the value of a setting, no matter if the user defined it as a constant or a variable. E.g. you'd use it to make this work so `$db = 'myDb';` or `define('db', 'myDb');` would both be able to be used, with you abstracting through `getVarConst('db');` so you get the available value, with variable prioritised over constant. – ShotgunToothpaste Apr 09 '11 at 04:11
0

This doesn't seem possible easily.

You can use reflection to determine the parameters of the function, but that returns the variable names that the function expects, and if you're already inside the function, you kind of already know those.

debug_backtrace is the usual way of peeking at what called you, but it returns the values of the passed arguments, not the variable names or constants that the caller used when making the call.

However, what it does give you is the file name and line number of the caller, so you could open up the file and seek to that line and parse it out, but that would be very silly and you should not do this. I am not going to give you example code for this, as it is so utterly silly that you should not consider doing it ever.

The get_defined_vars thing is a hack and is not guaranteed to work either, and definitely won't work for constants, as get_defined_constants does that.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • You are calling me utterly silly. Well, the fact is that I do that in a cool function for object inspection that I use all the time. It's not for production environment, I admit it, but let me tell you it's really handy for debugging. – Sebastián Grignoli Jul 27 '11 at 05:36
0

This isn't possible.

You literally want the following right?

 define('CONST_A', 'THIS COULD BE ANYTHING');
 $name = 'who cares';

 func(CONST_A); //returns 'CONST_A'
 func($name);   //returns '$name'

The function can't know that.

I suppose that reading the source code like Charles describes could get you this, but why?

jon_darkstar
  • 16,398
  • 7
  • 29
  • 37
  • Even then, it probably wouldn't be able to work out things like `$name = myFunc('hiThere');` or the like without executing the file. In other words, OP's problem is pretty much impossible, definitely not viable in a live environment because of the performance hit and no way it can't be solved through a little refactoring... Well planned apps should never need this kind of thing. – ShotgunToothpaste Apr 09 '11 at 08:36