当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Should I use an array or object to reference system settings?

问题描述:

I am building a site that has 30-40 system settings. These are editable in a text file. I want to make sure that I set the site up with the most logical and less resource-intensive method of pulling these settings.

I have been thinking about using an array:

$system['language'] = 'en';

$system['version'] = '0.1';

And referencing them throughout the site like: echo $system['version'];

Or, I can set them up as an object:

class SiteConfig {

public $language = 'en';

public $version = '0.1';

}

$system = new SiteConfig;

And referencing them throughout the site like: echo $system->version;

Which is best to use, or does anyone have a better suggestion?

网友答案:

The approach taken by some frameworks (Laravel for example) is to create a settings file, that you pull in with require.

Your various settings are all defined in that file in a single array, that is returned:

settings.php

<?php

return array(
    'language' => 'en',
    'version'  => '0.1',
    'timezone' => 'America/Sao_Paulo',
);

Then you can use it like:

$system = require 'settings.php';
echo $system['version'];

See example #5 on the php.net docs for include for more about this return.

分享给朋友:
您可能感兴趣的文章:
随机阅读: