• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP读取配置文件类(php,ini,yaml,xml)_php

php 搞代码 7年前 (2018-06-21) 152次浏览 已收录 0个评论
 <?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  /**   * 针对PHP的配置,如有配置文件   *config.php   <?php   $db = array();     // Enter your database name here:   $db['name'] = 'test';     // Enter the hostname of your MySQL server:   $db['host'] = 'localhost';   ?>     //具体调用:   include ('settings.php'); //原始环境假设每个类为单独的一个类名.php文件   // Load settings (PHP)   $settings = new Settings_PHP;   $settings->load('config.php');   echo 'PHP: ' . $settings->get('db.host') . '';   *   */    读取INI文件,主要用到parser_ini_file函数,该函数返回一个数组,如第二个参数为true时则返回多维数组  /**   * ini例子:config.ini   *    [db]   name = test   host = localhost     //调用例子:   $settings = new Settings_INI;   $settings->load('config.ini');   echo 'INI: ' . $settings->get('db.host') . '';   */    读取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('config.xml');   echo 'XML: ' . $settings->get('db.host') . '';   *   */    读取YAML格式文件,使用YAML必须使用到SPYC这个库,相关链接在http://spyc.sourceforge.net/  /**   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有点过时??  2。xml比较好,  3。yaml很好,但是毕竟没有标准化。  4。txt要自己组织格式,开放性不好。  5。类序列化。比较好,但是不熟悉的人使用比较麻烦!  6。php定义常量(你不用修改数据吗?)  所以:xml最好。

欢迎大家阅读php读取配置文件类(php,ini,yaml,xml)_php》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP读取配置文件类(php,ini,yaml,xml)_php
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址