简体   繁体   English

php ini vs json和暂停编译器hack

[英]php ini vs json and halt compiler hack

I am debating on using either a configuration.ini or config.json for my administration dashboard I am designing. 我正在就我正在设计的管理仪表板使用configuration.iniconfig.json进行辩论。 I like that ini is more for configuration purposes but with todays interest in JSON it seems more logical for the configurations be designed with json in mind. 我喜欢ini更多地用于配置目的,但是随着对JSON的当今兴趣,对于配置时考虑json似乎更加合乎逻辑。

Question here is should I use json for sure? 这里的问题是我应该确定使用json吗? And secondly I have this hack currently on my configuration.ini file which prevents the file being seen publicly. 其次,我目前在我的configuration.ini文件上存在此hack,这可以防止该文件被公开查看。

;<?php exit(); __halt_compiler();
; //to stop script execution if not used in our best interest!
; //remember this format

  //ini stuff

;?>

I was wondering if I could use this with the json file as well? 我想知道是否也可以将其与json文件一起使用? I haven't tested it just because I didn't want nothing strange completely screwing everything up.As well as a good description as to why this works, now I know this won't work specifically with JSON since the ; 我还没有测试过它,只是因为我不想让所有事情都搞砸了。关于它为什么起作用的很好的描述,现在我知道自;以来,它不适用于JSON ; is ini based commenting so could it be transformed to // or /**/ for json? 是基于ini的注释,因此可以将其转换为///**/用于json吗?

For interest purposes this is currently my admin__autoload.php page 出于兴趣目的,目前这是我的admin__autoload.php页面

set_include_path(dirname($_SERVER["DOCUMENT_ROOT"]));   
$ini = parse_ini_file("configurations.ini",true);
foreach($ini as $section=>$values) {
    foreach($values as $key=>$value )   {
        define("__".strtoupper($key)."__",$value);
    }
}

spl_autoload_register(function($class) {
        if(!file_exists(get_include_path(). DIRECTORY_SEPARATOR .__ADMIN__."classes/{$class}.php")) {
            echo get_include_path(). DIRECTORY_SEPARATOR .__ADMIN__."classes/{$class}.php does not exist";
        } else {
            include_once get_include_path(). DIRECTORY_SEPARATOR .__ADMIN__."classes/{$class}.php";
        }
});

__ADMIN__ was created with the foreach loop from my ini file. __ADMIN__是使用ini文件中的foreach循环创建的。

I think you're asking two different questions here. 我想您在这里问两个不同的问题。

  1. Which configuration format will best suite my needs 哪种配置格式最适合我的需求
  2. How do I prevent my configuration files from being accessed by the public 如何防止我的配置文件被公众访问

The answer to 2 is easy. 2的答案很简单。 Don't put your configuration files in your public document root. 不要将配置文件放在公共文档的根目录中。

As far as choosing between INI or JSON, it's rather subjective without understanding the use case very clearly. 至于在INI还是JSON之间进行选择,这是很主观的,没有非常清楚地了解用例。

INI is more expressive for the reader but harder to parse. INI对读者更具表现力,但更难解析。 JSON is quite portable as a serialization format, but harder to read as a human. JSON作为序列化格式具有很好的可移植性,但作为人类却很难阅读。

So if you're manually editing the configuration file a lot, it makes sense to go with INI as its slightly disambiguated than JSON. 因此,如果您要大量手动编辑配置文件,则使用INI会比较有意义,因为它比JSON稍有歧义。 If you're doing the editing through an autonomous process or one that otherwise requires portability across disparate systems, JSON may be more convenient. 如果您要通过一个自主的过程进行编辑,或者需要通过不同的系统进行移植,那么JSON可能会更方便。

A third alternative is YAML , which is a good middle ground between the human-readable aspect of INI, and the portability of JSON. 第三个选择是YAML ,它是INI的人类可读方面与JSON的可移植性之间的良好中间地带。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM