3

Please look at the following two examples:

Example 1:

$variable = 'some value';

class Foo {
   public function bar() {
      global $variable;
      print $variable;       
   }
}

Example 2:

$variable = 'some value';

class Foo {
   public function bar() {
      print $GLOBALS['variable'];  
   }    
}

Examples can be used like this:

$foo = new Foo();
$foo->bar();

Both seem to do the same thing? What is the difference? Is one way better than the other? Why are there two different ways of doing this?

The first example seems strange to me because it looks like a declaration followed by using the variable without assigning it.. looks weird to me.

Thank you.

hakre
  • 178,314
  • 47
  • 389
  • 754
ale
  • 10,432
  • 23
  • 85
  • 137
  • [don't use globals](http://stackoverflow.com/questions/5166087/global-in-functions/5166527#5166527) – OZ_ Sep 15 '11 at 11:06
  • Globals aren't always bad. I have written a config.inc/config.php file that makes a connection to an Oracle DB. In this file the connection gets assigned to a variable. To do queries, I need to access this variable, otherwise I have to keep on making connections. So that's a valid use right? Tell me if it isn't. – ale Sep 15 '11 at 11:12
  • is not. Answer, which I linked here, has references to very useful articles - it will be not wasting of time to read them. For your case you need [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) or [DI container](http://martinfowler.com/articles/injection.html). – OZ_ Sep 15 '11 at 11:16
  • 1
    Whether globals are useful or not depends merely on style and application design. Globals introduce global state, like singletons and static classes do as well which has downsides. However, in a very direct, procedural programming style global state might not be wrong but very efficient if you can live with the downsides. Normally it prevents you to create larger applications however. In short maybe: Pretty okay for a short script, a burden for a full blown application you would like to extend over years. You can always re-factor if you still can afford it later on. Otherwise throw it away. – hakre Sep 15 '11 at 11:49

5 Answers5

6

The global keyword 'imports' the variable into the local scope, while accessing a global through $GLOBALS does not.

Delan Azabani
  • 73,106
  • 23
  • 158
  • 198
2

$GLOBALS is just an other way of accessing global variables. The global keywords imports global variables into the local scope. $GLOBALS can be used to access global variables without polluting the local scope.

It may be used to iterate over global variables, for example.

See $GLOBALS:

$GLOBALS — References all variables available in global scope

And the global keyword:

By declaring $a and $b global within the function, all references to either variable will refer to the global version.

A second way to access variables from the global scope is to use the special PHP-defined $GLOBALS array.

Community
  • 1
  • 1
Arnaud Le Blanc
  • 90,979
  • 22
  • 192
  • 188
1

One difference is in use. From the manual page

<?php
function test() {
    $foo = "local variable";

    echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
    echo '$foo in current scope: ' . $foo . "\n";
}

$foo = "Example content";
test();
?>

You can see that you can access this, global variable and a local variable. You couldn't do that when useing global $foo. There are other subtile differences, but nothing that matter I think.

You might want to ask yourself if you should be using the globals anyway, the way it is used is almost always an anti-pattern as far as i'm conserned, although this might be a taste-thing.

Nanne
  • 61,952
  • 16
  • 112
  • 157
1

$GLOBALS represents the whole global variable table in form of an Array.

The global keyword allows you to specify one or more variable labels, that are part of the global variable table within the local variable table. Often called import in the other answers.

There once was a time when $GLOBALS within a function scope did not update the global table immediately. But I think that is fixed now. At least I was not able to reproduce it any longer.

So the difference is merely the semantics how to write something.

However, there is a difference between the two. You can not unset global variables when you make them accessible within the local scope via the global keyword.

$var = 1;
foo();

function foo()
{
    global $var;
    unset($var); # won't unset the global variable
}

But you can by accessing them via $GLOBALS.

$var = 1;
foo();

function foo()
{
    unset($GLOBALS['var']); # unsets the global variable
}

This is pretty specific, if interested see Demo.

hakre
  • 178,314
  • 47
  • 389
  • 754
0

From php manual http://php.net/manual/en/language.references.whatdo.php

Think about global $var; as a shortcut to $var =& $GLOBALS['var'];

Muhammad Atif
  • 101
  • 2
  • 8