Home > Backend Development > PHP Tutorial > PHP read configuration file class instance

PHP read configuration file class instance

墨辰丷
Release: 2023-03-30 18:30:02
Original
4266 people have browsed it

This article mainly introduces PHP reading configuration file class examples. Interested friends can refer to it. I hope it will be helpful to everyone.

The details are as follows:

<?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;]; 
 } 
} 
?>
Copy after login

Configuration example:

/** 
* 针对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
/** 
* 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这个库
/** 
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;; 
*/
Copy after login

Summary: That’s it for this article The entire content of the article is hoped to be helpful to everyone's study.

Related recommendations:

PHP file operations and algorithms

php reversible encryption methods and principles

php techniques for traversing and intercepting strings

The above is the detailed content of PHP read configuration file class instance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template