8

How should I store settings for project?

Which is better - to use a $settings array with all my settings:

$settings['max_photos'] = 30;
//...

or create a singleton Config class with all the settings in it?

Class Config {
    private $max_photos = 30;
    //...
}

Any good examples?

Eric
  • 95,302
  • 53
  • 242
  • 374
ideea
  • 353
  • 1
  • 3
  • 10

7 Answers7

11

I think it is best to use constants for configuration. For example using class constants:

class Config {
    const
    max_photos     = 30,
    something_else = 100,
    // ...
    ;
}

echo Config::max_photos;

If you have PHP 5.3 you could also define them as global constants:

const MAX_PHOTOS = 30;

echo MAX_PHOTOS;

But I think this is far less clean and straightforward.

Obviously this will only work as long as you are only storing constants, i.e. scalar, non-expression values. If your configuration contains arrays for example, this won't work anymore. In this case I would use a Config class with public static properties:

class Config {
    public static $per_page = 30;
    public static $array = array(...);
}

echo Config::$per_page;

The latter is very similar to the $config array approach, but has the benefit (or may this be a drawback?) that the class is accessible from everywhere including functions and classes, whereas the array is accessible only in the global space unless you import it into functions/classes using global $config;.

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • 1
    +1 constants for constant values, then you can be assured they are always set, unchangeable and persistent; fwiw static class variables carry a performance hit and can be changed at runtime. Finally, incase anybody suggests a config ini or xml file - the simple response is, why waste the time and add in extra load that isn't needed (parsing reading etc). – nathan Jul 17 '10 at 09:44
  • thanks, yes, i got arrays too: $settings['languages'] = array( 'eng' => 'In English', 'rus' => 'По-русски', 'lat' => 'Latviski' ); $settings['site_title'] = _("Site title"); $settings['default_lng'] = 'eng'; /** * Мин. количество гололов */ $settings['min_votes'] = 3; /** * Кол-во пользователей, после которого регистрация только по приглашениям. */ $settings['invites'] = 100000;//25000; /** * Время истечения приглашения */ $settings['invite_timeout'] = 86400 * 3; i think, this variant is better, thank you all for help! – ideea Jul 17 '10 at 13:27
2

Either will work nicely, do whichever you feel most comfortable with.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
1

The best way is to store your setting in a file . and to manipulate this file declare a class which does operations on file

Sagar Varpe
  • 3,531
  • 4
  • 27
  • 43
1

If you go for the array approach, you could use array literals for slightly more readable code:

$settings = array(
    'max_photos' => 30,
    'max_width'  => 100
    //...
)
Eric
  • 95,302
  • 53
  • 242
  • 374
1

My two cents: Use both. Most application configuration settings belong in a global array variable. Config data needs to be accessible from diverse application parts, and this is what global variables are for. And keeping everything together in a an array is most sensible. An array can be extended, e.g. some options set in a config.php and the rest read from a config.ini for example.

But there is also a place for CONSTANTS. The fine line to draw is, if an option is really something that MIGHT change during the application runtime, or if it is more a of fixed/magic value. If once set up, you should not change an application setting (or rendering might fail), then this option shouldn't be in the array, but sementically fixed as constant. (That's an interpretative rule of thumb, but served me well.)

mario
  • 144,265
  • 20
  • 237
  • 291
0

I tend to put configuration values that are only accessed globally in a config array and define values that are accessed anywhere. For example:

inc/config.php

// only accessed in global scope (see init.php)
$config['error_reporting'] = E_ALL & ~E_NOTICE;
$config['memory_limit'] = '16M';
$config['time_zone'] = 'America/Los_Angeles';

// accessed within functions or class methods (see somefile.php)
define('HTTP', 'http://imac.local/my_site/');
define('HTTPS', 'http://imac.local/my_site/');
define('FILE_ROOT', '/Library/WebServer/Documents/my_site/');

inc/init.php

require 'inc/config.php';

/* Set some php configurations */
isset($config['error_reporting']) ? error_reporting($config['error_reporting']) : '';
isset($config['time_limit']) ? set_time_limit($config['time_limit']) : '';
isset($config['memory_limit']) ? ini_set('memory_limit', $config['memory_limit']) : '';
isset($config['timezone']) ? date_default_timezone_set($config['timezone']) :  date_default_timezone_set('America/Los_Angeles');

somefile.php

require 'inc/init.php';

function site_url($uri, $secure = FALSE)
{
    return $secure ? HTTPS . $uri : HTTP . $uri;
}

Keep in mind this example is stripped down to demonstrate an idea.

None
  • 5,491
  • 1
  • 40
  • 51
0

If the project settings is too heavy, then file(xml is better) is good and a dedicated class for settings is also good.

If the project settings is small, then array is best, its very fast and no I/O related issue will arise.

Even you can use database (slower and auth issue).

Sadat
  • 3,493
  • 2
  • 30
  • 45