Is there a way to get the name of a variable? PHP - Reflection

前端 未结 11 633
旧巷少年郎
旧巷少年郎 2020-12-15 08:50

I know this is not exactly reflection, but kind of. I want to make a debug function that gets a variable and prints a var_dump and the variable name.

Of course, when

11条回答
  •  爱一瞬间的悲伤
    2020-12-15 09:11

    Not elegantly... BUT YOU COULD FAKE IT!

    • 1) Drink enough to convince yourself this is a good idea (it'll take a lot)
    • 2) Replace all your variables with variable variables:
    $a = 10
    //becomes
    $a = '0a';
    $$a = 10;
    
    • 3) Reference $$a in all your code.
    • 4) When you need to print the variable, print $a and strip out the leading 0.

    Addendum: Only do this if you are

    • Never showing this code to anyone
    • Never need to change or maintain this code
    • Are crazy
    • Not doing this for a job
    • Look, just never do this, it is a joke

提交回复
热议问题