douxi5940 2014-07-17 12:49
浏览 25
已采纳

使用define或static var? [关闭]

I'm currently updating my personal PHP framework and I have a question: what is the best way to store config variables in oop style?

In fact, I'm currently using define vars like this, because I always read that define vars are good as config vars:

define('LOG_FILE', 'app/log/log.txt');

But in an oop way, isn't it better to use a static class with static vars?

MyFramework::LOG_FILE = 'app/log/log.txt'
  • 写回答

1条回答 默认 最新

  • douhuzhi0907 2014-07-17 12:54
    关注

    Common accepted way to keep configs in Frameworks is to keep in registry (Registry Design Pattern explained in PHP)

    So that, you may create config.yaml or config.json or any other format you feel convenient to use. Then core of you framework will parse it and store in Registry. Registry by the principle is static so that makes it available "worldwide" around your framework. Also registry works a way of internal caching, to not read, parse, store the objects again and again.

    At any step you may get config this way:

    $config = Registry::get('config');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗