1

How do i get all defined global variables inside a class function?

When I call the get_defined_vars() function inside a class method I only get an empty object.

hakre
  • 193,403
  • 52
  • 435
  • 836
markcial
  • 9,041
  • 4
  • 31
  • 41
  • possible duplicate of [Define variables outside the PHP class](http://stackoverflow.com/questions/2505735/define-variables-outside-the-php-class) – hakre Aug 30 '12 at 08:14

3 Answers3

11

via => Define variables outside the PHP class

class Foo {
    function bar(){
       var_dump($GLOBALS);
    }
}
Foo::bar();

outputs :

array(8) {
  ["GLOBALS"]=>
    array(8) {
      ["GLOBALS"]=>
      *RECURSION*
      ["_POST"]=>
      array(0) {
      }
/*snip*/         
Community
  • 1
  • 1
markcial
  • 9,041
  • 4
  • 31
  • 41
2

Use get_class_vars(__CLASS__) if you want the predefined variables & values (those values defined and set as default before an object initialized).

Use get_object_vars($this) if you want all the variables (instance variables) including those which are created temporarily for a particular object while executing any method of the class.

I know these are not really the things you need. Its $GLOBALS that you need answered yourself. But I just gave these methods for your reference so that may be useful in the future.

Prashanth Shyamprasad
  • 827
  • 2
  • 17
  • 39
0

Maybe you can try get_class_vars()?

Florent
  • 12,310
  • 10
  • 49
  • 58
jholster
  • 5,066
  • 1
  • 27
  • 20
  • sorry maybe i had explain it wrong, i meant the wide site vars, like if get_defined_vars() was called in the main script. – markcial Mar 24 '10 at 08:54
  • I'm not aware of that being possible, because every function / method makes own scope. get_defined_vars() does return the variables defined in your class method, if you have any, but this is not helping you. – jholster Mar 24 '10 at 09:17