我建议级联INI文件:
$conf_dir = dirname(__FILE__);
$config = array_merge_recursive(
parse_ini_file($conf_dir.'base.ini'),
parse_ini_file($conf_dir.'client.ini')
);
的好处是可读性,执行无力(我想锁定下来的东西,可以是),你可以在git
跟踪base
INI(或任何你使用)而不是客户端。有一些缺点,但这就是生活。可以肯定,感觉更干净,但它们不会比.php
更快。
如果你想消除任何冗余执行(听,任何“性能优势”仍然有其“利”),系列化:
<?php
define('CACHE_DIR', '/tmp/');
// where 'http' is a path part that directly follows the app root, and will always
// be below where this file is called from.
$ini_cache = CACHE_DIR.'config.ser';
if(!file_exists($ini_cache)) {
// Build your config in any way you wish.
$conf_dir = dirname(__FILE__);
$config = array_merge_recursive(
parse_ini_file($conf_dir.'base.ini'),
parse_ini_file($conf_dir.'client.ini')
);
// Store it serialized
file_put_contents($ini_cache, serialize($config));
} else {
$config = deserialize(file_get_contents($ini_cache));
}
你可以得到这更有创意,但本质上,这允许您以任何您希望的方式存储/生成您的配置。如果你想不必删除每一个变化的序列缓存,你可以添加一个atime
检查:
<?php
define('CACHE_DIR', '/tmp/');
// where 'http' is a path part that directly follows the app root, and will always
// be below where this file is called from.
$ini_cache = CACHE_DIR.'config.ser';
$conf_dir = dirname(__FILE__);
$config = array();
if(file_exists($ini_cache)) {
$client_stat = stat($conf_dir.'client.ini');
$cache_stat = stat($ini_cache);
if($client_stat['atime'] < $cache_stat['atime']) {
$config = deserialize(file_get_contents($ini_cache));
}
}
if(empty($config)) {
// Build your config in any way you wish.
$config = array_merge_recursive(
parse_ini_file($conf_dir.'base.ini'),
parse_ini_file($conf_dir.'client.ini')
);
// Store it serialized
file_put_contents($ini_cache, serialize($config));
}
无论使用哪种序列化方法,可以使用以往$config
生成方案,你喜欢什么,如果你使用PHP ,你甚至可以获得真正的创意/复杂,并且缓存命中到页面将是微不足道的。
我认为不是使用常量,而是创建模板要容易得多。另外,您必须选择将其修改为行 – Ibu
我实际上正在考虑将其用作我的模板系统的一部分。 – user3154948