php 自定義配置檔案

DanaSwan發表於2018-11-05
info.conf.php   //配置檔案 

return array(
   'name' =>'dana',
   'address'=>'hunan'
);


// 配置類 
config.class.php

class Config
{
    protected static $config;
    
    // 載入配置檔案
    function loadConf($confFile){

        if (is_file($confFile)){
             self::$config = include_once $confFile;
        }
    }

    function getConf($name){
        if(isset(self::$config[$name])){
            return self::$config[$name];
        }else{
            return " config $name is undefined ";
        }
    }


}


test.php  // 測試用例

require_once 'config.class.php';

$conf = new Config();

$conf->loadConf('info.conf.php');
print $conf->getConf('name');  //dana




 

 

相關文章