【发布时间】:2019-11-07 16:43:18
【问题描述】:
我有一个文件 (config.php),其中包含我的应用配置数据:
<?php
return [
// here is my associative configuration array
// having also Closures in it
];
我所做的是将这个文件发送到我的 Config 类,它将操作我的数据,它将成为我的配置信息接口。
Config::init(require('config.php'));
那样的话,我不想以任何其他方式访问配置信息,而不是使用公共 Config 类方法。
所以,我只需要在我的项目中包含一次 config.php(发送到 Config 类时)。
解决方案是在文件顶部定义一个常量:
<?php
define('config', true);
return [
// here is my associative configuration array
// having also Closures in it
];
这样,包含两次config.php会产生错误,因为'config'常量被定义了两次,这在php中是非法的。
但是,'config' 常量可以在第二个包含之前轻松删除。与:
runkit_constant_remove('config');
这就是为什么我需要向您寻求更安全/可靠的解决方案,它可以保证配置信息只能从 Config 类中获取。
【问题讨论】:
-
尝试 require_once
-
但是,'config' 常量可以在第二个包含之前轻松删除 什么?怎么样?
-
是的。可以随时使用 runkit_constant_remove() 删除。
-
runkit_constant_remove 是 PECL 的东西,它不是原生的 3v4l.org/RO3iV
-
你说define()以后不能去掉?