搜索
首页php教程PHP源码PHP读取配置文件类(php,ini,yaml,xml)

php代码:

<?php   
class Settings {   
    var $_settings = array ();   
        
    function get($var) {   
        $var = explode ( &#39;.&#39;, $var );      
        $result = $this->_settings;   
        foreach ( $var as $key ) {   
            if (! isset ( $result [$key] )) {   
                return false;   
            }          
            $result = $result [$key];   
        }          
        return $result;   
    }   
        
    function load() {   
        trigger_error ( &#39;Not yet implemented&#39;, 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 == &#39;this&#39;)   
                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 (&#39;spyc.php&#39;);   
        $this->_settings = Spyc::YAMLLoad ( $file );   
    }   
}   
   
class Settings_XML extends Settings {   
    function load($file) {   
        if (file_exists ( $file ) == false) {   
            return false;   
        }   
            
        include (&#39;xmllib.php&#39;);   
        $xml = file_get_contents ( $file );   
        $data = XML_unserialize ( $xml );   
            
        $this->_settings = $data [&#39;settings&#39;];   
    }   
}   
?>   
 php
 
/**  
* 针对PHP的配置,如有配置文件  
*config.php  
<?php  
$db = array();  
  
// Enter your database name here:  
$db[&#39;name&#39;] = &#39;test&#39;;  
  
// Enter the hostname of your MySQL server:  
$db[&#39;host&#39;] = &#39;localhost&#39;;  
?>  
  
//具体调用:  
include (&#39;settings.php&#39;); //原始环境假设每个类为单独的一个类名.php文件  
// Load settings (PHP)  
$settings = new Settings_PHP;  
$settings->load(&#39;config.php&#39;);  
echo &#39;PHP: &#39; . $settings->get(&#39;db.host&#39;) . &#39;&#39;;  
*  
*/ 
 读取INI文件,主要用到parser_ini_file函数,该函数返回一个数组,如第二个参数为true时则返回多维数组
 
/**  
* ini例子:config.ini  
*   
[db]  
name = test  
host = localhost  
  
//调用例子:  
$settings = new Settings_INI;  
$settings->load(&#39;config.ini&#39;);  
echo &#39;INI: &#39; . $settings->get(&#39;db.host&#39;) . &#39;&#39;;  
*/ 
 读取XML文件,需要用到XML_PARSER,xmllib.php在http://hudeyong926.iteye.com/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(&#39;config.xml&#39;);  
echo &#39;XML: &#39; . $settings->get(&#39;db.host&#39;) . &#39;&#39;;  
*  
*/ 
 读取YAML格式文件,使用YAML必须使用到SPYC这个库,相关链接在http://spyc.sourceforge.net/
 
/**  
YAML配置例子:config.yaml  
db:  
   name: test  
   host: localhost   
  
// Load settings (YAML)  
$settings = New Settings_YAML;  
$settings->load(&#39;config.yaml&#39;);  
echo &#39;YAML: &#39; . $settings->get(&#39;db.host&#39;) . &#39;&#39;;  
*/ 
1。ini有点过时??
 
2。xml比较好,
 
3。yaml很好,但是毕竟没有标准化。
 
4。txt要自己组织格式,开放性不好。
 
5。类序列化。比较好,但是不熟悉的人使用比较麻烦!
 
6。php定义常量(你不用修改数据吗?)
 
所以:xml最好。
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)