doushang4293 2015-04-27 01:59
浏览 29
已采纳

我应该使用数组或对象来引用系统设置吗?

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?

  • 写回答

1条回答 默认 最新

  • dtujfmfs06058 2015-04-27 02:21
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi