Home Backend Development PHP Tutorial Examples of PHP reading four configuration files (php, ini, yaml, xml)

Examples of PHP reading four configuration files (php, ini, yaml, xml)

Jul 25, 2016 am 09:11 AM
ini yaml

Examples of PHP reading four configuration files (php, ini, yaml, xml). The code is as follows. Take a look at it for yourself. You may be able to use it one day.

  1. /**

  2. * Read the table information configured in 4, now supports php.ini, xml.yaml
  3. */
  4. class Settings{
  5. var $_settings = array();
  6. /**
  7. * Get the value of some settings
  8. *
  9. * @param unknown_type $var
  10. * @return unknown
  11. */
  12. function get($var) {
  13. $var = explode('.', $var);

  14. $result = $this->_settings;

  15. foreach ($var as $key) {
  16. if (!isset($result[$key])) { return false; }

  17. $result = $result[$key];

  18. }

  19. return $result;

  20. // trigger_error ('Not yet implemented', E_USER_ERROR);//引发一个错误

  21. }

  22. function load() {

  23. trigger_error ('Not yet implemented', E_USER_ERROR);
  24. }
  25. }

  26. /**

  27. * For PHP configuration, if there is a configuration file
  28. * $file=
  29. $db = array();

  30. // Enter your database name here:

  31. $db ['name'] = 'test';

  32. // Enter the hostname of your MySQL server:

  33. $db['host'] = 'localhost';

  34. < ;p>?>

  35. Specific call:

  36. include ('settings.php'); //The original environment assumes that each class is a separate class name .php file
  37. // Load settings (PHP)

  38. $settings = new Settings_PHP;
  39. $settings->load('config.php');

  40. echo 'PHP: ' . $settings->get('db.host') . '';

  41. *

  42. */
  43. Class Settings_PHP Extends Settings {
  44. function load ($file) {
  45. if (file_exists($file) == false) { return false; }

  46. // Include file

  47. include ($file);
  48. unset($file); //销毁指定变量
  49. $vars = get_defined_vars(); //返回所有已定义变量的列表,数组,变量包括服务器等相关变量,
  50. //通过foreach吧$file引入的变量给添加到$_settings这个成员数组中去.
  51. foreach ($vars as $key => $val) {
  52. if ($key == 'this') continue;

  53. $this->_settings[$key] = $val;

  54. }
  55. }
  56. }

  57. //////////////////////读取INI文件,主要用到parser_ini_file函数,该函数返回一个数组,如第二个参数为true时则返回多维数组/////////////////////

  58. /**
  59. * ini example:
  60. * [db]
  61. name = test
  62. host = localhost
  63. Call example:
  64. $settings = new Settings_INI;
  65. $settings->load('config.ini');
  66. echo 'INI: ' . $settings->get('db.host') . '';

  67. *

  68. */
  69. Class Settings_INI Extends Settings {
  70. function load ($file) {
  71. if (file_exists($file) == false) { return false; }
  72. $this->_settings = parse_ini_file ($file, true);
  73. }
  74. }

  75. //////////////////////读取XML文件,需要用到XML_PARSER////////////////////////

  76. /**
  77. * XML例子:
  78. test
  79. localhost
  80. 调用例子:
  81. // Load settings (XML)
  82. $settings = New Settings_XML;
  83. $settings->load('config.xml');
  84. echo 'XML: ' . $settings->get('db.host') . '';
  85. *
  86. */
  87. Class Settings_XML Extends Settings {
  88. function load ($file) {
  89. if (file_exists($file) == false) { return false; }

  90. /**xmllib.php is PHP XML Library, version 1.2b, related link: http://keithdevens.com/software/phpxml

  91. The main feature of xmllib.php is to convert an array into an xml or convert xml into an array
  92. XML_unserialize: Convert an xml into an array
  93. XML_serialize: Convert an array into an xml
  94. Since PHP5, simpleXML is very good, but it still does not support the function of converting xml into an array, so xmlLIB is still very good.
  95. */
  96. include ('xmllib.php');
  97. $xml = file_get_contents($file);
  98. $data = XML_unserialize($xml);
  99. $this->_settings = $data['settings'];
  100. }
  101. }
  102. ////////////////////////////////////Read YAML format files////////// //////////////////////////////////////
  103. /**
  104. To use YAML, you must use the SPYC library. The relevant link is at http://spyc.sourceforge.net/
  105. YAML configuration example:
  106. db:
  107. name: test
  108. host: localhost
  109. */
  110. Class Settings_YAML Extends Settings {
  111. function load ($file) {
  112. if (file_exists($file) == false) { return false; }

  113. include ('spyc.php');

  114. $this- >_settings = Spyc::YAMLLoad($file);
  115. }
  116. }

Copy code


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles