在大规模建筑时使用define(),Pros?缺点


Using define() when mass building, Pros? Cons?

我批量生产非常相似的网站,这意味着它们都使用相同的基本组件和页面,并且都是特定于单个行业的。这些是我的低端折扣网站设计。这些网站每天的访问者都不会超过20-30人,所以服务器上的任何额外负载都不是问题。

为了节省时间,因为它们都使用相同的组件,尽管它们可能在不同的位置或以不同的顺序,我想写一个可以包含在每个网站中的定义文件,所以我可以调用定义的常量,而不是每年在我建立的每个网站上写几百次代码。为了以后的编辑目的,这会让我的生活变得更轻松。

定义文件将类似于以下内容:

define('UPCONTACT','<h1>Contact Us</h1>');
define('ULCONTACT','<a href="contact_us.php">Contact Us</a>');
define('UPABOUTUS','<h1>About Us</h1>');
define('ULABOUTUS','<a href="about_us.php">About Us</a>');

显然,这是一个非常基本的例子,但我想你已经明白了。

所以问题是,以这种方式使用define()有什么利弊?

这很好。缺点是,如果你使用常量,你不能为单个页面或网站覆盖它们。

改为使用数组:

config.php

 return array(
     'aboutus' => '<h1>About Us</h1>',
     'contactus' => '<a href="contact_us.php">Contact Us</a>'
 );

将其包含在您的网站中:

$config = include('config.php');

然后你可以很容易地打印

<?php echo $config['aboutus'] ?>

您也可以在需要时更改值:

$config = include('config.php');
$config['aboutus'] = '<h1>About My Company</h1>';

这可能是你最好的选择。

它有优点也有缺点。

好处是这种方式比从数据库加载设置更快(创建数据库;创建抽象层…)

缺点是这种方式不能由客户自定义。如果他们需要更改,请事先确保网站是静态的,每次更改都会向他们收费。

IMHO最好有一些东西可以由客户自定义,而其他东西则不然。但是以这种方式使用define()根本不存在技术问题(可能除了允许的数据类型)。

使用ini文件或类似文件的更好方法。(如果是递归任务,可以从智能手机轻松编辑:)

寻找一个内置的php函数,可以简化你的生活

http://php.net/manual/fr/function.parse-ini-file.php

或者如果你想要一个更强大、更灵活的系统,去模板化(寻找smarty,或自制regex模板化)

寻找我的第一个正则表达式函数(龙多年前)退出Smarty手动

注:

使用常量不提供动态修改它们的功能内联代码,并且是不受支持的类型(例如,如果不进行序列化,就无法存储数组)

我建议级联ini文件:

$conf_dir = dirname(__FILE__);
$config = array_merge_recursive(
    parse_ini_file($conf_dir.'base.ini'),
    parse_ini_file($conf_dir.'client.ini')
);

好处是可读性、无法执行(我喜欢锁定可能发生的事情),并且您可以在git(或您使用的任何东西)中跟踪baseini,而不是客户端。有一些缺点,但生活就是这样。它们只是感觉更干净,但肯定不会比.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,您甚至可以使用它获得真正的创造性/复杂性,并且缓存的页面命中率可以忽略不计。