PHP get the name of the given variable used using var_name()

Reply to comment

Thanks for posting your solution. It works for variables with "global" scope. To use var_name() with variables defined within a function you would have to scan the array returned by get_defined_vars() instead of $GLOBALS.

Would be great to have a class with objects that know their own name. E.g. $a1 = new varNameClass(); with a method: $a1->getName() returning the string 'a1'.