How to get a variable name as a string in PHP?

前端 未结 24 1401
南旧
南旧 2020-11-22 01:35

Say i have this PHP code:

$FooBar = \"a string\";

i then need a function like this:

print_var_name($FooBar);
24条回答
  •  你的背包
    2020-11-22 01:48

    I made an inspection function for debugging reasons. It's like print_r() on steroids, much like Krumo but a little more effective on objects. I wanted to add the var name detection and came out with this, inspired by Nick Presta's post on this page. It detects any expression passed as an argument, not only variable names.

    This is only the wrapper function that detects the passed expression. Works on most of the cases. It will not work if you call the function more than once in the same line of code.

    This works fine: die(inspect($this->getUser()->hasCredential("delete")));

    inspect() is the function that will detect the passed expression.

    We get: $this->getUser()->hasCredential("delete")

    function inspect($label, $value = "__undefin_e_d__")
    {
        if($value == "__undefin_e_d__") {
    
            /* The first argument is not the label but the 
               variable to inspect itself, so we need a label.
               Let's try to find out it's name by peeking at 
               the source code. 
            */
    
            /* The reason for using an exotic string like 
               "__undefin_e_d__" instead of NULL here is that 
               inspected variables can also be NULL and I want 
               to inspect them anyway.
            */
    
            $value = $label;
    
            $bt = debug_backtrace();
            $src = file($bt[0]["file"]);
            $line = $src[ $bt[0]['line'] - 1 ];
    
            // let's match the function call and the last closing bracket
            preg_match( "#inspect\((.+)\)#", $line, $match );
    
            /* let's count brackets to see how many of them actually belongs 
               to the var name
               Eg:   die(inspect($this->getUser()->hasCredential("delete")));
                      We want:   $this->getUser()->hasCredential("delete")
            */
            $max = strlen($match[1]);
            $varname = "";
            $c = 0;
            for($i = 0; $i < $max; $i++){
                if(     $match[1]{$i} == "(" ) $c++;
                elseif( $match[1]{$i} == ")" ) $c--;
                if($c < 0) break;
                $varname .=  $match[1]{$i};
            }
            $label = $varname;
        }
    
        // $label now holds the name of the passed variable ($ included)
        // Eg:   inspect($hello) 
        //             => $label = "$hello"
        // or the whole expression evaluated
        // Eg:   inspect($this->getUser()->hasCredential("delete"))
        //             => $label = "$this->getUser()->hasCredential(\"delete\")"
    
        // now the actual function call to the inspector method, 
        // passing the var name as the label:
    
          // return dInspect::dump($label, $val);
             // UPDATE: I commented this line because people got confused about 
             // the dInspect class, wich has nothing to do with the issue here.
    
        echo("The label is: ".$label);
        echo("The value is: ".$value);
    
    }
    

    Here's an example of the inspector function (and my dInspect class) in action:

    http://inspect.ip1.cc

    Texts are in spanish in that page, but code is concise and really easy to understand.

提交回复
热议问题