简体   繁体   English

PHP如何从许多PHP文件访问配置文件?

[英]PHP how to access to config file from many php files?

In php how can I make this more elegant: 在php中如何使它更加优雅:

I have a config file from which I need values in different php files. 我有一个配置文件,需要从其中获取不同php文件中的值。 So I add $config_ini = parse_ini_file($ini_path, TRUE); 所以我加了$config_ini = parse_ini_file($ini_path, TRUE); to every of my php files. 到我的每个php文件。

I think is better to define the ini path in one variable which is available over all of my files. 我认为最好在一个变量中定义ini路径,该变量在我所有文件中都可用。 Next step would be to make the variable which contains the ini file's content available to all other files. 下一步将是使包含ini文件内容的变量可用于所有其他文件。

Is this the correct/ best way? 这是正确/最佳方法吗? How to achieve this? 如何实现呢?

All you need to do is include a file in every page that does the parse_ini_file call. 您需要做的就是在每个页面中包含一个文件来进行parse_ini_file调用。

File1.php: $config_ini = parse_ini_file($ini_path, TRUE); File1.php: $config_ini = parse_ini_file($ini_path, TRUE);

(All Other Files That Need Those Variables): require_once 'PATH/File1.php' (所有其他需要这些变量的文件): require_once 'PATH/File1.php'

echo $config_ini['mykey'];

Instead of 代替

(All Other Files That Need Those Variables): $config_ini = parse_ini_file($ini_path, TRUE); (所有其他需要这些变量的文件): $config_ini = parse_ini_file($ini_path, TRUE);

echo $config_ini['mykey'];

In this small example it doesn't seem to help much but long run with more code it is better because you can add more things to the "File1.php" which will be available in all your other files requesting it without having to edit those files individually. 在这个小例子中,它似乎并没有多大帮助,但是长期运行更多代码会更好,因为您可以向“ File1.php”中添加更多内容,而所有其他请求它的文件都可以使用它,而无需进行编辑文件。

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

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