Shylux
Shylux

Reputation: 1293

How should i store settings variables in php?

I am currently writing a program in php. I want to let the user of my script change some settings, like database location and user. These variable are placed on the first lines of the script.

Now i want to access these variables from everywhere of the script like in classes and functions.

I used globals for this. But a colleague told me not to use globals.

So what is the common way to store and access settings in php?

Upvotes: 5

Views: 780

Answers (5)

Oliver A.
Oliver A.

Reputation: 2900

Configuring a script by changing the variables inside the script is a bad idea.

The simplest way to store config setting is by using a .ini file and parse it with the parse_ini_file function. http://php.net/manual/en/function.parse-ini-file.php

sample ini:

[db]
host = "foo.org"
user = "dbguy"

[session]
timeout = 600

If your script is object oriented you should wrap the config setting in a class ... and pass it as a constructor argument to your main class.

Else you can use the global array but please do not use the 'global' keyword in your functions! your functions should accept settings from your functions as parameters.

$config = parse_ini_file("protectedFolder/config.ini", true);

//wrong, do not do it this way
function connect(){
  global $config;
  $connection = dostuff($config['db']['user'],$config['db']['host'];
  //do stuff
}

connect();

//right
function connect2($user, $host){
  $connection = dostuff($user, $host);
  //do stuff
}

connect2($config['db']['user'], $config['db']['host']);

This is a very simple concept, and there a better ways to do this, but it should get you started and it will do the job for simple apps.

If you need something more sophisticated you should google for "dependency injection"

EDIT: edited comments EDIT2: fixed missing quotes

Upvotes: 3

Shoe
Shoe

Reputation: 76240

You could use constants which are global by default:

define('APPLICATION_SETTING', true);

Otherwise you could use a Registry Singleton class in which you load the settings and store them in a class private variable. Classic Registry class structure:

class Registry {
    private $instance = NULL;
    private $vars = array();
    private __construct() {/**/}
    public static function getInstance() {/**/}
    public function get($key) {/**/}
    public function set($key, $value) {/**/}
}

And no, I suggest you not to use Injections and stuff patterns. Just use Singleton. With Injection you get everything worse and more complicated.

Upvotes: 6

Marijn van Vliet
Marijn van Vliet

Reputation: 5399

You colleague is wrong in this case. Globals are the most convenient way to go about storing settings that need to be available everywhere. That's what a global is, something that is available everywhere. PHP is already full of globals, such as $_REQUEST. Put your settings in a single global array called $SETTINGS is my advice.

Upvotes: -1

pgl
pgl

Reputation: 7981

There are several methods. Here are a few options:

  • You could use a constant, if the variable isn't going to change.
  • You could use a singleton config object (although singletons in general are frowned upon by many; personally I think in the case of global configuration they can be quite useful).
  • Also, you can just pass around variables as function arguments.

It really depends on the application you're developing and personal preference. Global variables do tend to get awkward using them in the long run, though.

Upvotes: 1

LostMohican
LostMohican

Reputation: 3144

You can have a simple sqlite db in the server maybe to put the configuration settings that can only be reached by your php program. By encapsulating the query of sqlite to one simple class, you dont have to use globals.

Upvotes: 0

Related Questions