1

We use instantiate and put system critical objects in $GLOBALS for easy access from anywhere (e.g. DB, Cache, User, etc.).

We use $GLOBALS so much that it would (yes, really) drop the amount of code quite a bit if I could reference it like $G = &$GLOBALS for a shorthand call.

The problem is that, per my experience and several hours of Googling, I have not found any construct in PHP which allows you to 'flag' a var as global, making $GLOBALS first class, and everything else second class.

Am I missing something? Is this possible?

Spot
  • 7,962
  • 9
  • 46
  • 55

5 Answers5

5
<?php

function &G($name) {
    if(func_num_args() > 1) {
        $GLOBALS[$name] = func_get_arg(1);
    }
    return $GLOBALS[$name];
}


G('test', 'hey');

echo G('test'); // outputs hey
echo $test; // outputs hey

$b =& G('test');
$b = 'hello';

echo G('test'); // outputs hello
echo $test; // outputs hello
Kamil Szot
  • 17,436
  • 6
  • 62
  • 65
2

Instead of putting everything into $GLOBALS you might want to take a look into the registry concept that is widespread in the php world.

However, having many global variables/objects is a sign of bad design and high coupling. Using something like $G guarantees spaghetti code and a soon-to-come maintenance nightmare. Who cares if you can drop the amount of code by a few characters?

middus
  • 9,103
  • 1
  • 31
  • 33
  • We do not put everything in `$GLOBALS`. There are maybe eight(8) objects in `$GLOBALS`. If something is system critical in that it is required on every pageload & is accessed very often, we put it in `$GLOBALS`. – Spot Mar 22 '10 at 09:37
  • Why not load the registry prior to any other operation ? – Benoit Mar 22 '10 at 10:59
  • If you downvote, please let me know why so I can learn from my mistakes. – middus Jun 12 '12 at 07:26
2

No, but you can use a little trick. Create a global function by the same name as your global var, and have it return the global instance. Eg.:

function db() {
  return $GLOBALS['db'];
}

You can now write your code as:

...
$stuffs = db()->query("select * from stuff");
...

You may recognise this as a variant over the singleton pattern, but a syntactically much more pleasant one.

Others have mentioned it, but you should also consider not using global objects in the first place. I generally prefer to pass objects in to there where it's needed (dependency injection). I'm not overly found of the registry pattern though.

troelskn
  • 115,121
  • 27
  • 131
  • 155
  • The global function idea is interesting. It does add another function call onto most system calls, but that might be worth it. To be clear, we literally have like 8 objects in GLOBALS, and they are instantiated objects which are used _constantly_. Our system is completely self-contained. We don't use any third-party code at all. We've actually had zero problems with it, in five+ years. I understand the concern over global data, but it doesn't really apply to this. We're quite careful about global data. We even put all our controllers in closures, just to be safe. :) Thanks for the response – Spot Jun 12 '12 at 00:28
1
global $variable; //?
Petr Peller
  • 8,581
  • 10
  • 49
  • 66
  • 1
    This only opens a hole in an established scope, it is not proactive in breaking through child scopes. – Spot Mar 22 '10 at 09:36
1

In addition to the registry concept Middus points out, there are several approaches and concepts around this, some of which you can find in the answers to this question:

In a PHP project, how do you organize and access your helper objects?

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088