Summary XML read configuration file easy way normal way PHP parsing XML configuration file resolving database connection pool test request is full after rejecting request is filled in summary
Summary
Before always in the script-oriented way to write PHP code, so to a large extent, neither specification, nor security, more difficult to maintain. In order to reuse the code, prepare to write a set of your own tool library, so that when you write the project will be easy to use it.
The implementation today is a database connection pool, implemented as a configuration file. XML
XML, as a highly available structured language, is really concise as a configuration file, although it is valuable to have a relatively small number of valid data compared to the YAML in the recent configuration file world, such as JSON.
Basically, you can read the XML node to see its functionality. This is why large projects use XML as a configuration file.
Redundancy can be tolerated, but it does not bring a little ambiguity or maintain difficult aspects of the problem.
In PHP, using an XML file can be a feast for the eyes, even if it is relative to a Java program. But PHP programs are less elegant than python processing. reading configuration Files
Read the configuration file in fact, is to read the file, and then packaging. I usually have the following two kinds of methods. Easy Way
The first time you use this simple way, it is really a bit depressing.
$content = file_get_contents ("Filename.xml");
Echo $content;
1 2 1 2
As a result, when you use the browser to access this test PHP file, only the content portion of the XML is displayed, but the node information is not displayed at all.
Then I looked up the help document, which returned the result of a string. And then the vardump proved it. So did not think much, but also thought that this way can automatically filter out the XML tag tag information.
The last accidental test, opened the Web page source code, found that the function does read all the information of XML, but the browser will be automatically resolved by the browser. So you can only see the relevant content section. Normal Way
The usual way is to read the files step-by-step. The rest is consistent with the above programme.
Read the contents of the configuration file
$handle = fopen ("filepath", "R");
$content = Fread ($handle, FileSize ("filepath"));
1 2 3 1 2 3
PHP parsing xml
Both of these read files are actually prepared for PHP parsing XML. There are a lot of blogs about how PHP parses XML. There are a lot of ways, like simplexml,xmlreader,dom and so on. However, for a smaller XML configuration file, SimpleXML is sufficient. configuration file
<?xml version= "1.0" encoding= "UTF-8"?>
<mysql>
<!--to prevent an unexpected occurrence, please write in this standard order. It doesn't matter, either,-->
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9-10
parsing
<?php/** * As a prerequisite tool for parsing XML configuration files */class Xmlutil {public static $dbconfigpath = "./db.config.xml";
public static function Getdbconfiguration () {$dbconfig = array ();
try {//Read configuration file content $handle = fopen (self:: $dbconfigpath, "R");
$content = Fread ($handle, FileSize (self:: $dbconfigpath));
Obtains the XML document root node, then obtains the related database information $mysql = simplexml_load_string ($content);
Assigns the acquired XML node information to the associative array, which facilitates the next method call $dbconfig [' host '] = $mysql->host;
$dbconfig [' user '] = $mysql->user;
$dbconfig [' password '] = $mysql->password;
$dbconfig [' db '] = $mysql->db;
$dbconfig [' port '] = $mysql->port;
Returns the configuration information in the form of an associative array return $dbconfig; The catch (Exception $e) {throw new RuntimeException ("<mark> read Database configuration file information is wrong.
</mark><br/> ");
return $dbconfig; }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18-------------19 20 21 22 23 24 25 26 27-28 29 1 2, 3 19 20 21 22 23 24 25 26 27 28 29
Database Connection Pool
For PHP programs, optimization is endless. and the database connection pool has played the role of optimization to some extent. This makes it unnecessary for each request of the user to have to request a link resource like a database every time. Instead, it returns through a link in the existing database connection pool, which is a big boost in terms of time and efficiency.
So, here is a simple simulation of the database connection pool implementation. The core is to maintain a "pool".
Take it from the pool, use it, and return it to the pool.
<?php/**x * PHP Database Tools class Design * Guopu * December 23, 2016 * **/class DBHelper {private $dbconfig;
Private $dbpool;
Public $poolsize; Public function __construct ($poolsize =) {if (! file_exists (./utils.php))} {throw new Runtime Exception ("<mark>utils.php file is missing and cannot be initialized for configuration files.)
</mark><br/> ");
}else {require './utils.php ';
///Initialize profile information $this->dbconfig = Xmlutil::getdbconfiguration ();
Prepare database connection pool "pseudo queue" $this->poolsize = $poolsize;
$this->dbpool = Array (); for ($index = 1; $index <= $this->poolsize; $index + +) {$conn = Mysqli_connect ($this->dbconfig [' Ho St '], $this->dbconfig [' user '], $this->dbconfig [' Password '], $this->dbconfig [' db ']) or Die ("<mark> connection number According to the library failure.
</mark><br/> ");
Array_push ($this->dbpool, $conn);
}/** * Get a Database link resource from the database connection pool * * @throws errorexception * @return Mixed/Public Function Getconn () {if (count ($this-&
Gt;dbpool) <= 0 {throw new Errorexception ("There is no linked resource in the <mark> database connection pool, please try again later!</mark>");
else {return Array_pop ($this->dbpool);
}/** * Put the Used database link resources back into the database connection pool * * @param unknown $conn * @throws errorexception
*/Public Function release ($conn) {if (count ($this->dbpool) >= $this->poolsize) {
throw new Errorexception ("<mark> database connection pool is full </mark><br/>");
else {Array_push ($this->dbpool, $conn);
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 4 45 46 47 48 49 50 51