我在packagist.org上有一个PHP库,它使用一些常量,从一个项目更改到另一个项目。
我试着用这样的常量:
project1,在/conf.php中
define("HOST", "host1.com");
project2,in /conf.php
define("HOST", "host2.com");
但看上去是不对的。
在composer库中使用常量的正确方法是什么?
发布于 2017-08-16 08:36:44
我更喜欢用一种稍微不同的方式
在我的libabry我会
/vendor/vendorname/pkg/config/system.php
/vendor/vendorname/pkg/config/local.sample.php
并提供复制指示
/vendor/vendorname/pkg/config/local.sample.php
至
/config/local.php
那么,在我的代码中,我会有这样的东西
$sysconffile = static::$vendorbasedir . '/config/system.php';
if (file_exists($sysconffile)) {
$sysconf = require $sysconffile;
} else {
throw new \RuntimeException('Sys Conf Missing!');
}
$localconf = [];
$localconfile = static::$appbasedir . '/config/local.php';
if (file_exists($localconfile)) {
$localconf = require $localconfile;
}
更新:
我也更喜欢带有数据的静态类,而不是定义,因为定义在文档、类型提示和过度可写方面非常松散。
所以,一旦我有了这两个配置,我通常会
static::$config = array_replace_recursive($sysconf, $localconf);
https://stackoverflow.com/questions/45717072
复制相似问题