您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 平凉分类信息网,免费分类信息发布

PHP读取配置文件类实例

2024/4/26 7:00:04发布3次查看
本篇文章主要介绍php读取配置文件类实例,感兴趣的朋友参考下,希望对大家有所帮助。
具体如下:
<?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的配置,如有配置文件 *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/** * 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这个库/** 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') . ''; */
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
php的文件操作及算法
php可逆加密的方法及原理
php针对字符串的遍历与截取操作技巧
以上就是php读取配置文件类实例的详细内容。
平凉分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录