Global Object vs property

Which would be better?

I have a stdClass of settings.

If I need the settings in a class would it be better to global the settings object in each of the methods that need the settings or to pass the settings to a property and access them from the property?


Method One:

public function example () {
    global $settings;
    echo $settings->test;

Method Two:

public $settingsProperty = $settings;

public function example () {
    echo $this->settingsProperty->test;

Pass the settings in upon instantiation of the class and store locally. Globals are bad, avoid at all costs.

“upon instantiation of the class and store locally”

Do you think you could explain that a little more? I don’t understand.

thanks :).

At some point, you have to have some objects that are global (at least one).

If the system you are writing is already object based (i.e., you basically call create a new instance of one class and it starts a chain reaction that gets the whole site going), then I would keep it inside that master class.

If it’s not and you use the same settings for everything and they don’t change, you can create a Singleton (google “singleton” if you aren’t familiar with it). It’s a class that can be accessed from anywhere without having to pass it around (which is why I like it).

If it’s not and you want to have different setting objects, then you’ll have to create them as global objects and pass them into your other objects through the constructor or somewhere else.

(not tested, hopefully you get the idea though)


class Whatever {

	public function __construct($settings) {
		$this->settings = $settings;
	public function doSomething() {
		echo "<p>Blah {$this->settings->foo}</p>";


$settings = new StdClass();
$settings->foo = 'twiddle dee';

$whatever = new Whatever($settings);

Globals aren’t always bad (though 99% of the time they are =p).

If they need to be globally accessed, another alternative is singletons. They aren’t the end all, be all, but in many situations they work well. Though, a singleton would only work if ALL instances share the same exact copy of the singleton (hence SINGLEton, there can be only one).

If it’s not, then definitely pass it in as parameter. If it needs to change, make sure it’s being passed as a reference. Normally it is, but if you pass it around a lot or pass it funny or something it might not.

If they need to update their

Globals are bad unless your referring to a constant, then its ok. All other cases use dependency injection or something anything other then globals. That is unless of course your working on a system that is complete rubish in the first place, then go global crazy if its the only way to do thing. However, in the “ideal” world avoid globals like the plague. When I see “global $foo” I die a little inside. Then again working with Drupal and its wonderful contributed modules I would probably have been dead long ago if that were true.

Thanks all :).

So if I have a bunch of settings which I grab from the database depending on which settings I need. Would it be better to have a global array or object of the needed settings or to get them individually in their functions using multiple queries.

Basically, what’s better? one query and a global array or object or multiple queries and no global.


Thanks, one last question. Is it ok not to have a master class and just call classes from the main script rather than inside a class?

Generally, that’s an acceptable method with PHP.

There is a lot of debate back and forth on this, and you’ll hear experts arguing both ways. They both have advantages and disadvantages. I generally go the master class route, but stuff like Wordpress doesn’t, so I have to confirm to their approach when I do that.

Awesome, thanks for all your help.