久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

PHP讀取配置文件類(php,ini,yaml,xml)

 Fujie_2014 2014-11-11
                 
<?php   
class Settings {   
    var $_settings = array ();   
       
    function get($var) {   
        $var = explode ( '.', $var );      
        $result = $this->_settings;   
        foreach ( $var as $key ) {   
            if (! isset ( $result [$key] )) {   
                return false;   
            }          
            $result = $result [$key];   
        }          
        return $result;   
    }   
       
    function load() {   
        trigger_error ( 'Not yet implemented', E_USER_ERROR );   
    }   
}   
  
class Settings_PHP extends Settings {   
    function load($file) {   
        if (file_exists ( $file ) == false) {   
            return false;   
        }   
           
        // Include file   
        include ($file);   
        unset ( $file );   
           
        // Get declared variables   
        $vars = get_defined_vars ();   
           
        // Add to settings array   
        foreach ( $vars as $key => $val ) {   
            if ($key == 'this')   
                continue;              
            $this->_settings [$key] = $val;   
        }   
       
    }   
}   
  
class Settings_INI extends Settings {   
    function load($file) {   
        if (file_exists ( $file ) == false) {   
            return false;   
        }   
        $this->_settings = parse_ini_file ( $file, true );   
    }   
}   
  
class Settings_YAML extends Settings {   
    function load($file) {   
        if (file_exists ( $file ) == false) {   
            return false;   
        }   
           
        include ('spyc.php');   
        $this->_settings = Spyc::YAMLLoad ( $file );   
    }   
}   
  
class Settings_XML extends Settings {   
    function load($file) {   
        if (file_exists ( $file ) == false) {   
            return false;   
        }   
           
        include ('xmllib.php');   
        $xml = file_get_contents ( $file );   
        $data = XML_unserialize ( $xml );   
           
        $this->_settings = $data ['settings'];   
    }   
}   
?>   
 php

/**  
* 針對(duì)PHP的配置,如有配置文件  
*config.php  
<?php  
$db = array();  
 
// Enter your database name here:  
$db['name'] = 'test';  
 
// Enter the hostname of your MySQL server:  
$db['host'] = 'localhost';  
?>  
 
//具體調(diào)用:  
include ('settings.php'); //原始環(huán)境假設(shè)每個(gè)類為單獨(dú)的一個(gè)類名.php文件  
// Load settings (PHP)  
$settings = new Settings_PHP;  
$settings->load('config.php');  
echo 'PHP: ' . $settings->get('db.host') . '';  
*  
*/  
 讀取INI文件,主要用到parser_ini_file函數(shù),該函數(shù)返回一個(gè)數(shù)組,如第二個(gè)參數(shù)為true時(shí)則返回多維數(shù)組

/**  
* ini例子:config.ini  
*   
[db]  
name = test  
host = localhost  
 
//調(diào)用例子:  
$settings = new Settings_INI;  
$settings->load('config.ini');  
echo 'INI: ' . $settings->get('db.host') . '';  
*/  
 讀取XML文件,需要用到XML_PARSER,xmllib.php在http://hudeyong926./admin/blogs/836048

/**  
* XML例子:config.xml  
<?xml version="1.0" encoding="UTF-8"?>  
<settings>  
<db>  
    <name>test</name>  
    <host>localhost</host>  
</db>  
</settings>  
 
// Load settings (XML)  
$settings = New Settings_XML;  
$settings->load('config.xml');  
echo 'XML: ' . $settings->get('db.host') . '';  
*  
*/  
 讀取YAML格式文件,使用YAML必須使用到SPYC這個(gè)庫(kù),相關(guān)鏈接在http://spyc./

/**  
YAML配置例子:config.yaml  
db:  
   name: test  
   host: localhost   
 
// Load settings (YAML)  
$settings = New Settings_YAML;  
$settings->load('config.yaml');  
echo 'YAML: ' . $settings->get('db.host') . '';  
*/  
1,。ini有點(diǎn)過(guò)時(shí),??

2,。xml比較好,,

3。yaml很好,,但是畢竟沒(méi)有標(biāo)準(zhǔn)化,。

4。txt要自己組織格式,,開(kāi)放性不好,。

5。類序列化,。比較好,,但是不熟悉的人使用比較麻煩!

6,。php定義常量(你不用修改數(shù)據(jù)嗎,?)

所以:xml最好。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多