在PHP Web应用程序中保存配置变量的最好方法是什么? [英] What is the best way to save config variables in a PHP web app?

查看:177
本文介绍了在PHP Web应用程序中保存配置变量的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在.NET和PHP开发之间切换。使用 ASP.NET网站,我会在 web.config 文件中保存配置信息(例如连接字符串,目录,应用程序设置),该文件受到适当保护并且易于访问值,

PHP 中,我使用每个变量的具有静态方法的类解决此问题:

  class webconfig {
public static function defaultPageIdCode(){
return'welcome';
}
}

文件包括由应用程序变量访问的一行:

  $ dp = webconfig :: defaultPageIdCode(); 

由于PHP未编译,所以很容易 telnet 并更改网站的值,因此此解决方案运行良好,并为我提供两个优点




  • 我可以添加逻辑添加到配置变量,而不破坏与应用程序的接口

  • 这些配置变量显示为 intellisense 在我的eg Eclipse,NetBeans等。



但我可以想象还有其他方法可以解决在PHP中保存web配置设置, 。



尤其是那些有过PHP框架经验的人,还有什么方法可以保存配置变量及其优缺点? / p>

解决方案

我决定列出所有已知的方法及其优点和缺点。








全局常量



分配:




  • define('CONFIG_DIRECTIVE','value');



访问:




  • $ object = new MyObject(CONFIG_DIRECTIVE);



优点:




  • 有全域范围。

  • 自动完成



$ b

缺点:




  • 指令不能包含数组(在v7.0.0之前)。



特殊注释:




  • 不能重新分配。






替代语法文件



:XML,INI,YAML等。



分配:




  • 只需使用特定语言编辑文件即可。 (例如,对于INI文件: config_directive = value 。)


b $ b

访问:




  • 配置文件需要解析。 (例如,对于INI的 parse_ini_file()。)



优点:




  • 很可能有更适合配置文件的语法。



缺点:




  • 访问和解析文件可能产生的开销。 >





数组



分配: / h3>


  • $ config ['directive'] ='value';



访问:




  • 访问配置值的最简洁的方法使用这种方法是将需要的值传递给需要它们的对象,或者将它们传递给你的容器对象,让它处理在内部传递它们。


    • $ object = new MyObject($ config ['directive']);

    • $ container = new MyContainer($ config);




优点:




  • 指令可以是数组。



缺点:




  • 没有自动完成。



特殊注释:




  • 可能发生变量冲突。如果这是一个问题,请适当地命名您的数组以避免它们。






/ h1>

分配:




  • 有许多不同的基于类的实现。


    • 静态类。


      • myCfgObj :: setDirective('DIRECTIVE','value');


    • 实例类。


      • myCfgObj-> setDirective('DIRECTIVE','value');





访问:




  • 同样有各种基于类的实现。


    • 静态类。


      • $ object = new MyObject(myCfgObj :: getDirective('DIRECTIVE')); li>

    • 实例类。


      • $ object = new MyObject(myCfgObj-> getDirective('DIRECTIVE'));

    • h3>


      • 可以自动加载。



      / h3>


      • 往往有点冗长。

      • 如果容器类不是


      I often switch between .NET and PHP development. With ASP.NET sites I save configuration information (e.g. connection strings, directories, application setting) in the web.config file which is appropriately protected and easy to access the values, etc.

      In PHP, I solve this with a class that has static methods for each variable:

      class webconfig {
          public static function defaultPageIdCode() {
              return 'welcome';
          }
      }
      

      The file is included by the app variables are accessed with a one-line:

      $dp = webconfig::defaultPageIdCode();
      

      And since PHP isn't compiled, it is easy to telnet in and change a value for a website anyway, so this solution works fairly well and gives me these two advantages:

      • I can add logic to a config variable without breaking its interface with the application
      • these config variables appear as intellisense in my e.g. Eclipse, NetBeans, etc.

      But I can imagine there are other ways people solve saving web config settings in PHP which may have other advantages.

      Especially those who have experience with a number of PHP frameworks, what are other ways of saving config variables and their advantages and disadvantages?

      解决方案

      I've decided to list all known methods along with their advantages and disadvantages.

      I've marked this answer as a community wiki so collaboration is easier.


      Global Constants

      Assigning:

      • define('CONFIG_DIRECTIVE', 'value');

      Accessing:

      • $object = new MyObject(CONFIG_DIRECTIVE);

      Advantages:

      • Has global scope.
      • Autocompleted by most IDEs.
      • Has an agreed upon naming convention (UPPERCASE_UNDERSCORE_SEPARATED).

      Disadvantages:

      • Directives cannot contain arrays (prior to v7.0.0).

      Special Notes:

      • Cannot be reassigned.

      Alternate Syntax Files

      For example: XML, INI, YAML, etc.

      Assigning:

      • Simply edit the file in it's specific language. (For example, for INI files: config_directive = value.)

      Accessing:

      • The config file needs to be parsed. (For example, for INI's: parse_ini_file().)

      Advantages:

      • Most likely has a syntax more suited for a config file.

      Disadvantages:

      • Possible overhead of accessing and parsing the file.

      Array

      Assigning:

      • $config['directive'] = 'value';

      Accessing:

      • The cleanest method of accessing configuration values using this method is to pass the required values to the object that needs them on creation, or pass them to your container object and let it handle passing them out internally.
        • $object = new MyObject($config['directive']);
        • $container = new MyContainer($config);

      Advantages:

      • Directives can be arrays.

      Disadvantages:

      • No autocompletion.

      Special Notes:

      • Variable collisions can occur. If this is a concern, name your array appropriately to avoid them.

      Class

      Assigning:

      • There are many different class based implementations.
        • Static class.
          • myCfgObj::setDirective('DIRECTIVE', 'value');
        • Instanced class.
          • myCfgObj->setDirective('DIRECTIVE', 'value');

      Accessing:

      • Again there are various class based implementations.
        • Static class.
          • $object = new MyObject(myCfgObj::getDirective('DIRECTIVE'));
        • Instanced class.
          • $object = new MyObject(myCfgObj->getDirective('DIRECTIVE'));

      Advantages:

      • Can be autoloaded.

      Disadvantages:

      • Tends to be a bit verbose.
      • Can be hard to maintain if a container class is not being used.

      这篇关于在PHP Web应用程序中保存配置变量的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆