V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iugo
V2EX  ›  PHP

PHP 如何写配置文件才好看?

  •  
  •   iugo · 2015-01-13 15:25:09 +08:00 · 2766 次点击
    这是一个创建于 3396 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我看到一种写法:

    class ET {
    
    public static $config = array();
    
    public static function writeConfig($values)
    {
    
    if (file_exists($file = PATH_CONFIG."/config.php")) include $file;
    
    if (!isset($config) or !is_array($config)) $config = array();
    $config = array_merge($config, $values);
    self::$config = array_merge(self::$config, $values);
    
    $contents = "<?php\n";
    foreach ($config as $k => $v) $contents .= '$config["'.$k.'"] = '.var_export($v, true).";\n";
    $contents .= "\n// Last updated by: ".ET::$session->user["username"]." (".ET::$session->ip.") @ ".date("r")."\n?>";
    file_put_contents($file, $contents);
    }
    
    }
    
    ET::writeConfig(array("esoTalk.skin" => $skin));
    

    想知道有什么好的轮子可用吗?

    3 条回复    2015-01-14 15:04:06 +08:00
    vibbow
        1
    vibbow  
       2015-01-13 18:39:37 +08:00
    config.php:
    <?php //config in json


    $config = file_get_contents('config.php')
    $config = substr($config, 8);
    $config = json_decode($config, true);
    Actrace
        2
    Actrace  
       2015-01-14 10:57:46 +08:00
    楼主这是用生命在水贴啊,不怕被各路大神骂死嘛....
    iugo
        3
    iugo  
    OP
       2015-01-14 15:04:06 +08:00
    @Actrace 确为新手. 若是发错地方, 还请见谅... 若叨扰到大神, block 即可.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   786 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:46 · PVG 05:46 · LAX 14:46 · JFK 17:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.